2

Could someone please tell me why the following VB script works fine if executed from within excel but won't work if it executed using the cmd: cscript c:\vb\test.vbs?. Below is my code I'm trying to get it working using the cmd. I'm using excel .xls (excel 97-2003).

    Private Sub CopyData()
    Dim x
    Dim y

    '## Open both workbooks first:
    Set x = Workbooks.Open("C:\VB\CopyDataTest.xls")

    'Now, copy what you want from x:
    Sheets("Sheet1").Range("A:B").Copy
    Set y = Workbooks.Open("C:\VB\Destination.xls")

    'Now, paste to y worksheet:
    y.Sheets("Sheet2").Range("A:B").PasteSpecial

    'Close x:
      y.Close
End Sub
4
  • how can Excel run VBS? It only supports VBA Commented Jul 25, 2016 at 11:07
  • Ok - maybe I'm wrong but the file name is "test.vbs" and I need to execute it using the cmd? by typing cscript c:\vb\test.vbs. Commented Jul 25, 2016 at 11:10
  • Because this is not VBS, obviously cscript can't execute it. The extension is only for Windows to display the icon and choose which app to open the file. Programs don't use extensions to know filetype. This is VBA so running from Excel will work Commented Jul 25, 2016 at 11:15
  • Ok - thanks for this but I've seen this happening where .txt files are saved as .vbs then executed using the cmd?. My intention of this is that I eventually would like to execute this script using selenium webdriver. Commented Jul 25, 2016 at 11:33

2 Answers 2

2

If you need to run the script from cmd, you need to create an excel object. Try this:

Private Sub CopyData()
    Dim x
    Dim y

    Set xlApp = CreateObject("Excel.Application") 
    Set xlBook = xlApp.Workbooks.Open("C:\VB\Destination.xls", 0, True) 

    ## Open both workbooks first:
    Set x = xlApp.Workbooks.Open("C:\VB\Destination.xls")

    Now, copy what you want from x:
    xlApp.Sheets("Sheet1").Range("A:B").Copy
    Set y = xlApp.Workbooks.Open("C:\VB\Destination.xls")

    Now, paste to y worksheet:
    y.Sheets("Sheet2").Range("A:B").PasteSpecial

    Close x:
    y.Close
End Sub

CopyData()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks the code is doing what I wanted but the only thing is that once the date copied to the destination workbook successfully, the source workbook (i.e. X) is getting locked for some reason if I try to open it. Any idea how to avoid the source workbook from getting locked?. Thanks.
@Mnimonic, The code above works fine but I need to copy more columns ( A, B, C, D, E, F, I) to destination sheet . If I try to copy 3 columns or more im getting the error: 800A03EC ( copied from comment on my answer...)
2

Mnimonic already gave an perfectly usable workaround for this, but an explanation could be usefull too.

You have written a piece of code in VBA (Visual Basic for Applications).

You tried to run it as VBS (VB Script)

VB script doesn't know about Office and other libraries already loaded when running the code inside excel.

You'll need to learn how to interact with the COM interfaces from office in VBscript.

Better solution now would be to program in VB.NET and interact with excel inside .NET:

Link: VB.NET and excell

The code will still look very familiar, but it's what Microsoft would like you to do now.

Attention: You will always need to have Excel installed on the PC running the script! If you want to avoid that, maybe look at something like Aspose to do things without Office installed...

1 Comment

@Mnimonic, The code above works fine but I need to copy more columns ( A, B, C, D, E, F, I) to destination sheet . If I try to copy 3 columns or more im getting the error: 800A03EC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.