0
我想將一張紙上的內容複製到另一張紙上。我試圖使用:使用VBA將列從一張紙複製到另一張紙上?
For Each r In tempWbk.Sheets("BSC").Columns
tempWbk.Sheets("BSC").Columns(r) = instanceWbk.Sheets("BSC").Columns(r)
Next
但它不起作用。任何建議?
我想將一張紙上的內容複製到另一張紙上。我試圖使用:使用VBA將列從一張紙複製到另一張紙上?
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