我正在遍历一张工作表,以便复制一定范围的单元格的值,并在另一个工作簿中的工作表中以更紧凑的间距分页(因此j =(i / 2)+3,因此一个工作表中第10行的范围将粘贴到另一工作表的第8行中)。但是我一直收到错误1004,因此由于某些原因,即使我指定了范围的地址并将它们分配给变量,它似乎也找不到我要放置数据或从中获取数据的地方(这给了我这个错误行“设置range1 = sht.Range(Cells(i,“ K”),Cells(i,“ AX”)))“)。有人知道这是怎么回事吗?我是VBA的新手,所以我很乐意提供任何帮助/建议。谢谢。
Sub Update()
'sets variable to refer to the correct location
Set sht = Workbooks("CAB DL Loop Summary
Testing.xlsm").Worksheets("Import Sheet")
Set sht2 = Workbooks("QE-NA DL Loop Overall Summary edit
practice.xlsm").Worksheets("Data")
'Finds last column and row with data in it in the Import sheet of CAB DL
Loop
last_column = sht.Cells(4, sht.Columns.Count).End(xlToRight).Column
last_row = sht.Cells(sht.Rows.Count, "A").End(xlDown).Row
'Sorts worksheet first so that correct data is selected for copy
With Workbooks("CAB DL Loop Summary Testing.xlsm").Worksheets("Import
Sheet").Sort
.SortFields.Add Key:=Range("I4"), Order:=xlAscending
.SortFields.Add Key:=Range("J4"), Order:=xlAscending
.SetRange Range(Cells(4, 1), Cells(last_row, 50))
.Header = xlYes
.Apply
End With
'sets new last row to reference proper data range for copy/pasting
new_last_row = sht.Cells(sht.Rows.Count, "I").End(xlDown).Row
'copies and pastes data from CAB DL Loop Summary to Overall Summary
For i = 10 To new_last_row Step 6
Dim row_overall As Integer
row_overall = (i / 2) + 3
NAvalue = sht2.Cells(row_overall, 6).Value
If NAvalue <> "NA" Then
Set range1 = sht.Range(Cells(i, "K"), Cells(i, "AX"))
Set range2 = sht2.Range(Cells(row_overall, "F"),
Cells(row_overall, "AR"))
range1.Copy
range2.Paste PasteSpecial:=xlPasteValues
End If
Next i
End Sub