簡體   English   中英

從Powershell內執行Excel VBA宏,因為宏內的循環不起作用

[英]Execute excel vba macro from within powershell, for loop inside macro doesn't work

我正在嘗試從Powershell中執行Excel VBA宏。 在宏內部,我使用一個for循環刪除excel單元內的一些錯誤值。
該循環不是由powershell執行的,但是如果我在VBA編輯器中運行宏,則可以正常工作。

請在下面找到我的代碼:


來自Powershell的代碼:

$excel = New-Object -Com Excel.Application                                   $objMisVal = [System.Reflection.Missing]::Value                                                                                                           
$xlsdoc = "C:\Path\ExcelFile.xlsm"
$sheetname = "XXX"
$makro = "formatanpassungen"                       
$workbook = $excel.workbooks.open($xlsdoc, $objMisVal, $objMisVal, $objMisVal, `
$objMisVal, $objMisVal, $objMisVal, $objMisVal, $objMisVal, $objMisVal, `
$objMisVal, $objMisVal, $objMisVal, $objMisVal, $objMisVal)                                   

$excel.visible = $true

$excel.run($makro)                                 

$Workbook|Get-Member *Save*
$workbook.Save()

來自Excel VBA的代碼:

Sub formatanpassungen()
Dim iZeile As Integer
Dim sRechnungsNrOLD As String
Dim sRechnungsNrNEW As String

'ganzes Arbeitsblatt nach doppelten Rechnungsnummern durchsuchen
For i = 4 To iZeile

    If Sheets("XXX").Cells(i, 9) = 0 Then GoTo 10

    'Ermittlung RG-Nr OLD
    If i >= 5 Then sRechnungsNrOLD = Sheets("XXX").Cells(i - 1, 9)

    'Ermittlung RG-Nr NEW
    sRechnungsNrNEW = Sheets("XXX").Cells(i, 9)

    'Wenn RG-NrNEW = RG-NrOLD, then....
    If sRechnungsNrNEW = sRechnungsNrOLD Then
        Sheets("XXX").Cells(i, 14) = ""
        Sheets("XXX").Cells(i, 15) = ""
    End If

10:

Next i
End Sub

下面的代碼現在可以正常運行,但不能運行完整的代碼。

' Aktualisieren Makro

Dim iZeile As Integer
Dim sRechnungsNrOLD As String
Dim sRechnungsNrNEW As String

iZeile = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row

With Range("I4:I" & iZeile)
    .NumberFormat = "General"
    .Value = .Value
End With

'Formatanpassungen
iZeile = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row
For i = 4 To iZeile

    If Sheets("xxx").Cells(i, 9) = 0 Then GoTo 10

    'Ermittlung RG-Nr OLD
    If i >= 5 Then sRechnungsNrOLD = Sheets("xxx").Cells(i - 1, 9)

    'Ermittlung RG-Nr NEW
    sRechnungsNrNEW = Sheets("xxx").Cells(i, 9)

    'Wenn RG-NrNEW = RG-NrOLD, dann....
    If sRechnungsNrNEW = sRechnungsNrOLD Then
        Sheets("xxx").Cells(i, 14) = ""
        Sheets("xxx").Cells(i, 15) = ""
    End If

10:

Next i

'Alle Pivot-Tabellen aktualisieren
Sheets("ÜBERSICHT_Alle").Select
Range("A7").Select
ActiveWorkbook.RefreshAll
Sheets("xxx").Select

End Sub

暫無
暫無

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

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