2013-10-30 66 views
0

我有一個正確運行的VB腳本,但是在電子表格的循環中,它似乎等待當前電子表格的宏運行,然後再打開下一個電子表格並啓動宏。有沒有辦法一次運行它們,而不是說file2不得不等待文件1打開並運行?代碼如下。VBScript在多個電子表格上運行Excel宏

Dim xlApp 
    Dim xlBook 
    Dim f 
    Dim curfile 
    Dim username 
    Dim password 
    ' Prompt user for credentials 
    username = InputBox("Enter your username") 
    password = InputBox("Enter your password") 

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set xlApp = CreateObject("Excel.Application") 
    xlApp.DisplayAlerts = False 
    For f = 1 to 3 
    Set xlBook = xlApp.Workbooks.Open(fso.GetParentFolderName(WScript.ScriptFullName) & "\full_volume" & f & ".xlsm", 0, True) 
    ' Paste in credentials to current spreadsheet 
    xlBook.Sheets("Main").Cells(2,5).Value = username 
    xlBook.Sheets("Main").Cells(3,5).Value = password 
    xlApp.Application.Visible = True 
    xlApp.Run "batch_calc" 
    ' Close Excel File 
    ' xlApp.Quit 
    Set xlBook = Nothing 
    Next 
    Set xlApp = Nothing 
    WScript.Echo f & " Batch Files Finished" 
    WScript.Quit 

回答

0

VBScript不支持並行執行。作爲一種解決方法,您可以修改VBScript,以便將文件名(也可能是憑據)作爲參數,然後使用批處理腳本並行啓動腳本:

@echo off 

setlocal 

set /p "username=Enter your username for TBA: " 
set /p "password=Enter your password for TBA: " 

for /L %%i in (1,1,3) do (
    start "" cscript.exe //NoLogo script.vbs "%~dp0ah_full_volume%%i.xlsm" "%username%" "%password%" 
) 
相關問題