freshman2017
Goto Top

Excel VBA über Batch starten

Moin moin,

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!

Content-Key: 393589

Url: https://administrator.de/contentid/393589

Printed on: April 24, 2024 at 19:04 o'clock

Member: emeriks
emeriks Nov 23, 2018 at 07:46:44 (UTC)
Goto Top
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.
Member: freshman2017
freshman2017 Nov 23, 2018 at 07:55:44 (UTC)
Goto Top
Hi emeriks,

danke für den Link. Verstehe ich es falsch, dass hier die VBA in der Excel-Liste vorhanden sein muss? Das sieht ja relativ kompliziert aus....
Member: emeriks
emeriks Nov 23, 2018 at 08:00:12 (UTC)
Goto Top
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 ...")
Mitglied: 137846
Solution 137846 Nov 23, 2018, updated at Nov 25, 2018 at 14:50:25 (UTC)
Goto Top
Moin.
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.

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
Mitglied: 137846
137846 Nov 28, 2018 updated at 10:31:40 (UTC)
Goto Top
Dann noch bitte den Haken dran, nicht vergessen.