Home

Running macro code externally with vbs and batch file

%3CLINGO-SUB%20id%3D%22lingo-sub-819414%22%20slang%3D%22en-US%22%3ERunning%20macro%20code%20externally%20with%20vbs%20and%20batch%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-819414%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20macro%20that%20copies%20data%20from%20multiple%20workbooks%20into%20a%20Master%20wkbook.%20I%20am%20only%20able%20to%20do%20this%20by%20running%20the%20macro%20from%20the%20Master%20wkbk.%20However%20I%20want%20to%20be%20able%20to%20run%20this%20code%20externally%20(outside%20of%20the%20excel%20sheet).%3C%2FP%3E%3CP%3EI%20have%20tried%20using%20a%20vb%20script%20but%20it%20tells%20me%20I%20dont%20have%20the%20admin%20rights%20to%20run%20it%20(I%20dont%20have%20rights).%20I%20also%20found%20somewhere%20where%20I%20could%20use%20a%20batch%20file%20to%20run%20the%20vbs%20but%20unfortunately%20it%20doesnt%20work.%20Here%20is%20my%20macro%20code%2C%20the%20VB%20script%20and%20the%20bat%20file%20intended%20to%20run%20the%20code%20externally.%3C%2FP%3E%3CP%3EThe%20VB%20code%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ESub%20CopyRange()%0A%20%20%20%20Application.DisplayAlerts%20%3D%20False%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Dim%20wkbDest%20As%20Workbook%0A%20%20%20%20Dim%20wkbSource%20As%20Workbook%0A%20%20%20%20Set%20wkbDest%20%3D%20ThisWorkbook%0A%20%20%20%20Dim%20LastRow%20As%20Long%0A%20%20%20%20Const%20strPath%20As%20String%20%3D%20%22C%3A%5CUsers%5CG852589%5Ccnq%20transfer%5C%22%0A%20%20%20%20ChDir%20strPath%0A%20%20%20%20strExtension%20%3D%20Dir(%22*.xlsx*%22)%0A%20%20%20%20Do%20While%20strExtension%20%26lt%3B%26gt%3B%20%22%22%0A%20%20%20%20%20%20%20%20Set%20wkbSource%20%3D%20Workbooks.Open(strPath%20%26amp%3B%20strExtension)%0A%20%20%20%20%20%20%20%20With%20wkbSource%0A%20%20%20%20%20%20%20%20%20%20%20%20LastRow%20%3D%20.Sheets(%22Sheet1%22).Cells.Find(%22*%22%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlPrevious).Row%0A%20%20%20%20%20%20%20%20%20%20%20%20.Sheets(%22Sheet1%22).Range(%22A5%3AGJ%22%20%26amp%3B%20LastRow).Copy%20wkbDest.Sheets(%22Sheet1%22).Cells(Rows.Count%2C%20%22A%22).End(xlUp).Offset(1%2C%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20.Close%20savechanges%3A%3DFalse%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20strExtension%20%3D%20Dir%0A%20%20%20%20Loop%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThe%20VB%20script%20I%20have%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EDim%20args%2C%20objExcel%0A%0ASet%20args%20%3D%20WScript.Arguments%0ASet%20objExcel%20%3D%20CreateObject(%22Excel.Application%22)%0A%0AobjExcel.Workbooks.Open%20args(0)%0AobjExcel.Visible%3D%20True%0A%0AobjExcel.Run%20%22OpenRange%22%0A%0AobjExcel.ActiveWorkbook.Save%0AobjExcel.ActiveWorkbook.Close(0)%0AobjExcel.Quit%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EAnd%20finally%20the%20bat%20file%20i%20intend%20to%20run%20the%20vbs%20with%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Ecscript%20script.vbs%20%22C%3A%5CUsers%5CG852589%5Ccnq%20transfer%5CResult.xlsm%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CSPAN%3EResult.xlsm%20is%20the%20Master%20workbook%20where%20the%20macro%20code%20is%20seated.%20The%20bat%20file%20does%20nothing%20after%20running%20so%20I%20would%20appreciate%20if%20someone%20can%20point%20out%20where%20my%20code%20is%20wrong%20or%20what%20I%20am%20not%20doing%20right%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-819414%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-819602%22%20slang%3D%22en-US%22%3ERe%3A%20Running%20macro%20code%20externally%20with%20vbs%20and%20batch%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-819602%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3ECan%20you%20please%20take%20a%20look%20at%20my%20issue%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-819955%22%20slang%3D%22en-US%22%3ERe%3A%20Running%20macro%20code%20externally%20with%20vbs%20and%20batch%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-819955%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F371611%22%20target%3D%22_blank%22%3E%40Fatman003%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20never%20tried%20to%20run%20macros%20outside%20Excel%20before!%3C%2FP%3E%3CP%3EBut%20after%20searching%20the%20web%20I%20found%20a%20good%20way%20to%20do%20so%20in%20this%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F10232150%2Frun-excel-macro-from-outside-excel-using-vbscript-from-command-line%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EStackOverflow%20question%3C%2FA%3E.%3C%2FP%3E%3CP%3EI've%20tried%20the%20solution%20in%20the%20last%20reply%2C%20and%20it%20works%20just%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3C%2FLINGO-BODY%3E
Fatman003
Occasional Contributor

I have a macro that copies data from multiple workbooks into a Master wkbook. I am only able to do this by running the macro from the Master wkbk. However I want to be able to run this code externally (outside of the excel sheet).

I have tried using a vb script but it tells me I dont have the admin rights to run it (I dont have rights). I also found somewhere where I could use a batch file to run the vbs but unfortunately it doesnt work. Here is my macro code, the VB script and the bat file intended to run the code externally.

The VB code:

Sub CopyRange()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Const strPath As String = "C:\Users\G852589\cnq transfer\"
    ChDir strPath
    strExtension = Dir("*.xlsx*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("Sheet1").Range("A5:GJ" & LastRow).Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

The VB script I have:

Dim args, objExcel

Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Open args(0)
objExcel.Visible= True

objExcel.Run "OpenRange"

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit

And finally the bat file i intend to run the vbs with:

cscript script.vbs "C:\Users\G852589\cnq transfer\Result.xlsm"

Result.xlsm is the Master workbook where the macro code is seated. The bat file does nothing after running so I would appreciate if someone can point out where my code is wrong or what I am not doing right

 

2 Replies
@Haytham Amairah Can you please take a look at my issue

@Fatman003

 

Hi,

 

I've never tried to run macros outside Excel before!

But after searching the web I found a good way to do so in this StackOverflow question.

I've tried the solution in the last reply, and it works just fine.

 

Regards

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies