Moving macro to Personal.XLSB - Workbook Reference

skiskool

New Member
Joined
Aug 18, 2011
Messages
2
I have imported a macro from a workbook into my personal.xlsb file so that I can run across different workbooks and activate via the Quick Access Toolbar (QAT).

The macro uses data from a source worksheet (variable name) and copies/reformats it on a new target worksheet (in the same workbook), created within the macro.

This works fine when I have the macro stored in the workbook, but following the move to personal.xlsb, the target worksheet is created in the personal.xlsb workbook and cannot find the source worksheet (because its not in the personal.xlsb workbook).

How do I re-focus the macro to the workbook that was active when it's run from the QAT?

The relevant code is below (Excel 2007, Win 7):

Sub CreateTargetSheet()

' Sets the variables used in the macro
Dim wsNew As Worksheet
Dim wsSource As String
Dim NewSetName As String

' Captures source of the set being created
wsSource = ActiveSheet.Name

' Define name for new worksheet
NewSetName = Application.InputBox("Enter a name for the new worksheet")

' Checks for the existance of a sheet with that name then creates a new worksheet
If Not SheetExists(NewSetName) Then
Set wsNew = ThisWorkbook.Worksheets.Add
wsNew.Name = NewSetName

Else
Dim Msg, Style, Title, Response, MyString
Msg = "New Worksheet name already exists, do you wish to overwrite?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "New Worksheet" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then

' User chose Yes so delete existing worksheet and set up a new one
Sheets(NewSetName).Delete
Set wsNew = ThisWorkbook.Worksheets.Add
wsNew.Name = NewSetName
Else ' User chose No. so end macro
GoTo MyExit
End If
End If
' rest of macro code goes here

MyExit:
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
How about having:
Code:
Dim CurrWbk As Workbook
Set CurrWbk = ActiveWorkBook
and then changing all occurences of ThisWorkbook to CurrWbk
?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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