ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping through Sheets (https://www.excelbanter.com/excel-programming/311367-re-looping-through-sheets.html)

Jim Thomlinson[_3_]

Looping through Sheets
 
I am not 100% sure exactly what you are looking for but if all you want to do
is loop through all of the sheets in a workbook this will do it...

dim wks as worksheet

for each wks in worksheets
'do that voodoo that you do
next wks

"Utkarsh Majmudar" wrote:

I have a Sub that I run on each of the Sheets in my workbook. This Sub
essentially copies data from a set of files into each sheet of my main
file. Is there a way in which I need not run the macro 'n' number of
times but loop through all the sheets at one go.

My current code looks like this:
***************
Sub Populate()

Dim Sname As String
Dim IFname As String

Application.ScreenUpdating = False
myfile = ActiveWorkbook.Name
mypath = ActiveWorkbook.Path
Range("E11").Value = ActiveSheet.Name
Sname = Range("E11").Value
IFname = Sname & ".xls"

Workbooks.Open Filename:=mypath & "\" & IFname
'Enter Basic Information
Workbooks(IFname).Sheets(Sname).Activate
Range("E1").Select
Selection.Copy
Windows(myfile).Activate
Range("E1").Select
ActiveSheet.Paste

...... more stuff here

Workbooks(IFname).Sheets(Sname).Activate
ActiveWorkbook.Close savechanges:=False
Workbooks(myfile).Sheets(Sname).Activate
Range("A1").Select
Application.ScreenUpdating = True
End Sub
*****************

Thanks for the help!

Utkarsh


Utkarsh Majmudar[_2_]

Looping through Sheets
 
Jim

Your idea is right. Unfortunately the code doesn't run. It populates the
first sheet but does not move on to the next one.

Any clues?

Utkarsh

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

CBrine

Looping through Sheets
 

Can you paste your revised code. We should be able to figure out what'
wrong. Hard to tell without the code

--
CBrin

-----------------------------------------------------------------------
CBrine's Profile: http://www.excelforum.com/member.php...fo&userid=1470
View this thread: http://www.excelforum.com/showthread.php?threadid=26324


Utkarsh Majmudar[_2_]

Looping through Sheets
 
Thanks to all those who helped. Resolved the problem with the following
code that I found on a posting:

****************

Dim sh As Worksheet
For i = 1 To Sheets.Count
Set sh = Sheets(i)
sh.Activate

'My voodoo stuff here

Next i

*****************

Utkarsh

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


All times are GMT +1. The time now is 02:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com