Share via

loop through 2 non adjacent column

Anonymous
2021-01-07T22:37:23+00:00

Hi

need to loop through 2 columns, top to botton from the first and then top to bottom second.  Managed to do just one range:

Set rngOccasion = Range("A89:A103")

this loops ok if i declare one range only 

        For Each cl In rngOccasion.Cells

                        If cl.Value = targetDate Then

                            flagNewDate = False

                            Exit For

                        End If

            Next cl

need to go through 2 ranges

Set rngOccasion = Range("A89:A103", "L89:L103")

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2021-01-07T23:04:08+00:00

Use

Set rngOccasion = Range("A89:A103,L89:L103")

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-01-07T23:33:17+00:00

    Re:  ranges

    Sub Testing()

    Dim rngOccasion As Excel.Range

    Dim rArea As Excel.Range

    Dim rCell As Excel.Range

    Set rngOccasion = Range("A89:A103", "L89:L103")

    VBA.MsgBox "Areas " & rngOccasion.Areas.Count & vbCr & _

               "Cells " & rngOccasion.Cells.Count

             

    Set rngOccasion = Range("A89:A103, L89:L103")

    VBA.MsgBox "Areas " & rngOccasion.Areas.Count & vbCr & _

               "Cells " & rngOccasion.Cells.Count

              

    For Each rArea In rngOccasion

      For Each rCell In rArea

        'do something

      Next 'rCell

    Next   'rArea

    End Sub

    '---

    Info...

    Reference            Meaning

      'Range("A1")       Cell A1

      'Range("A1:B5")  Cells A1 through B5

      'Range("C5:D9,G9:H16")  A multiple-area selection

      'Range("A:A")     Column A

      'Range("1:1")     Row one

      'Range("A:C")    Columns A through C

      'Range("1:5")     Rows one through five

      'Range("1:1,3:3,8:8")    Rows one, three, and eight

      'Range("A:A,C:C,F:F")  Columns A, C, and F

    '---

    NLtL

    https://1drv.ms/u/s!Au8Lyt79SOuhZ\_zj8wEtOjDcqAI?e=MrzfpA

    Thanks for that, appreciate it

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-01-07T23:32:19+00:00

    Thanks again!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2021-01-07T23:26:00+00:00

    Re:  ranges

    Sub Testing()
    Dim rngOccasion As Excel.Range
    Dim rArea As Excel.Range
    Dim rCell As Excel.Range

    Set rngOccasion = Range("A89:A103", "L89:L103")
    VBA.MsgBox "Areas " & rngOccasion.Areas.Count & vbCr & _
               "Cells " & rngOccasion.Cells.Count
             
    Set rngOccasion = Range("A89:A103, L89:L103")
    VBA.MsgBox "Areas " & rngOccasion.Areas.Count & vbCr & _
               "Cells " & rngOccasion.Cells.Count
              
    For Each rArea In rngOccasion
      For Each rCell In rArea
        'do something
      Next 'rCell
    Next   'rArea
    End Sub

    '---
    Info...
    Reference            Meaning
      'Range("A1")       Cell A1
      'Range("A1:B5")  Cells A1 through B5
      'Range("C5:D9,G9:H16")  A multiple-area selection
      'Range("A:A")     Column A
      'Range("1:1")     Row one
      'Range("A:C")    Columns A through C
      'Range("1:5")     Rows one through five
      'Range("1:1,3:3,8:8")    Rows one, three, and eight
      'Range("A:A,C:C,F:F")  Columns A, C, and F
    '---

    NLtL https://1drv.ms/u/s!Au8Lyt79SOuhiXoNjAh-_-zLi49O

    Was this answer helpful?

    0 comments No comments