Use Current Workbook

mlindquist

New Member
Joined
Sep 6, 2019
Messages
24
What is the correct way to write the VBA to have myWorkbook be equal to the current active workbook instead of a user entering a name. The user will be required to be on the workbook to do all the steps that are coming up. I thought of instead of having them entering the workbook name we could have it use the active workbook. Everything I tried isn't working.

Code:
Dim myWorkbook as Workbook: set myWorkbook = ThisWorkbook

ALSO TRIED

Dim myWorkbook as Workbook: set myWorkbook = ActiveWorkbook

Here is my original code

VBA Code:
Sub CopySheets()

myWorkbook = InputBox("Enter Workbook Name to Copy Worksheet onto - ex. 06-2022HireRightInvoice - do not include .xlsx")

ActiveWorkbook.CheckCompatibility = False
 
Workbooks("HirerightInvoice_SplitMacro").Sheets("Confirmation Form").Copy _
    After:=Workbooks(myWorkbook & ".xlsx").Sheets(Workbooks(myWorkbook & ".xlsx").Sheets.Count)
        
Workbooks("HirerightInvoice_SplitMacro").Sheets("Dispute Reasons").Copy _
    After:=Workbooks(myWorkbook & ".xlsx").Sheets(Workbooks(myWorkbook & ".xlsx").Sheets.Count)

Sheets("Confirmation Form").Select

Range("C18:C29").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=DisputeReasons"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This should work
VBA Code:
Dim myWorkbook As Workbook: Set myWorkbook = ActiveWorkbook
In what way isn't it working for you?
 
Upvote 0
Both those lines should work.
What makes you think that they don't work?

Note that once you do that, it is a Workbook variable, and not a string anymore, so you wouldn't reference it like a string, i.e.
this whole reference:
VBA Code:
Workbooks(myWorkbook & ".xlsx")
would be replaced by this:
VBA Code:
myWorkbook
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,019
Members
449,060
Latest member
LinusJE

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