Macro recording: replace specific sheet name into active sheet

valmir

Active Member
Joined
Feb 10, 2021
Messages
267
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,
I have this macro recording:
Sub CondFormat_F5_P48()
'
' CondFormat_F5_P48 Macro
'

'
Range("B1").Select
Windows("PERSONAL.XLSB").Activate
Range("F5:P48").Select
Selection.Copy
Windows("Stats 1999.xlsx").Activate
Range("F5").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("H48,J48,L48,N48,P48").Select
Range("P48").Activate
Selection.NumberFormat = "-#"
Range("B1").Select
End Sub

Everytime I want to use it, I have to change the specific sheet name (in this case "Stats 1999") into whatever sheet name I'm using.
So, how can I make it work with whatever sheet is active at the moment?
Thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I would be careful with this being in your personal macro workbook and referring to activesheet, if you inadvertently trigger the code on a workbook it isn't intended for then you may mess up your work.

WIth that caveat out the way, try:

VBA Code:
Sub CondFormat_F5_P48()
'
' CondFormat_F5_P48 Macro
'

'
Workbooks("PERSONAL.XLSB").Sheets(1).Range("F5:P48").Copy
With ActiveSheet
    .Range("F5:P48").PasteSpecial Paste:=xlPasteFormats
    .Range("H48,J48,L48,N48,P48").NumberFormat = "-#"
    .Range("B1").Select
End With
End Sub
 
Upvote 0
Solution
I would be careful with this being in your personal macro workbook and referring to activesheet, if you inadvertently trigger the code on a workbook it isn't intended for then you may mess up your work.

WIth that caveat out the way, try:

VBA Code:
Sub CondFormat_F5_P48()
'
' CondFormat_F5_P48 Macro
'

'
Workbooks("PERSONAL.XLSB").Sheets(1).Range("F5:P48").Copy
With ActiveSheet
    .Range("F5:P48").PasteSpecial Paste:=xlPasteFormats
    .Range("H48,J48,L48,N48,P48").NumberFormat = "-#"
    .Range("B1").Select
End With
End Sub
First of all, thanks for your prompt reply. And thanks for the warning, I am aware that I need to be careful otherwise I may mess up with whatever sheet is active at the moment!
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

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