Make VBA code standalone executable?

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
I have some VBA code which I would really like to run after I close excel.

I was wondering if there was a way to paste this into a text file and save as a vb filetype, then I could just run it whenever I wanted to.

I believe the method I am using to copy the files is not dependant on the excel application...

The code simply backs up my toolbar and personal macro workbook. But, the toolbar file (excel11.xlb) doesnt get updated until you close excel.

Currently, I have to close excel, then open it back up, then run this macro to get the most up to date files.

So, how would I go about making this code executable outside of the excel application.
Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub backupWorkspaceFiles()
    '''''''''''''''''''''''''
    Dim PERSONAL                    As Object
    Dim ExceL11                     As Object
    Dim strDate                     As String
    Dim strPersDir                  As String
    Dim strExcDir                   As String
    Dim strNewDir                   As String
    Dim strNewPers                  As String
    Dim strNewExc                   As String
    '''''''''''''''''''''''''
    strDate = Format((Date), "YYYY-MM-DD")
    strPersDir = "C:\Documents and Settings\MYuserNAME\Application Data\Microsoft\Excel\XLSTART"
    strExcDir = "C:\Documents and Settings\MYuserNAME\Application Data\Microsoft\Excel\"
    strNewDir = "\\MYDIR\MY FILES\EXCEL WORKSPACE FILES"
    strNewPers = "(" & strDate & ")" & "PERSONAL.xls"
    strNewExc = "(" & strDate & ")" & "Excel11.xlb"
    Set PERSONAL = CreateObject("Scripting.FileSystemObject")
    Set ExceL11 = CreateObject("Scripting.FileSystemObject")
    '''''''''''''''''''''''''
    'object.copyfile,source,destination,file overright(True is default)
    PERSONAL.CopyFile strPersDir & "\" & "PERSONAL.xls", strNewDir & "\" & strNewPers, True
    ExceL11.CopyFile strExcDir & "\" & "Excel11.xlb", strNewDir & "\" & strNewExc, True
    Set PERSONAL = Nothing
    Set ExceL11 = Nothing
    '''''''''''''''''''''''''
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
OK, so I did some digging, and found out I could save as filetype .vbs.

I'm already using late binding to create my objects, so I thought it might work.

I dbl-clicked the .vbs file and got a compiler error...

I then dug some more and I read that variables should not be declared as types in vbs files, so I commented out the "As Type" lines.

I dbl-clicked my file and this time it ran. BUT...the new files were not created.

Anyone have any suggestions?

So, my modified code looks like:
Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub backupWorkspaceFiles()
    '''''''''''''''''''''''''
    Dim PERSONAL                    'As Object
    Dim ExceL11                     'As Object
    Dim strDate                     'As String
    Dim strPersDir                  'As String
    Dim strExcDir                   'As String
    Dim strNewDir                   'As String
    Dim strNewPers                  'As String
    Dim strNewExc                   'As String
    '''''''''''''''''''''''''
    strDate = Format((Date), "YYYY-MM-DD")
    strPersDir = "C:\Documents and Settings\MYuserName\Application Data\Microsoft\Excel\XLSTART"
    strExcDir = "C:\Documents and Settings\MYuserName\Application Data\Microsoft\Excel\"
    strNewDir = "\\MYDIR\ MY FILES\EXCEL WORKSPACE FILES"
    strNewPers = "(" & strDate & ")" & "PERSONAL.xls"
    strNewExc = "(" & strDate & ")" & "Excel11.xlb"
    Set PERSONAL = CreateObject("Scripting.FileSystemObject")
    Set ExceL11 = CreateObject("Scripting.FileSystemObject")
    '''''''''''''''''''''''''
    'object.copyfile,source,destination,file overright(True is default)
    PERSONAL.CopyFile strPersDir & "\" & "PERSONAL.xls", strNewDir & "\" & strNewPers, True
    ExceL11.CopyFile strExcDir & "\" & "Excel11.xlb", strNewDir & "\" & strNewExc, True
    Set PERSONAL = Nothing
    Set ExceL11 = Nothing
    '''''''''''''''''''''''''
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
Upvote 0
Try this...

Code:
    Set PERSONAL = [COLOR="Red"]WScript.[/COLOR]CreateObject("Scripting.FileSystemObject")
    Set ExceL11 = [COLOR="Red"]WScript.[/COLOR]CreateObject("Scripting.FileSystemObject")

BTW: I have no idea what I'm doing when it comes to VBS. So if it helped, great. I doubt I could answer anything additional though.
 
