运行时错误1004对象'_Global'的方法'Range'失败

时间:2018-08-31 04:05:44

标签: excel vba

我无法确定为什么我会遇到“对象'_global'的'范围'失败”的原因。

下面是调试器中突出显示的代码:

Set PivotTablePasteRange = Range("B" & LastRow + 1 & ":" & "B" & LastRow + Data1ColumnDTotalRows - 2)

请注意,我需要在公式末尾保留-2,因为这实际上是复制和粘贴数据,突然中断的原因是因为整个页面都是空白的,有时在这种大数据转储中可能会发生这种情况。出现-2是因为我遇到了一个问题,即当两个或更少的数字存在时,最后的VBA翻了一番。

完整的VBA代码:

Sub FirstClick()

​

Sheets("combined sheet").Activate

Range("A2:D100000").Clear

Dim Data1ColumnD, Data2ColumnF, Data3ColumnD, Data4ColumnD, PivotTablePasteRange As Range

Dim Data1ColumnDLastRow, Data1ColumnDTotalRows, Data2ColumnFLastRow, Data2ColumnFTotalRows,Data3ColumnDLastRow, Data3ColumnDTotalRows,
Data4ColumnDLastRow, Data4ColumnDTotalRows, LastRow AsLong

Sheets("Data1").Activate

Data1ColumnDLastRow = Range("D" & Rows.Count).End(xlUp).Row

Data1ColumnDTotalRows = Data1ColumnDLastRow - 1

Set Data1ColumnD = Range("D4:D" & Data1ColumnDLastRow)

​

Sheets("Data2").Activate

Data2ColumnFLastRow = Range("F" & Rows.Count).End(xlUp).Row

Data2ColumnFTotalRows = Data2ColumnFLastRow - 1

Set Data2ColumnF = Range("F2:F" & Data2ColumnFLastRow)

​

Sheets("Data3").Activate

Data3ColumnDLastRow = Range("D" & Rows.Count).End(xlUp).Row

Data3ColumnDTotalRows = Data3ColumnDLastRow - 1

Set Data3ColumnD = Range("D2:D" & Data3ColumnDLastRow)

​

Sheets("Data4").Activate

Data4ColumnDLastRow = Range("D" & Rows.Count).End(xlUp).Row

Data4ColumnDTotalRows = Data4ColumnDLastRow - 1

Set Data4ColumnD = Range("D2:D" & Data4ColumnDLastRow)

​

Sheets("combined sheet").Activate

​

LastRow = Range("B" & Rows.Count).End(xlUp).Row

**Set PivotTablePasteRange = Range("B" & LastRow + 1 & ":" & "B" & LastRow + Data1ColumnDTotalRows - 2)**

Data1ColumnD.Copy

If Data1ColumnDTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("B" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("B" & LastRow + 1 & ":" & "B" & LastRow + Data2ColumnFTotalRows)

Data2ColumnF.Copy

If Data2ColumnFTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("B" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("B" & LastRow + 1 & ":" & "B" & LastRow + Data3ColumnDTotalRows)

Data3ColumnD.Copy

If Data3ColumnDTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("B" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("B" & LastRow + 1 & ":" & "B" & LastRow + Data4ColumnDTotalRows)

Data4ColumnD.Copy

If (Data4ColumnDTotalRows > 0) Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

Dim Data1ColumnH, Data2ColumnJ, Data3ColumnM, Data4ColumnM, PivotTableColumnBPasteRange,Data4Columnoutput As Range

Dim Data1ColumnHLastRow, Data1ColumnHTotalRows, Data2ColumnJLastRow, Data2ColumnJTotalRows,Data3ColumnMLastRow, Data3ColumnMTotalRows,
Data4ColumnMLastRow, Data4ColumnMTotalRows,Data4ColumnCoutputRows As Long

​

Sheets("Data1").Activate

Data1ColumnHLastRow = Range("H" & Rows.Count).End(xlUp).Row

Data1ColumnHTotalRows = Data1ColumnHLastRow - 1

Set Data1ColumnH = Range("H4:H" & Data1ColumnHLastRow)

​

Sheets("Data2").Activate

Data2ColumnJLastRow = Range("J" & Rows.Count).End(xlUp).Row

Data2ColumnJTotalRows = Data2ColumnJLastRow - 1

Set Data2ColumnJ = Range("J2:J" & Data2ColumnJLastRow)

​

Sheets("Data3").Activate

Data3ColumnMLastRow = Range("M" & Rows.Count).End(xlUp).Row

Data3ColumnMTotalRows = Data3ColumnMLastRow - 1

Set Data3ColumnM = Range("M2:M" & Data3ColumnMLastRow)

​

Sheets("Data4").Activate

Data4ColumnMLastRow = Range("M" & Rows.Count).End(xlUp).Row

Data4ColumnMTotalRows = Data4ColumnMLastRow - 1

Set Data4ColumnM = Range("M2:M" & Data4ColumnMLastRow)

​

Sheets("combined sheet").Activate

LastRow = Range("C" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("C" & LastRow + 1 & ":" & "C" & LastRow + Data1ColumnHTotalRows - 2)

Data1ColumnH.Copy

If Data1ColumnHTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("C" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("C" & LastRow + 1 & ":" & "C" & LastRow + Data2ColumnJTotalRows)

Data2ColumnJ.Copy

If Data2ColumnJTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("C" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("C" & LastRow + 1 & ":" & "C" & LastRow + Data3ColumnMTotalRows)

Data3ColumnM.Copy

If Data3ColumnMTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

LastRow = Range("C" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("C" & LastRow + 1 & ":" & "C" & LastRow + Data4ColumnMTotalRows)

Data4ColumnM.Copy

If Data4ColumnMTotalRows > 0 Then

PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If

​

Dim MyRange As Long

MyRange = Cells(Rows.Count, 2).End(xlUp).Row

Dim src As String

Dim ws As Worksheet

Set ws = Sheets("Mapping")

LastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row

lastColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column

src = "R1C1:R" & LastRow & "C" & lastColumn

Range("D2:D" & MyRange).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],Mapping!" & src & ",2,0), ""Not Mapped"")"

​

​

Application.CutCopyMode = False

​

ActiveWorkbook.RefreshAll

​

Range("A2").Select

​

End Sub

​

1 个答案:

答案 0 :(得分:0)

您只需要粘贴范围的左上角单元格。

...
LastRow = Range("B" & Rows.Count).End(xlUp).Row

Set PivotTablePasteRange = Range("B" & LastRow + 1)

If Data1ColumnDTotalRows > 0 Then

    Data1ColumnD.Copy
    PivotTablePasteRange.PasteSpecial Paste:=xlPasteValues

End If
...