running a macro in another workbook

nwille7400

New Member
Joined
Dec 16, 2019
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm trying to change several workbooks (target) in one folder using vba in a separate (source) workbook. All the target workbooks are pretty much the same, small differences. After I make my changes I need to run a macro that exists in the target workbook to format some other areas in the target workbook. I'm opening the target files in the folder one at a time and everything works fine until I try to run the macro. The macro is in Module 8 and is called 'Update'. It is used on Sheet1 in a button. Gives the error attached but when I just open one of the target workbooks and run the macro it works fine. Can someone help on this? Let me know if you need any info that I may have left out. In some of the target workbooks the button has been removed and I would like to know how to detect if the button itself is still showing in sheet1. Thanks


Sub Button1_Click()
'this sets your template workbook/worksheet
Dim copyWB As Workbook
Set copyWB = ThisWorkbook
'open insp report to copy from
file_name = "new inspection report_Rev E beta 7.xls"
Set myTextFile = Workbooks.Open("X:\Inspection Reports\test\Beta version\" & file_name)
'this creates a collection of all filenames to be processed

Dim loopFolder As String
Dim fileNm As Variant
Dim myFiles As New Collection


'don't forget the backslash before the final double-quote below
loopFolder = ThisWorkbook.Sheets("Sheet1").Cells(3, 4) 'this is where the folder location is stated
fileNm = Dir(loopFolder & "*.xls")

Do While fileNm <> ""
myFiles.Add fileNm
fileNm = Dir
'MsgBox fileNm
Loop
Application.DisplayAlerts = False
'this loops through all filenames and copies your copyWS to the beginning
Dim wb As Workbook
insp_report_name = ""
For Each fileNm In myFiles
Set wb = Workbooks.Open(Filename:=(loopFolder & fileNm))
wb.Sheets("Input Sheet").Range("A5:I13").Copy _
myTextFile.Sheets("Input Sheet").Range("A5") 'copy input sheet data to new sheet
wb.Sheets("Input Sheet").Range("A15:I23").Copy _
myTextFile.Sheets("Input Sheet").Range("A16")
wb.Sheets("Input Sheet").Range("E30:E37").Copy _
myTextFile.Sheets("Input Sheet").Range("E31")

If wb.Sheets("Input Sheet").Checkbox8 = True Then
myTextFile.Sheets("Input Sheet").Checkbox8 = True
Else
myTextFile.Sheets("Input Sheet").Checkbox8 = False
myTextFile.Sheets("Input Sheet").Checkbox9 = True
myTextFile.Activate
Application.Run "CheckBox9_Click"
End If

insp_report_name = wb.Name 'record name of specific insp report
wb.Close 'close specific insp report
Kill loopFolder & insp_report_name 'delete specific insp report
myTextFile.SaveAs Filename:=loopFolder & insp_report_name 'save new inspection report as specific insp report name
Sheets("Insp. Sheet Final").Activate
Next
Application.DisplayAlerts = True
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    20.9 KB · Views: 6

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Application.Run "CheckBox9_Click"
Use workbook variable name to run correct macro
VBA Code:
Sub RunFromOtherWB()
    Dim wb As Workbook
    Set wb = Workbooks("2018.xls")
    Application.Run ("'" & wb.Name & "'!CheckBox2_Click")
End Sub
 
Upvote 0
Thanks Davesexcel, that works well with a macro that I have that is made with formcontrol and in a module. I also have one that is made with active x and is in sheet1 code. It gives me the error code attached when I try to run that code.

Also, any thoughts on this part of my request?
'In some of the target workbooks the button has been removed and I would like to know how to detect if the button itself is still showing in sheet1.'
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

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