Macro to open existing workbook, run macro in the opened workbook, close it, repeat with next workbook

Chetter

New Member
Joined
Sep 1, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have 8 individual xlsm files. When run separately each one imports a csv file, formats it, then exports 2 csv files, each with their own variations. I'm thinking instead of opening each file, running a macro in that file, closing it without saving then moving on to the next. It would be quicker to create one main workbook that has a macro to cycle through each of the 8 files. I tried using this code:

VBA Code:
Workbooks.Open Filename:=ThisWorkbook.Path & "\BOOK123 USE THIS ONE 2099.xlsm"
Application.Run ("'BOOK123 USE THIS ONE 2099.xlsm'!CopyPasteSave2099")

and just changing the file name and macro name for each file and repeating the code but the macro closes itself after the first two lines of code (shown above).

I read through this forum often so thought I'd join and post to get your thoughts. I figured there has to be an easier, simpler way of accomplishing this. Thank you.
 
Try adding this line after that one and see what it returns:
VBA Code:
MsgBox mcrName
and verify it matches exactly the previous manual line that worked successfully.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
OK, I think I see the issue.

I think this line:
VBA Code:
If Right(strFile, 7) = "BOOK123" Then
should be:
VBA Code:
If Right(strFile, 12) = "BOOK123.xlsm" Then
since it has the extension on the end of it!
 
Upvote 0
Here's what I have but nothing runs, weird. It was running all the way to the end before, when it was looking for a macro that doesn't exist in this workbook.

VBA Code:
Option Explicit
Sub UJXlsmFiles()

    Dim strPath As String
    Dim strFile As String
    Dim wbSource As Workbook
    Dim wsSource As Worksheet
    Dim mcrName As String
  
   '============================
   'EDIT THE PATH TO THE FOLDER
   '============================
    strPath = "C:\Users\R616026\OneDrive\Documents\Weekly Pricing Macro"
'    strPath = ThisWorkbook.Path
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
  
   'get the first file
    strFile = Dir(strPath & "*.xlsm")
    
   'loop through the excel files in the folder
    Do While strFile <> ""

        If Right(strFile, 12) = "BOOK123.xlsm" Then
'        If InStr(strFile, "USE THIS ONE") > 0 Then
        
                'open the workbook
                Set wbSource = Workbooks.Open(strPath & strFile)
        
                mcrName = "'" & wbSource.Name & "'" & "!CopyPasteSave"
                Application.Run (mcrName)
                
                MsgBox mcrName
                                
                'close the opened workbook
'                wbSource.Close SaveChanges:=False
            End If
'      End If

'mcrName = "'" & ActiveWorkbook.Name & "'" & "!CopyPasteSave"
'Application.Run (mcrName)
      
      'get the next file
      strFile = Dir()
      
'      wbSource.Close SaveChanges:=False
      
    Loop
  
   'tidy up
    Set wsSource = Nothing
    Set wbSource = Nothing
      
End Sub
 
Upvote 0
Do you know how to Step into your code to run it one line at a time, line-by-line, using the F8 key?
If you do this, you can see exactly what it is doing every step of the way.
You can see if it is entering loops, satisfying/entering "If" clauses, etc.
Also, if you hover over any variable (such as "strFile), it will show you the exact value of that variable at that point in time.
And, you can see what is happening on the Excel files as they happen.

This is a very valulable debugging technique that programmers use all the time.
I would highly recommend trying this.
Many times, the problems areas will become quite evident when you see this happening as the code runs one line at a time.
 
Upvote 0
Solution
I got it working perfectly. I stepped through and figured out this line was the problem:

VBA Code:
If Right(strFile, 12) = "BOOK123.xlsm" Then

I changed it to this:

VBA Code:
If InStr(strFile, "USE THIS ONE") > 0 Then

Thank you for everything!
 
Upvote 0
You are welcome.
Glad you got it working!
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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