VBScript在多个电子表格上运行Excel宏

时间:2013-10-30 21:13:56

标签: excel-vba vbscript vba excel

我有一个正确运行的VB脚本,但是,在电子表格循环中,它似乎等待当前电子表格的宏运行,然后打开下一个电子表格并启动该宏。有没有办法一次性运行它们而不是说file2必须等待文件1打开并运行?代码如下。

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

1 个答案:

答案 0 :(得分: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%"
)
相关问题