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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB code to reference a series of worksheets

Status
Not open for further replies.

bbannock

Technical User
Oct 24, 2003
30
US
Hi everyone.

I'm looking to write code for some macros that I'm creating, the only problem is that the way I'm doing it now is very tedious. I am manually changing the macro so that id references every tab individually. I'm wondering if there is a way to reference the series of sheets as a whole. Here are two examples of the macros...

-----------------------------------------------------------

1)

Sub RefCompData()
X = EssVRetrieve("Sheet 1", Null, 1)
X = EssVRetrieve("Sheet 2", Null, 1)
If X = 0 Then
MsgBox ("REFRESH SUCCESSFUL")
Else
MsgBox ("REFRESH FAILED - TRY AGAIN")
End If
Calculate
End Sub

2)

Sub HIDEROWS()
'
' Macro1 Macro
' Macro recorded 09/11/2003 by A953997
'

'
Sheets("Sheet 1").Select
range("P1").Select
range(Selection, Selection.End(xlDown)).Select
For Each X In Selection
If ActiveCell.Value = "HIDE" Then
Selection.EntireRow.Hidden = True
End If
If ActiveCell.Value = "SHOW" Then
Selection.EntireRow.Hidden = False
End If
ActiveCell.Offset(1, 0).Select
Next X
range("B10").Select

Sheets("Sheet 2").Select
range("P1").Select
range(Selection, Selection.End(xlDown)).Select
For Each X In Selection
If ActiveCell.Value = "HIDE" Then
Selection.EntireRow.Hidden = True
End If
If ActiveCell.Value = "SHOW" Then
Selection.EntireRow.Hidden = False
End If
ActiveCell.Offset(1, 0).Select
Next X
range("B10").Select
End Sub

-----------------------------------------------------------

Please let me know if there is an easier way to do this...otherwise I'll be writing a lot of unecesarry code.
Thanks alot,
Brandon
 
You could reference the sheet as follows

i = 1
Do While i < last_sheet_number
sheet_ref = &quot;Sheet &quot; & i 'Sheet 1

Sheets(sheet_ref).Select
range(&quot;P1&quot;).Select
range(Selection, Selection.End(xlDown)).Select
For Each X In Selection
If ActiveCell.Value = &quot;HIDE&quot; Then
Selection.EntireRow.Hidden = True
End If
If ActiveCell.Value = &quot;SHOW&quot; Then
Selection.EntireRow.Hidden = False
End If
ActiveCell.Offset(1, 0).Select
Next X
range(&quot;B10&quot;).Select



i=i+1

Loop

If the code for all the sheets is the same you can just reference the sheet as above and loop through all the sheets.
 
I was looking for somethign similar to the code I already head, however instead of writing &quot;sheet 1&quot; I would insert the range of sheets

ie. sheet 1:sheet6
 
The code I have posted will work through sheet 1 then sheet 2 .... until sheet 6 has been done. Is this not what you wanted?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top