OzGrid

Working With Excel Worksheets In Excel VBA Macro Code

< Back to Search results

 Category: [Excel]  Demo Available 

Working With Excel Worksheets In Excel VBA Macro Code

 

Restrict vba loops to specific worksheets and much more

Working With Excel Sheets In VBA: You'll get all the below codes, plus more. All code is ready to use, unless stated otherwise. Below is some examples on non ready code.  All code has liberal comments so you can understand the examples.

'''''''''''''''''''''''''''
''WORKING WITH WORKSHEETS''
'''''''''''''''''''''''''''

Sub SelectedWorksheets()
'http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm
Dim ws As Worksheet

    For Each ws In ActiveWindow.SelectedSheets
        With ws
        
            'With Code Here
        
        End With
    Next ws
End Sub



Sub WorksheetsByCodeName()
'http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm
Dim ws As Worksheet

    For Each ws In Worksheets
        'Select case is case sensitive
        Select Case UCase(ws.CodeName)
        'http://www.ozgrid.com/VBA/select-case.htm
           Case "SHEET1", "SHEET3", "SHEET5" 'ect
                With ws
            
                    'With Code Here
            
                End With
           'Optional use when most sheets are to be used
           'In this case, use "Case" to name sheets to be NOT used
           Case Else
                With ws
        
                    'With Code Here
        
                End With
     End Select
     
    Next ws
End Sub


Sub WorksheetsByName()
Dim ws As Worksheet
'http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm
    For Each ws In Worksheets
        'Select case is case sensitive
        'http://www.ozgrid.com/VBA/select-case.htm
        Select Case UCase(ws.Name)
           Case "SHEET1", "SHEET3", "SHEET5" 'ect
                With ws
            
                    'With Code Here
            
                End With
           'Optional use when most sheets are to be used
           'In this case, use "Case" to name sheets to be NOT used
           Case Else
                With ws
        
                    'With Code Here
        
                End With
     End Select
     
    Next ws
End Sub


Sub WorksheetsByNamingConvention()
Dim ws As Worksheet
'http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm
    For Each ws In Worksheets
    
       If ws.Name Like "Data*" Then
                With ws
            
                    'With Code Here
            
                End With
       End If
     
    Next ws
End Sub
 

See also:

Index to Excel VBA Code
Index to Excel Freebies
Excel Visual Basic Editor Environment Tips & Tricks
Vlookup Across Excel Worksheets

 

See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)