erik.van.geit
MrExcel MVP
- Joined
- Feb 1, 2003
- Messages
- 17,832
Hi, all,
This is my (and yours if you want) "for future reference" post about WORKSHEETS.
(summary of my libraries and several sources: helpfiles, forumthreads, experience, ...)
You'll probably find the answer to some of your questions about worksheets.
New interesting information or relevant comments are WELCOME
Please do NOT post your specific project-oriented questions here. Anyway I'll not reply to those questions.
REFERENCING WORKSHEETS
To reference sheets when coding in VBA, you can use 3 properties
1. "code"name (in VBA)
2. "index" (relative TAB position)
3. "user"name (TAB name)
Each of the three styles has its pros and contras.
1. The codename is the VBA-name that Excel is automatically assigning to a sheet when created.
NOTE: The only time you cannot use a sheet's CodeName is when you reference a sheet that is in a workbook different from the one in which the code resides.
2. The index (position) of the sheet in the workbook is calculated by counting sheet-tabs from the left, so the left tab is the first sheet, etcetera ...
this referencestyle can be useful in loops
adding or moving sheets can disturb this code
3. The "user"name is what you see on the sheet tab.
used for "hardcoding"
an error will occur in the code above when sheetname has changed
Within the VBE window you can see two referencestyles next to each other. The name to the left is the code name of the sheet and the name to the right (between parentheses) is the "user"name. For example, renaming Sheet1 to "ProjectList", the name in the project explorer would then appear as: Sheet1(ProjectList).
LOOP THROUGH WORKSHEETS
There are different ways to loop through the sheets: using indexnumbers, through the collection, using an array or the selected sheets. Loop using the codenames is too cumbersome in most of the cases to my sense.
1. loop using the index number (tabs from left to right including invisible tabs)
see code "view_123" from above
2. loop through the entire collection
you can use some code to exclude or include certain sheets (most of the times we use the names for that purpose)
3. loop through a predefined array of sheets
3A. using names
3B. using indexes
3C. the list can be taken from a sheet
instead of hardcoded within the macro
4. loop through selected sheets
barely used to my sense
5. loop using codenames
displayed as an example, but not userfriendly to my sense
also this code is rather "stupid"
EXTRAS
1. synchronise tab & codenames
2. add sheet if it doesn't exist
I hope this will be useful
best regards,
Erik
This is my (and yours if you want) "for future reference" post about WORKSHEETS.
(summary of my libraries and several sources: helpfiles, forumthreads, experience, ...)
You'll probably find the answer to some of your questions about worksheets.
New interesting information or relevant comments are WELCOME
Please do NOT post your specific project-oriented questions here. Anyway I'll not reply to those questions.
REFERENCING WORKSHEETS
To reference sheets when coding in VBA, you can use 3 properties
1. "code"name (in VBA)
2. "index" (relative TAB position)
3. "user"name (TAB name)
Each of the three styles has its pros and contras.
1. The codename is the VBA-name that Excel is automatically assigning to a sheet when created.
Code:
Sub Rename()
'using codename sheet1 you can reference the worksheet
'regardless of the "user" name
sheet1.Activate
sheet1.Name = InputBox("Please type sheetname", "RENAME SHEET1", sheet1.Name)
sheet1.Name = InputBox("Please type sheetname", "RENAME SHEET1", sheet1.Name)
End Sub
2. The index (position) of the sheet in the workbook is calculated by counting sheet-tabs from the left, so the left tab is the first sheet, etcetera ...
this referencestyle can be useful in loops
Code:
Sub view_123()
Dim I As Integer
For I = 1 To 3
Sheets(I).Select
MsgBox "You are viewing Sheet " & Sheets(I).Name & _
" which is at position " & I & " (counting from the left)", 64, "SHEET INFO"
Next I
End Sub
3. The "user"name is what you see on the sheet tab.
used for "hardcoding"
Code:
Sub test()
Sheets("ProjectList").Select
End Sub
Within the VBE window you can see two referencestyles next to each other. The name to the left is the code name of the sheet and the name to the right (between parentheses) is the "user"name. For example, renaming Sheet1 to "ProjectList", the name in the project explorer would then appear as: Sheet1(ProjectList).
LOOP THROUGH WORKSHEETS
There are different ways to loop through the sheets: using indexnumbers, through the collection, using an array or the selected sheets. Loop using the codenames is too cumbersome in most of the cases to my sense.
1. loop using the index number (tabs from left to right including invisible tabs)
see code "view_123" from above
2. loop through the entire collection
you can use some code to exclude or include certain sheets (most of the times we use the names for that purpose)
Code:
Sub viewall_exceptone()
Dim sht As Worksheet
For Each sht In Worksheets
If LCase(sht.Name) <> "summary" Then
MsgBox sht.Name
End If
Next sht
End Sub
3. loop through a predefined array of sheets
3A. using names
Code:
Sub operate_on_sheets()
'Erik Van Geit
'050910
'do some operations with certain sheets
Dim SheetsArray As Variant
Dim i As Integer
SheetsArray = Array("total", "report", "current month")
For i = 0 To UBound(SheetsArray)
Sheets(SheetsArray(i)).PrintOut Copies:=1
Next i
End Sub
Code:
Sub operate_on_sheets()
'Erik Van Geit
'050911
'do some operations with certain sheets
Dim SheetsArray As Variant
Dim i As Integer
SheetsArray = Array(5, 6, 9, 19)
For i = 0 To UBound(SheetsArray)
Sheets(SheetsArray(i)).Select '.PrintOut Copies:=1
Next i
End Sub
instead of hardcoded within the macro
Code:
Sub operate_on_sheets()
Dim SheetsArray As Variant
SheetsArray = Sheets(4).Range("A1:A3")
Sheets(Application.Transpose(SheetsArray)).Select
End Sub
4. loop through selected sheets
barely used to my sense
Code:
Sub test()
Dim ws As Worksheet
Dim xlsheetArray() As Variant
Dim x As Integer
ReDim xlsheetArray(ActiveWindow.SelectedSheets.Count - 1)
For Each ws In ActiveWindow.SelectedSheets
xlsheetArray(x) = ws.Name
x = x + 1
Next ws
For x = 0 To UBound(xlsheetArray)
MsgBox xlsheetArray(x)
Next x
End Sub
5. loop using codenames
displayed as an example, but not userfriendly to my sense
also this code is rather "stupid"
Code:
Sub ShowSheetCodenames()
Dim i As Integer
For i = 1 To Sheets.Count
MsgBox ActiveWorkbook.VBProject _
.VBComponents("Sheet" & i).Properties("_CodeName")
Next i
End Sub
EXTRAS
1. synchronise tab & codenames
Code:
Option Explicit
Sub sheetcodename_is_sheetname()
'Erik Van Geit
'060907
Dim sh As Worksheet
Dim msg As String
msg = "This code will replace all codenames of your sheets by the tabnames"
If MsgBox(msg, 36, "Replace codenames") = vbNo Then Exit Sub
msg = ""
On Error Resume Next
For Each sh In Worksheets
ActiveWorkbook.VBProject.VBComponents(sh.CodeName).Properties( _
"_CodeName").Value = Application.Substitute(sh.Name, " ", "_")
If Err Then
Err.Clear
msg = msg & vbLf & sh.Name
End If
Next sh
If msg <> "" Then
msg = "Some codenames could not be changed" & msg
Workbooks.Add
Range("A1") = msg
MsgBox msg, 48, "ERROR REPORT"
End If
End Sub
2. add sheet if it doesn't exist
Code:
Sub add_sheet()
Dim x As Worksheet
Dim sh_name As String
sh_name = "temp"
On Error Resume Next
With ThisWorkbook
Set x = .Sheets(sh_name)
If Err = 9 Then
.Worksheets.Add.Name = sh_name
End If
End With
On Error GoTo 0
End Sub
I hope this will be useful
best regards,
Erik