簡體   English   中英

Powershell在多個工作表中的excel工作簿中運行多個vba腳本

[英]Powershell Run Multiple vba scripts in excel workbook across multiple worksheets

嗨,我需要在同一工作簿中的不同工作表上運行不同的vba腳本。 基本上,每個工作表都有自己的vba腳本,該腳本會觸發ODBC連接,然后從數據庫更新工作表。 我已經能夠使一個vba腳本在一張紙上運行並另存為...沒問題,但是最多只能運行一個。 這是我正在使用的代碼

$excel = new-object -comobject excel.application
$excelFiles = Get-ChildItem -Path C:\test\Daily_update.xlsm
$Date = (Get-Date -Format dd-MM-yy)
Foreach($file in $excelFiles)
{
 $workbook = $excel.workbooks.open($file.fullname)
 $worksheet = $workbook.worksheets.item(2)
 $excel.Run("Test_Refresh")

 $workbook.saveAs("C:\test\Daily_update_$Date.xlsm")
 $workbook.close()
}
$excel.quit()

當我嘗試添加其他工作表和vba腳本時,它根本不起作用。

離開問題一會兒,再喝點咖啡,並運用一些邏輯后,可以。 我有事情要工作。 因此,以防萬一您需要一個腳本來執行我想做的事情,並在同一工作簿中的特定工作表上運行特定宏,就在這里。

$excel = new-object -comobject excel.application
$excelFiles = Get-ChildItem -Path C:\Test\Daily_update.xlsm
$Date = (Get-Date -Format dd-MM-yy)

 $workbook = $excel.workbooks.open($excelfiles.fullname)
 $WS2 = $workbook.worksheets.item(2)
 $WS2.Activate()
 $excel.Run("Test_Refresh")

 $WS3 = $workbook.worksheets.item(3)
 $WS3.Activate()
 $excel.Run("test_Refresh_2")

 $WS4 = $workbook.worksheets.item(4)
 $WS4.Activate()
 $excel.Run("test_Refresh_3")

 $workbook.saveas("C:\Test\SQL\Daily_update_$Date.xlsm")
 $workbook.close()

$excel.quit()

只是增加一點。 經過反復試驗,我發現下面的代碼比上面的代碼運行效率更高。 我還注意到,如果只有3個工作表就可以了,但是任何其他工作表都會引發錯誤,特別是在調用工作表時。 但是,當工作簿打開並可見時,該問題消失了。

#Call the application
$excel = new-object -comobject excel.application
#Now we select the file and path 
$excelFiles = Get-ChildItem -Path "\\Server\Test\Daily_refresh.xlsm"
#The next variable speeds the script up by not calling the comobject as often
$app = $excel.Application
#Get system date and time and format it to comply with the final filename format
$Date = (Get-Date -Format dd-MM-yy)
#And again for the year folder
$Year = (Get-Date -Format yyyy)
#Test if folder exists
$DestYearFolder = "\\Server\Test\Daily_Refresh_Output\Test\$Year"
if (!(Test-Path -path $DestYearFolder)) {New-Item $DestYearFolder -Type Directory}
#Same as above only for the month folder
$Month = (Get-Date -Format MMM)
#Test if folder exists
$DestMonthFolder = "\\Server\Test\Daily_Refresh_Output\Test\$Year\$Month"
if (!(Test-Path -path $DestMonthFolder)) {New-Item $DestMonthFolder -Type Directory}
#Now we open the Excel file and activate the macro enabled content
 $workbook = $app.workbooks.open($excelfiles)
 #The next command makes Excel visible
 $app.Visible = $true
 $workbook.Activate()
 #Now we run all the Macros that need to be run.
 $app.Run("Macro_1")
 $app.Run("Macro_2")
 $app.Run("Macro_3")
 $app.Run("Macro_4")
 $app.Run("Macro_5")
 $app.Run("Macro_6")
 $app.Run("Macro_7")
 $app.Run("Macro_8")
 $app.Run("Macro_9")
 $app.Run("Macro_10")

 #Now we save the workbook in the standard daily format and the close Excel
 $workbook.saveas("\\Server\Test\Daily_Refresh_Output\Test\$Year\$Month\Daily_Refresh_test_$Date.xlsm")
 $workbook.close()

$excel.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM