2013-08-25 191 views

回答

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 
相關問題