|
-
Nov 14th, 2006, 06:51 PM
#1
Thread Starter
Fanatic Member
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.
-
Nov 14th, 2006, 09:46 PM
#2
Re: Iterating all worksheets in Excel
VB Code:
Dim s As Worksheet
For Each s In Sheets
Debug.Print s.Name ' put you code in here
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
-
Nov 15th, 2006, 01:13 AM
#3
Thread Starter
Fanatic Member
Re: Iterating all worksheets in Excel
-
Nov 15th, 2006, 01:58 AM
#4
Thread Starter
Fanatic Member
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?
-
Nov 15th, 2006, 02:05 AM
#5
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.
-
Nov 15th, 2006, 02:17 AM
#6
Thread Starter
Fanatic Member
Re: Iterating all worksheets in Excel
Hehe. For some weird reason, for each is not iterating. Hehe. Anyway, thanks!
-
Nov 15th, 2006, 02:33 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 15th, 2006, 02:45 AM
#8
Thread Starter
Fanatic Member
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.
-
Nov 15th, 2006, 02:50 AM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|