Consulting

Results 1 to 5 of 5

Thread: VBA to loop through an array

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    VBA to loop through an array

    I have a column (A) with 12 months January to December in a Column.
    I have loaded those months into an array like;

    Dim myArray     As Variant
     myArray = Application.Transpose(Worksheets("Sheet1").Range("A1:A12"))
    I have Sheet names with the names of the Array names

    How can I loop through that array to act on cells on each sheet in the array.

    Something like

    For Each sh In myArray
     sh.Range("D1").Value = DONE
    I get an error " Object Required "

    Thanks

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    For Each sh In Worksheets(myArray)

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Thanks mana :

    For Each sh In Worksheets(myArray) is correct.That corrected that mistake, however, it will NOT let me select a specific cell on any of he sheets.
    i.e. sh.Range("D1").Select

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    For Each sh In Worksheets(myArray)
        sh.select
        sh.Range("D1").Select
    

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    You could get rid of the A1:A12 list of months if you wanted to

    Also, it's usually not necessary to select WS or Cells before acting on them


    Option Explicit
    Sub test()
        Dim i As Long
        Dim wsMonth As Worksheet
        Dim sMonth As String
        
        For i = 1 To 12
            sMonth = Application.GetCustomListContents(4)(i)
        
            Debug.Print sMonth
    '        Set wsMonth = Worksheets(sMonth)
    '        wsMonth.Range("D3").Value = 123
            
        Next i
    End Sub
    
    'or possibly better
    
    Sub test2()
        Dim i As Long
        Dim aryMonthSheets(1 To 12) As Worksheet
        Dim sMonth As String
        
        For i = 1 To 12
            sMonth = Application.GetCustomListContents(4)(i)
            Set aryMonthSheets(i) = Worksheets(sMonth)
        Next i
            
        aryMonthSheets(3).Range("D3").Value = 123
            
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •