任何人都可以帮助解释为什么我的范围不会完全更新以及我如何强制这样做? 单元格范围更新,但焦点仍在同一张纸上。然后当它到达必须复制的下一行时失败。
Workbooks.Open Filename:="U:\T\A.xlsx", UpdateLinks:=xlUpdateLinksNever
dblLastRow = 1 + Workbooks("A.xlsx").Worksheets("Z").Cells(Rows.Count, 1).End(xlUp).Row
Set rngReport2 = Workbooks("A.xlsx").Worksheets("Report2").Range(Cells(1, 1), Cells(dblLastRow, 7))
rngReport2.Copy
Workbooks("B.xls").Worksheets("Y").Range("A1").PasteSpecial (xlPasteValues)
dblLastRow = Workbooks("A.xlsx").Worksheets("Sector code").Cells(Rows.Count, 1).End(xlUp).Row
Set rngReport2 = Workbooks("A.xlsx").Worksheets("Sector code").Range(Cells(1, 1), Cells(dblLastRow, 3))
rngReport2.Copy
Workbooks("B.xls").Worksheets("X").Range("A1").PasteSpecial (xlPasteValues)
答案 0 :(得分:1)
您应始终使用工作表参考资格Cells()
:
Sub test()
Dim wbA As Workbook, wbB As Workbook
Dim dblLastRow As Long
Set wbA = Workbooks.Open(Filename:="U:\T\A.xlsx", UpdateLinks:=xlUpdateLinksNever)
Set wbB = Workbooks("B.xls")
With wbA.Worksheets("Report2")
dblLastRow = 1 + .Cells(Rows.Count, 1).End(xlUp).Row
CopyValues .Range(.Cells(1, 1), .Cells(dblLastRow, 7)), wbB.Worksheets("Y").Range("A1")
End With
With wbA.Worksheets("Sector code")
dblLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
CopyValues .Range(.Cells(1, 1), .Cells(dblLastRow, 3)), wbB.Worksheets("X").Range("A1")
End With
End Sub
Sub CopyValues(rngSrc As Range, rngDest As Range)
rngDest.Cells(1).Resize(rngSrc.Rows.Count, _
rngSrc.Columns.Count).Value = rngSrc.Value
End Sub