Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Looping Through Worksheets Given in a List?

Hi All,

I have the following code which is not currently working, I was wondering if
someone could spot the (probably obvious) error?

What I have is a list of worksheet names in cells range E3:H6. I want the
macro to step through these and perform actions in each worksheet. I also
want it to check if the worksheet actually exists before performing any
actions.

At the moment the macro halts at the Sheets(myCell).Select part where I'm
trying to refer to the worksheet name.

Any help would be much appreciated!
Marc



Sub Macro3()

For Each myCell In Range("E3:H6")

If myCell.Value < "" Then

Sheets(myCell).Select

.....rest of working code goes here.....

End If

Next myCell

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Looping Through Worksheets Given in a List?

Marc,

Because you are selecting sheets then this must go in a general module.
Alt+F11 topn vb editor. Right click 'ThisWorkbook' and insert module and
paste the code in there. note that I've also qualified the range for E3 - H6
being in sheet1 so change to suit but you must qualify it.

Note it is unlikely you will actually need to select the sheet to do what
you want

Sub Macro3()
For Each myCell In Sheets("Sheet1").Range("E3:H6")
If myCell.Value < "" Then
Sheets(CStr(myCell)).Select
' .....rest of working code goes here.....
End If
Next myCell
End Sub

Mike

"Marc T" wrote:

Hi All,

I have the following code which is not currently working, I was wondering if
someone could spot the (probably obvious) error?

What I have is a list of worksheet names in cells range E3:H6. I want the
macro to step through these and perform actions in each worksheet. I also
want it to check if the worksheet actually exists before performing any
actions.

At the moment the macro halts at the Sheets(myCell).Select part where I'm
trying to refer to the worksheet name.

Any help would be much appreciated!
Marc



Sub Macro3()

For Each myCell In Range("E3:H6")

If myCell.Value < "" Then

Sheets(myCell).Select

.....rest of working code goes here.....

End If

Next myCell

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Looping Through Worksheets Given in a List?

Thanks a lot Mike, that works great!

One last thing that's puzzling me. Is there any way to alter the If
statement to also check if 'myCell.Value' does not exist as a worksheet?

Marc

"Mike H" wrote:

Marc,

Because you are selecting sheets then this must go in a general module.
Alt+F11 topn vb editor. Right click 'ThisWorkbook' and insert module and
paste the code in there. note that I've also qualified the range for E3 - H6
being in sheet1 so change to suit but you must qualify it.

Note it is unlikely you will actually need to select the sheet to do what
you want

Sub Macro3()
For Each myCell In Sheets("Sheet1").Range("E3:H6")
If myCell.Value < "" Then
Sheets(CStr(myCell)).Select
' .....rest of working code goes here.....
End If
Next myCell
End Sub

Mike

"Marc T" wrote:

Hi All,

I have the following code which is not currently working, I was wondering if
someone could spot the (probably obvious) error?

What I have is a list of worksheet names in cells range E3:H6. I want the
macro to step through these and perform actions in each worksheet. I also
want it to check if the worksheet actually exists before performing any
actions.

At the moment the macro halts at the Sheets(myCell).Select part where I'm
trying to refer to the worksheet name.

Any help would be much appreciated!
Marc



Sub Macro3()

For Each myCell In Range("E3:H6")

If myCell.Value < "" Then

Sheets(myCell).Select

.....rest of working code goes here.....

End If

Next myCell

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Looping Through Worksheets Given in a List?

Option Explicit
Sub Macro3()
Dim MyCell as Range
dim wks as worksheet

For Each myCell In activesheet.Range("E3:H6").cells
If myCell.Value < "" Then

set wks = nothing
on error goto 0
set wks = worksheets(cstr(mycell.value))
on error goto 0

if wks is nothing then
'skip it
else
wks.select
'rest of code
end if

end if

Next myCell

End Sub

But you don't usually need to select a sheet to work with it.

instead of:
wks.select
range("a1").value = "hi"

You could use:
wks.range("a1").value = "hi"



Marc T wrote:

Hi All,

I have the following code which is not currently working, I was wondering if
someone could spot the (probably obvious) error?

What I have is a list of worksheet names in cells range E3:H6. I want the
macro to step through these and perform actions in each worksheet. I also
want it to check if the worksheet actually exists before performing any
actions.

At the moment the macro halts at the Sheets(myCell).Select part where I'm
trying to refer to the worksheet name.

Any help would be much appreciated!
Marc

Sub Macro3()

For Each myCell In Range("E3:H6")

If myCell.Value < "" Then

Sheets(myCell).Select

.....rest of working code goes here.....

End If

Next myCell

End Sub


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through worksheets Hermeticia Excel Programming 3 April 10th 08 05:01 AM
Looping Thru Worksheets Jay Excel Programming 2 April 12th 07 03:14 PM
Looping through Worksheets Steve Excel Programming 7 July 12th 05 07:56 PM
looping through worksheets Alex ekster Excel Programming 1 July 21st 03 03:16 AM
looping through worksheets alex Excel Programming 0 July 20th 03 06:43 PM


All times are GMT +1. The time now is 04:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"