我想将内容从一张纸复制到另一张。我正在尝试使用:
For Each r In tempWbk.Sheets("BSC").Columns
tempWbk.Sheets("BSC").Columns(r) = instanceWbk.Sheets("BSC").Columns(r)
Next
但它不起作用。有什么建议吗?
答案 0 :(得分:0)
如果要将一个工作簿中的工作表内容复制到另一个工作簿中的工作表中,则无需遍历这些列。
您可以这样做:
Sub CopyCells()
Dim srcWb As Workbook
Dim tgtWb As Workbook
Dim srcWs As Worksheet
Dim tgtWs As Worksheet
Set srcWb = ThisWorkbook
Set tgtWb = Workbooks.Open("C:\Temp\Targetbook.xlsx")
Set srcWs = srcWb.Sheets("BSC")
Set tgtWs = tgtWb.Sheets("BSC")
srcWs.Cells.Copy tgtWs.Range("A1")
End Sub
如果您需要逐列复制特定原因,可以将此作为起点:
Sub CopyColumns()
Call CopyThisManyColumns(5)
End Sub
Sub CopyThisManyColumns(colCount As Integer)
Dim srcWb As Workbook
Dim tgtWb As Workbook
Dim srcWs As Worksheet
Dim tgtWs As Worksheet
Dim lastRow As Long
Dim i As Integer
Set srcWb = ThisWorkbook
Set tgtWb = Workbooks.Open("C:\Temp\Targetbook.xlsx")
Set srcWs = srcWb.Sheets("BSC")
Set tgtWs = tgtWb.Sheets("BSC")
For i = 1 To colCount
lastRow = srcWs.Cells(srcWs.Rows.Count, i).End(xlUp).Row
srcWs.Range(srcWs.Cells(1, i), srcWs.Cells(lastRow, i)).Copy tgtWs.Cells(1, i)
Next i
End Sub