2012-09-21 177 views

回答

1

您需要執行VB編輯器菜單/工具/參考,然後選中Microsoft Scripting Runtime旁邊的複選框。 這使您可以訪問的Scripting.Dictionary,這是加密散列對象(鍵/值對)

Sub CopyByCol() 

Dim lCol As Long, lCols As Long, sCol As String, lRows As Long 
Dim dictCol As New Scripting.Dictionary 
Dim rFrom As Range, rTo As Range 

    Set rFrom = Sheet1.Range("A1") 
    Set rTo = Sheet2.Range("A1") 

' Store hash of where column names occur in target sheet, keyed by Col name 
    lCols = rFrom.Offset(0, 255).End(xlToLeft).Column - rFrom.Column 
    For lCol = 0 To lCols 
     sCol = rTo.Offset(0, lCol).Value 
     dictCol.Add sCol, lCol 
    Next 

' Get key column name from From sheet, and write to correct col in To sheet 
    For lCol = 0 To lCols 
     sCol = rFrom.Offset(0, lCol).Value 
     lRows = rFrom.Offset(65000, lCol).End(xlUp).Row - rFrom.Row 
     Range(rFrom.Offset(1, lCol), rFrom.Offset(lRows, lCol)).Copy Destination:=rTo.Offset(1, dictCol(sCol)) 
    Next 

End Sub 
+0

謝謝你,很抱歉這麼晚纔回復:)這將肯定幫助別人的未來... – vettipayyan

相關問題