Excel VBA über Batch starten
Moin moin,
habe folgende VBA:
Wie kann ich VBA auf eine Test.xls, die auf dem Desktop liegt, anwenden, sodass dann nach Abschluss die Excel Datei geöffnet werden kann?
Ich freue mich auf eine Rückmeldung und verbleibe mit einem schönen Abend!
habe folgende VBA:
Sub CreatePivotTable()
Dim PTCache As PivotCache
Dim PT As PivotTable
'-- Create the cache
Set PTCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion)
'-- Add a new sheet for the pivot table
Worksheets.Add
'-- Create the pivot table
Set PT = ActiveSheet.PivotTables.Add( _
PivotCache:=PTCache, _
tabledestination:=Range("A3"))
'-- Specify the fields
With PT
.PivotFields("Region").Orientation = xlPageField
.PivotFields("Month").Orientation = xlColumnField
.PivotFields("Store").Orientation = xlRowField
.PivotFields("Sales").Orientation = xlDataField
'-- Rename Worksheet
ActiveSheet.Name = "PivotTable"
'-- No field captions such as "Row Labels" or "Column Labels"
.DisplayFieldCaptions = False
End With
End Sub
Wie kann ich VBA auf eine Test.xls, die auf dem Desktop liegt, anwenden, sodass dann nach Abschluss die Excel Datei geöffnet werden kann?
Ich freue mich auf eine Rückmeldung und verbleibe mit einem schönen Abend!
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 393589
Url: https://administrator.de/forum/excel-vba-ueber-batch-starten-393589.html
Ausgedruckt am: 11.04.2025 um 09:04 Uhr
5 Kommentare
Neuester Kommentar
Hi,
z.B. mit VBscript oder PowerShell. Das geht dann in diese Richtung:
Run Excel Macro from Outside Excel Using VBScript From Command Line
E.
z.B. mit VBscript oder PowerShell. Das geht dann in diese Richtung:
Run Excel Macro from Outside Excel Using VBScript From Command Line
E.

Moin.
Gruß A.
Zitat von @emeriks:
Nein. Man kann fast alles (wenn sogar alles), was man im VBA als Code hat, auch in VBscript umsetzen. Der Unterschied ist nur, dass man sich die "Grund-Objekte" z.B. "Application" usw. erst instantiieren muss. ("CreateObject ...")
Und als Ergänzung: Die Excel Konstanten wie xlPageField usw. muss man in VBS in Ihre tatsächlichen Integer-Werte umwandeln oder Variablen dafür deklarieren, weil diese in der VBS Umgebung ja nicht existieren. Die Werte kann man entweder in der Doku nachschlagen, oder man lässt sie sich im VBA Direktfenster per debug.print xlKonstante ausgeben. Zusätzlich gibt es keine benannten Parameter, die muss man weglassen aber die Reihenfolge muss nach Doku stimmen.Nein. Man kann fast alles (wenn sogar alles), was man im VBA als Code hat, auch in VBscript umsetzen. Der Unterschied ist nur, dass man sich die "Grund-Objekte" z.B. "Application" usw. erst instantiieren muss. ("CreateObject ...")
Gruß A.
Dim PT,PTCache
Const DIR = "C:\Ordner"
Set objExcel = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
objExcel.ScreenUpdating = False
objExcel.DisplayAlerts = False
objExcel.EnableEvents = False
For Each file In fso.GetFolder(DIR).Files
If LCase(fso.GetExtensionName(file.Name)) = "xls" Then
Set wb = objExcel.Workbooks.Open(file.Path)
With wb
'-- Create the cache
Set PTCache = .PivotCaches.Create(1,.Sheets.Item(1).Range("A1").CurrentRegion)
'-- Add a new sheet for the pivot table
Set wsPivot = .Worksheets.Add
'-- Rename Worksheet
wsPivot.Name = "PivotTable"
'-- Create the pivot table
Set PT = wsPivot.PivotTables.Add(PTCache,wsPivot.Range("A3"))
'-- Specify the fields
With PT
.PivotFields("Region").Orientation = 3
.PivotFields("Month").Orientation = 2
.PivotFields("Store").Orientation = 1
.PivotFields("Sales").Orientation = 4
'-- No field captions such as "Row Labels" or "Column Labels"
.DisplayFieldCaptions = False
End With
'.Save
'.Close True
End With
End If
Next
objExcel.ScreenUpdating = True
objExcel.DisplayAlerts = True
objExcel.EnableEvents = True
objExcel.Visible = True

Dann noch bitte den Haken dran, nicht vergessen.