我需要将两个报告合并到一个中央模板中,并且由于某种原因我的代码没有复制我的第一个报告的最后一行和最后一列,任何人都可以对其进行编辑以使其起作用吗?
Dim legal_wb As Workbook, cib_wb As Workbook, comp_wb As Workbook
Set legal_wb = Workbooks.Open(Filename:=Legal)
Set cib_wb = Workbooks.Open(Filename:=CIB)
Set comp_wb = Workbooks.Open(Filename:=Comp_TCR)
Dim legal_ws As Worksheet, cib_ws As Worksheet, comp_ws As Worksheet
Set legal_ws = legal_wb.Sheets("ps")
Set cib_ws = cib_wb.Sheets("ps")
Set comp_ws = comp_wb.Sheets("Sheet1")
Dim lrow As Long, lcol As Long
Dim legal_rng As Range
lrow = legal_ws.Range("A" & Rows.Count).End(xlUp).Row
lcol = legal_ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set legal_rng = legal_ws.Range(Cells(lrow, 2), Cells(lrow, lcol))
legal_rng.Copy
在Set legal_rng
行上发生错误
我希望将第二行的legal_ws
范围(标题被跳过)复制到最后一行和最后一列,因此我可以将其移至comp_wb
,但是它什么也没选择。
答案 0 :(得分:1)
这可能是因为您需要在括号内的legal_ws
之前插入Cells
。
因此,此:
Set legal_rng = legal_ws.Range(Cells(lrow, 2), Cells(lrow, lcol))
对此:
Set legal_rng = legal_ws.Range(legal_ws.Cells(lrow, 2), legal_ws.Cells(lrow, lcol))