Run Code on Inserted Worksheet

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
I have code which needs to be executed when a user copies a worksheet into the workbook where the code exists.

I can include a shape and apply the code/macro to, however how would the reference to the sheet be. The sheets copied into the workbook all have different names.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
No I have not. I'll try that option.

Actually I copied the code to the personal workbook.

Thank you for the response.
 
Upvote 0
In general unless you have specified the workbook then if you have it in the personal workbook it will refer to the personal workbook.

Please post your full code.
 
Upvote 0
This is the code

Sub MergeColumns()


Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range



Application.ScreenUpdating = False


lr = Cells(Rows.Count, 6).End(xlUp).Row


For i = 9 To lr

Set rng1 = Range(Cells(i, 6), Cells(i + 1, 7))
With rng1
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True
End With


Set rng2 = Range(Cells(i, 9), Cells(i + 1, 10))
With rng2
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True
End With

Set rng3 = Range(Cells(i, 11), Cells(i + 1, 13))
With rng3
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True
End With


Set rng4 = Range(Cells(i, 4), Cells(i + 1, 5))
With rng4
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True
End With

Next

Cells(1, 1).Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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