Amalgamating 2 macros gives Compiler error.

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
136
Office Version
  1. 2013
Platform
  1. Windows
I have 2 macros that I am trying to amalgamate.

Macro1 uses an InputBox to get data from the User and stores the answer in a specific sheet in the Template that is opened to run the macro. The code for macro 1 is:-

VBA Code:
Dim myYear As Integer
myYear = InputBox("Please enter the year (Format YYYY) that you want to create monthly Sheets for", "Enter Year")
Sheets("Formula").Select
Range("R2").Value = myYear
ActiveWorkbook.Save

Macro 2 starts off with the statements:-

VBA Code:
Dim ws As Worksheet
fPath = ThisWorkbook.Path & "\"

It then proceeds to use the following command:-

VBA Code:
ActiveWorkbook.SaveAs Filename:=fPath & "01 January.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

to save 12 workbooks with the names 01 January, 02 February, etc etc etc.

I am attempt to amalgamate the 2 macros, I get the error “Compiler error: Variable not found” against the line fPath = ThisWorkbook.Path & "\".

When I run the macros individually or call macro 2 from macro 1, then no problems are encountered.

Any assistance offered would be gratefully received.
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,387
Office Version
  1. 2019
Platform
  1. Windows
Hi,
Declare the missing variable & see if resolves issue

Rich (BB code):
Dim ws As Worksheet
Dim fPath As String

fPath = ThisWorkbook.Path & "\"

Dave
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,123,252
Messages
5,600,543
Members
414,387
Latest member
Vincent88

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
Top