How to Run Module without being in an Active Sheet using VBA

Santude

New Member
Joined
Jul 28, 2012
Messages
18
Currently writing a Quote application for my company. I am almost done and came across a hiccup. In order to run the Quote I have to be in the Active Sheet. I am doing copypicture on range Sheet3.Range(B1:H14) and the pasting it to Cell Sheet3.Range("R1"). I Then name the copied picture "HeaderTemplet". I am able to copy and paste the picture, but I am not able to name it without being in the Active Sheet (Sheet3).

So, in a nutshell, I need to execute my code/module from Sheet1...

With Sheet3

For Each HeadShp In .Shapes
If InStr(HeadShp.Name, "Header") > 0 Then HeadShp.Delete
Next HeadShp
'Create Duplicate/Linked header
.Range("B1:H13").CopyPicture
.Range("R1").PasteSpecial

''''''Here is where I am running into the problem'''''''

With Selection
.Formula = "$B$1:$H$13"
.Name = "HeaderTemplate"
End With

Thank you for your help!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What is Selected when this code is run?
 
Upvote 0
How about temporarily activating Sheet1 and after the code has run restoring it?

VBA Code:
' variable declaration section
Dim myActiveSheet as String

' place before the error occurs
myActiveSheet = ActiveSheet.Name

ThisWorkbook.Sheets("Sheet1").Activate

' your code to run

ThisWorkbook.Sheets(myActiveSheet).Activate
 
Upvote 0
How about temporarily activating Sheet1 and after the code has run restoring it?

VBA Code:
' variable declaration section
Dim myActiveSheet as String

' place before the error occurs
myActiveSheet = ActiveSheet.Name

ThisWorkbook.Sheets("Sheet1").Activate

' your code to run

ThisWorkbook.Sheets(myActiveSheet).Activate
Thank you for the replay Peter! That is an option, however I am being asked to hide all tabs. I can go through the process of hiding and unhiding, but then I run into eating up a ton of memory.
 
Upvote 0
What is Selected when this code is run?
I am creating a picture of the top page - Quote Header - Calling this HeaderTemplet

1693788212835.png
 

Attachments

  • 1693788065243.png
    1693788065243.png
    63.3 KB · Views: 4
Upvote 0
Can you answer the question by @Skyybot in post 2
What is Selected when this code is run?

because by the code you have posted it gives the impression that you are putting the value "$B$1:$H$13" in the cell then naming the cell rather than the picture
 
Upvote 0
Can you answer the question by @Skyybot in post 2


because by the code you have posted it gives the impression that you are putting the value "$B$1:$H$13" in the cell then naming the cell rather than the picture
Hello Mark,
It is the Value(Location) for the shape in the Name Manager within Formula's
1693791956792.png
 
Upvote 0
Hello Mark,
It is the Value(Location) for the shape in the Name Manager within Formula's
View attachment 98206
I'm referencing the Range "$B$1:$H$13" , as this is the selected range that I am doing a "CopyPicture" on. I am then Naming it "HeaderTemplet"

With Selection
.Formula = "$B$1:$H$13"
.Name = "HeaderTemplate"
End With
 
Upvote 0
"$B$1:$H$13" isn't a value for the shape, it is a value in the cell L24 in the sheet "Main" according to the Name Manager
 
Last edited:
Upvote 0
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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