Upvote 0
Also discovered that Format is not a VBS function. There isn't a built-in equivalent for it. Found this snippet to mimic strDate = Format((Date), "YYYY-MM-DD")

Code:
    Dim PERSONAL                    'As Object
    Dim ExceL11                     'As Object
    Dim strDate                     'As String
    Dim strPersDir                  'As String
    Dim strExcDir                   'As String
    Dim strNewDir                   'As String
    Dim strNewPers                  'As String
    Dim strNewExc                   'As String
        
    '''''''''''''''''''''''''
    strDate = [COLOR="Red"]YEAR(Date()) & _ 
        "-" & Pd(Month(date()),2) & _ 
        "-" & Pd(DAY(date()),2) [/COLOR]
    strPersDir = "C:\Documents and Settings\MYuserName\Application Data\Microsoft\Excel\XLSTART"
    strExcDir = "C:\Documents and Settings\MYuserName\Application Data\Microsoft\Excel\"
    strNewDir = "\\MYDIR\ MY FILES\EXCEL WORKSPACE FILES"
    strNewPers = "(" & strDate & ")" & "PERSONAL.xls"
    strNewExc = "(" & strDate & ")" & "Excel11.xlb"
    Set PERSONAL = [COLOR="Red"]WScript.[/COLOR]CreateObject("Scripting.FileSystemObject")
    Set ExceL11 = [COLOR="Red"]WScript.[/COLOR]CreateObject("Scripting.FileSystemObject")
    '''''''''''''''''''''''''
    'object.copyfile,source,destination,file overright(True is default)
    PERSONAL.CopyFile strPersDir & "\" & "PERSONAL.xls", strNewDir & "\" & strNewPers, True
    ExceL11.CopyFile strExcDir & "\" & "Excel11.xlb", strNewDir & "\" & strNewExc, True
    Set PERSONAL = Nothing
    Set ExceL11 = Nothing
    '''''''''''''''''''''''''

    [COLOR="Red"]Function pd(n, totalDigits) 
        if totalDigits > len(n) then 
            pd = String(totalDigits-len(n),"0") & n 
        else 
            pd = n 
        end if 
    End Function[/COLOR]
 
Upvote 0
Thank you for all the responses guys. I am going to try them out now.
 
Last edited:
Upvote 0
Hmmm...I made changes, but I'm still not sure whats wrong. The script runs with no errors but still is not copying any files...
Code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub backupWorkspaceFiles()
    '''''''''''''''''''''''''
    Dim PERSONAL                    'As Object
    Dim ExceL11                     'As Object
    Dim strDate                     'As String
    Dim strPersDir                  'As String
    Dim strExcDir                   'As String
    Dim strNewDir                   'As String
    Dim strNewPers                  'As String
    Dim strNewExc                   'As String
    '''''''''''''''''''''''''
    strDate = YEAR(Date()) & _ 
        "-" & fDate(Month(date()),2) & _ 
        "-" & fDate(DAY(date()),2)
    strPersDir = "C:\Documents and Settings\MYNAME\Application Data\Microsoft\Excel\XLSTART"
    strExcDir = "C:\Documents and Settings\MYNAME\Application Data\Microsoft\Excel\"
    strNewDir = "\\MYDIR\001 MY FILES\EXCEL WORKSPACE FILES"
    strNewPers = "(" & strDate & ")" & "PERSONAL.xls"
    strNewExc = "(" & strDate & ")" & "Excel11.xlb"
    Set PERSONAL = WScript.CreateObject("Scripting.FileSystemObject")
    Set ExceL11 = WScript.CreateObject("Scripting.FileSystemObject")
    '''''''''''''''''''''''''
    'object.copyfile,source,destination,file overright(True is default)
    PERSONAL.CopyFile strPersDir & "\" & "PERSONAL.xls", strNewDir & "\" & strNewPers, True
    ExceL11.CopyFile strExcDir & "\" & "Excel11.xlb", strNewDir & "\" & strNewExc, True
    Set PERSONAL = Nothing
    Set ExceL11 = Nothing
    '''''''''''''''''''''''''
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function fDate(n, totalDigits) 
    if totalDigits > len(n) then 
        fDate = String(totalDigits-len(n),"0") & n 
    else 
        fDate = n 
    end if 
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
Upvote 0
OK, so I got this working. YAY!

The solution was what Kenneth Hobson posted early on, I just somehow missed his post! Wow, thank you Kenneth!

From what I can understand, in VBScript you need to write code without any tags around it, and that code is what is executed when you open the .vbs file. At least thats how I percieve it.

Anyways, thank you to everyone who helped me with this!
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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