这是将两种软件文档组合在一起并分析数据以生成图表或报告的工具代码的一部分。这个工具已经使用了很长时间。但是这个特定文件的列标题已经改变了。所以这个工具不能再运作了。在运行这个porgrem时。从新文件导入数据时,调试器显示错误1004并指定范围选择问题。感谢您的帮助。
Sub Datenzusammenfügen(nomdoc As String, x As Integer)
Dim LZ As Long
Dim LS As Long
Dim LZe As Long
Dim LSe As Long
LS = Workbooks(nomdoc).Worksheets("inter1SAP").Cells(3, 256).End(xlToLeft).Column
LZ = Workbooks(nomdoc).Worksheets("inter1SAP").Cells(Rows.Count, 1).End(xlUp).Row
blatterstellen "inter2SAP", nomdoc, 5
Workbooks(nomdoc).PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Range(Workbooks(nomdoc).Worksheets("inter1SAP").Cells(3, 1), Workbooks(nomdoc).Worksheets("inter1SAP").Cells(LZ, LS)), _
Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=Workbooks(nomdoc).Worksheets("inter2SAP").Cells(1, 1), _
TableName:="SAPGesamt", DefaultVersion:=xlPivotTableVersion12
Workbooks(nomdoc).Worksheets("inter2SAP").Activate
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("Partnerobjekt")
.Orientation = xlRowField
.Position = 1
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("Partnerobjektart")
.Orientation = xlRowField
.Position = 2
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("Cost Center")
.Orientation = xlRowField
.Position = 3
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("Leistungsart")
.Orientation = xlRowField
.Position = 4
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("Bereich in SAP")
.Orientation = xlRowField
.Position = 5
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
With ActiveSheet.PivotTables("SAPGesamt").PivotFields("OBS element SAP")
.Orientation = xlRowField
.Position = 6
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End With
k = 7
Do While Workbooks(nomdoc).Worksheets("inter1SAP").Cells(2, k).Value <> ""
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Jan"), "Summe von Jan", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Feb"), "Summe von Feb ", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Mrz"), "Summe von Mrz", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Apr"), "Summe von Apr", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Mai"), "Summe von Mai", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Jun"), "Summe von Jun", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Jul"), "Summe von Jul", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Aug"), "Summe von Aug", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Sep"), "Summe von Sep", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Okt"), "Summe von Okt", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Nov"), "Summe von Nov", xlSum
ActiveSheet.PivotTables("SAPGesamt").AddDataField ActiveSheet.PivotTables( _
"SAPGesamt").PivotFields("Dez"), "Summe von Dez", xlSum
k = k + 12
Loop
With ActiveSheet.PivotTables("SAPGesamt")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
Battloeschen "inter1SAP", nomdoc
LZe = Workbooks(nomdoc).Worksheets("inter2SAP").Cells(Rows.Count, 4).End(xlUp).Row
LSe = Workbooks(nomdoc).Worksheets("inter2SAP").Cells(2, 256).End(xlToLeft).Column
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 1), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 1)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 2).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 2), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 2)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 3).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 3), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 3)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 5).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 4), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 4)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 7).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 5), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 5)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 8).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 6), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, 6)).Copy
Sheets("Rohdaten SAP " & x).Select
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 11).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range(Workbooks(nomdoc).Worksheets("inter2SAP").Cells(3, 7), _
Workbooks(nomdoc).Worksheets("inter2SAP").Cells(LZe, LSe)).Copy
Workbooks(nomdoc).Worksheets("Rohdaten SAP " & x).Cells(4, 17).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Battloeschen "inter2SAP", nomdoc
formatertableau nomdoc, "Rohdaten SAP " & x, LZe, 3, 2, 3, 5, 7, 8, 11
End Sub
答案 0 :(得分:0)
我认为问题出在Range
您使用的行中:
Workbooks(nomdoc).PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ Range(Workbooks(nomdoc).Worksheets("inter1SAP").Cells(3, 1), Workbooks(nomdoc).Worksheets("inter1SAP").Cells(LZ, LS)), _ Version:=xlPivotTableVersion12).CreatePivotTable
由于您尚未在Range
对象之前指定工作表,因此它会尝试在活动工作表上找到它。
尝试这样的事情:
Dim PivotRange as Range
With Workbooks(nomdoc).Worksheets("inter1SAP")
Set PivotRange = .Range(.Cells(3,1), .Cells(LZ, LS))
End With
Workbooks(nomdoc).PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=PivotRange, _
Version:=xlPivotTableVersion12).CreatePivotTable... etc.
这是未经测试的,所以不要直接复制/粘贴它,因为它可能有其他错误,但我认为它应该让你朝着正确的方向。