从PowerShell传递variables到VBAmacros?

我有一个自动化一些报告处理PowerShell脚本,我有许多独立的macros来执行非常类似的自动过滤function,但在不同的标准。

是否有可能将这个标准从PowerShell传递到macros? 我可以只有1macros。

ColNum = Application.Match("*header", Range("A1:Z1"), 0) If Not IsError(ColNum) Then ActiveSheet.Range("A1").AutoFilter Field:=ColNum, Criteria1:="$criterafromPowerShell", Operator:=xlAnd End If 

我目前正在做类似的事情,但反过来,我插入到工作簿,并提取回PowerShell这些macros的输出如下所示:

 $counts = $workbook.worksheets.item(2) $xRows = $counts.cells.item(1,1).Text $yeRows = $counts.cells.item(1,2).Text 

我承认我可以这样做,反过来,并插入我打算在工作表中使用的文本之后,打开文件,然后macros运行,然后在macros里面拿起它…但似乎凌乱。

有什么build议么?

例:

 $xlApp = New-Object -ComObject "Excel.Application" $xlApp.Workbooks.Open("C:\Users\MacroMan\Documents\MyMacroWorkbook.xlsm") $returnValue = $xlApp.Run("'MyMacroWorkbook.xlsm'!GenerateString", 6) Echo $returnValue FOOBAR $returnValue = $xlApp.Run("'MyMacroWorkbook.xlsm'!GenerateString", 3) Echo $returnValue FOO 

在“MyMacroWorkbook”(VBA)中:

 Public Function GenerateString(strLength As Integer) As String GenerateString = Left("FOOBAR_SOMETHING", strLength) End Function 

我使用下面的代码来传递VBscript中的两个参数来打开特定的工作簿并启动所需的macros,您可以将想要的variables作为variables传递给子variables,即Sub test(passedvariablehere) ,并使子variables:

 'Create a WshShell to get the current directory Dim WshShell Set WshShell = CreateObject("WScript.Shell") If (Wscript.Arguments.Count < 2) Then Wscript.Quit End If 'retrieve the arguments Dim strWorkerWB strWorkerWB = Wscript.Arguments(0) Dim strMacroName strMacroName = Wscript.Arguments(1) ' Create an Excel instance Dim myExcelWorker Set myExcelWorker = CreateObject("Excel.Application") myExcelWorker.Application.Visible = True ' Open the Workbook specified on the command-line Dim oWorkBook Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB) on error resume next ' Run the calculation macro myExcelWorker.Run strMacroName if err.number <> 0 Then ' Error occurred - just close it down. oWorkBook.Close Set oWorkBook = Nothing myExcelWorker.Quit Wscript.Quit End If err.clear oWorkBook.Close Set oWorkBook = Nothing myExcelWorker.Quit Set myExcelWorker = Nothing Set WshShell = Nothing on error goto 0 

第一个参数是工作簿的完整path/名称,第二个参数是子名称