Results 1 to 9 of 9

Thread: Iterating all worksheets in Excel

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Iterating all worksheets in Excel

    How can I iterate all the worksheets in Excel with the Macro->Visual Basic? Or does this matter if it's a Macro or not. Not totally familiar with VB6 so bear with me.
    Last edited by nebulom; Nov 15th, 2006 at 02:56 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Iterating all worksheets in Excel

    VB Code:
    1. Dim s As Worksheet
    2. For Each s In Sheets
    3.     Debug.Print s.Name  ' put you code in here
    4. Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Iterating all worksheets in Excel

    Millions of thanks!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Iterating all worksheets in Excel

    Btw, I can't iterate the same sheets twice. It goes only for one iteration.
    Code:
    For Each w in Sheets
    . . . ' Do something
    Next
    
    For Each w in Sheets
    . . . ' Do another thing -- This goes only for the last sheet
    Nex
    Is there any specific functions to reset the sheet?

  5. #5
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Iterating all worksheets in Excel

    im not sure, try something like this
    Code:
    For Each w in Sheets
    . . . ' Do something
    Next
    set w=nothing
    set w = New Worksheet
    For Each w in Sheets
    . . . ' Do another thing -- This goes only for the last sheet
    Next
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Iterating all worksheets in Excel

    Hehe. For some weird reason, for each is not iterating. Hehe. Anyway, thanks!

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Iterating all worksheets in Excel

    Post #2's code works just as you wanteed, at least for me. The sheets are all in the same workbook and running the code in Excels VBA IDE?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Iterating all worksheets in Excel

    Hmmm. Yeah I can see that it can iterate at least when I set a counter for the sheets. But didn't save to file. Here's my code.
    Code:
    Dim rowData As String
        
        Dim fileName As String
        Dim boxConfCode As String
        
        Dim w As Worksheet
        
        ' Save for Box Configuration
        fileName = Application.GetSaveAsFilename(fileFilter:="All Files (*.psv), *.psv")
        If fileName <> "" Then
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set writer = fs.CreateTextFile(fileName, True)
            
            For Each w In Worksheets
                With w
                    rowData = ""
                    boxConfCode = Trim(.Cells(2, 1).Value)
                    rowData = rowData + boxConfCode
                    rowData = rowData + "|" + Trim(.Cells(2, 2).Value)
                    writer.WriteLine (rowData)
                End With
            Next
            
            writer.Close
        End If
        
        ' Save for Box Configuration Operations
        fileName = Application.GetSaveAsFilename(fileFilter:="All Files (*.psv), *.psv")
        If fileName <> "" Then
            Set fs2 = CreateObject("Scripting.FileSystemObject")
            Set writer = fs.CreateTextFile(fileName, True)
            rowCounter = 6
            
            For Each w In Worksheets
                With w
                    While Trim(.Cells(rowCounter, "A").Value) <> ""
                        rowData = ""
                        rowData = rowData + boxConfCode
                        rowData = rowData + "|" + Trim(.Cells(rowCounter, 1).Value) + "|"
       
                        writer.WriteLine (rowData)
                        rowCounter = rowCounter + 1
                    Wend
                End With
            Next
       
            writer.Close
           'MsgBox "Save to " + fileName
        End If
    This may not be related to sheets iteration because of the fact that IT IS iterating.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Iterating all worksheets in Excel

    Bahahaha. I admit. I AM stupid. It's the rowCounter. It iterates alright. Thanks mates. Good day.

Posting Permissions

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



Click Here to Expand Forum to Full Width