我想将内容从一张纸复制到另一张纸。我正在尝试使用:
For Each r In tempWbk.Sheets("BSC").Columns
tempWbk.Sheets("BSC").Columns(r) = instanceWbk.Sheets("BSC").Columns(r)
Next
但它不起作用。有什么建议吗?
如果您将一个工作簿中的工作表内容复制到另一个工作簿中的工作表,则无需遍历列。
你可以这样做:
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