Define sheet names VBA

erlisocial

New Member
Joined
Feb 5, 2020
Messages
15
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I have over 100 sheets with different names. (ABC, CDE, ENT etc). I have a VBA which requires the sheet selection many times throughout the code. I need to repeat the same code for the next sheet. Rather than me having to rename all the Sheets on my VBA code, is there a way were I can define that name and just change it once at the beginning of my statement and the Sheets to pick up that name and update my VBA code?

Many thanks in advance


VBA Code:
Sub Sheet1 ()


''
Sheets("ABC").Select
Sheets("ABC").Select
Sheets("ABC").Select
Sheets("ABC").Select

''
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If I am understanding correctly, you need to loop through each sheet and perform the same action on each sheet. If this is correct then the following should work


VBA Code:
Sub Test()
Dim ws As Worksheet
For Each ws In Worksheets
'...Your action code here
Next ws
End Sub
 
Upvote 0
If I am understanding correctly, you need to loop through each sheet and perform the same action on each sheet. If this is correct then the following should work


VBA Code:
Sub Test()
Dim ws As Worksheet
For Each ws In Worksheets
'...Your action code here
Next ws
End Sub

Thank you for the prompt response. Part of the action code is to select the named sheet.
For example, the first one will have some filters specifically for ABC sheet to paste the data there. The next code, CDE will have different filters and paste the data to CDE etc.
 
Upvote 0
If I am understanding correctly, you need to loop through each sheet and perform the same action on each sheet. If this is correct then the following should work


VBA Code:
Sub Test()
Dim ws As Worksheet
For Each ws In Worksheets
'...Your action code here
Next ws
End Sub
Is not a loop. Let me try to re-explain it.

Sheet Name : Blue
Sheet Name: Red
Sheet Name: Green
Sheet Name: Data

I have a vba that looks in the SheetName Data, filters for some data with the string BLUE, copies it, goes to sheet BLUE and pastes it.
I need to do the same for the RED, filter for RED, go to Sheet Named RED and paste the data.

Many thanks in advance
 
Upvote 0
Is this what you mean
VBA Code:
Dim wsRed As Worksheet, wsBlue As Worksheet, wsGreen As Worksheet

Set wsRed = Sheets("Red")
Set wsBlue = Sheets("Blue")
Set wsGreen = Sheets("Green")
 
Upvote 0
Is this what you mean
VBA Code:
Dim wsRed As Worksheet, wsBlue As Worksheet, wsGreen As Worksheet

Set wsRed = Sheets("Red")
Set wsBlue = Sheets("Blue")
Set wsGreen = Sheets("Green")
Hi

Thank you for your reply. In my code I have 3 time "Sheets("Red"). Select" . If I want to copy and paste the same code to a new Module, I will change the red to a different name say Blue. I will need to manually change all the "Sheets("Red"). Select" to "Sheets("Blue"). Select" . Rather than doing that is it possible to do the following:

Set X as Name
X= "RED" (The only value I should have to change)

And where I have "Sheets("Red").Select" to be replaced with "Sheets("X").Select"



VBA Code:
Sub TestCopy_20()
Dim RedFilter As String

''
''
RedFilter = "Red"

' Unprotect destination sheet
Sheets("Red").Select
    ActiveSheet.Unprotect

Sheets("Data").Select
''
ActiveSheet.Range("B1:AD1").AutoFilter
ActiveSheet.Range("B1:AD100").AutoFilter Field:=3, Criteria1:=RedFilter
''
'Copy filtered table and paste it in Destination cell.
ActiveSheet.Range("B2:AD100").SpecialCells(xlCellTypeVisible).Copy
Sheets("Red").Select
ActiveSheet.Range("B3").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
''
'Remove filter that was applied.
ActiveSheet.AutoFilterMode = False
Range("B2").Select
Sheets("Red").Select
    ActiveSheet.Protect

''
End Sub
 
Upvote 0
How about an input box that changes the sheet name?


VBA Code:
Sub Test()
Dim ws As Worksheet
Set ws = InputBox("What Sheet to Name?")
'use ws to identify your sheet with your actions
'ie.  with ws
' Your code for the worksheet

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top