Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I loop through worksheets in a workbook?

Status
Not open for further replies.

KellyStee

Technical User
Jul 30, 2001
106
US
I'm trying to create CSV files from an Excel workbook. I need to create a file for each worksheet in the workbook, but the number of worksheets varies from workbook to workbook.
I know I can open the workbook, count the number of sheets in it and create a Do Loop for each worksheet, I just don't know how to move on to the next worksheet, after I finished with the previous one.
Here's a part of my code:

lngSheetCount = objExcel.Sheets.Count
Set objExcel_Sheet = objExcel.ActiveSheet
Do Until x = lngSheetCount
objExcel_Sheet.SaveAs FileName:=objExcel_Sheet.Name, FileFormat:=xlCSV, CreateBackup:=False
x = x + 1
Move to next sheet?
Loop

Thanks!
Kelly
 
ok this is out of an faq im writing but here goes

Private Sub RenameSheets()

Dim i As Integer
i = 1
For Each Sheet In myExcelWorkbook.Sheets
Set myExcelWorksheet = Sheet
myExcelWorksheet.Name = "Name" & i
i = i + 1
Next Sheet

End Sub

hope this will get you started!!

good luck! If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top