2016-05-16 84 views
0

目前我有這個在Sheet2上參考不同片的最後一排

Range("A2").Select 
Selection.AutoFill Destination:=Range("A2:A1001") 
Range("A2:A1001").Select 
Range("B2").Select 
Selection.AutoFill Destination:=Range("B2:B1001") 
Range("B2:B1001").Select 
Range("C2").Select 
Selection.AutoFill Destination:=Range("C2:C1001") 
Range("C2:C1001").Select 
Range("D2").Select 
Selection.AutoFill Destination:=Range("D2:D1001") 
Range("D2:D1001").Select 
Range("E2").Select 
Selection.AutoFill Destination:=Range("E2:E1001") 
Range("E2:E1001").Select 
Range("G2").Select 
Selection.AutoFill Destination:=Range("G2:G1001") 
Range("G2:G1001").Select 
Range("H2").Select 
Selection.AutoFill Destination:=Range("H2:H1001") 
Range("H2:H1001").Select 
Range("I2").Select 
Selection.AutoFill Destination:=Range("I2:I1001") 
Range("I2:I1001").Select 
Range("J2").Select 
Selection.AutoFill Destination:=Range("J2:J1001") 
Range("J2:J1001").Select 
Range("K2").Select 
Selection.AutoFill Destination:=Range("K2:K1001") 
Range("K2:K1001").Select 

而是有目的地範圍A2至A1001的我想如果在Sheet1中的最後一排有A2相同的最後一行的工作表Sheet1如是行147我想要的代碼來填充下來Selection.AutoFill Destination:=Range("A2:A147")

我不知道如何做到這一點。

感謝

+4

谷歌如何找到最後一行。編輯:我只能說這是因爲它需要寫出你的問題時,谷歌會已經告訴你了^ _ ^; – findwindow

+4

[如何找到最後一行(http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba),因爲我有它書籤。 – gtwebb

+1

'隨着表( 「原始」) LASTROW = .Range( 「I」 &.Rows.Count).END(xlUp).Row 完隨着 隨着表( 「定影液」) 範圍( 「A2」 )。選擇 Selection.AutoFill目的地:=範圍( 「A2:A&LASTROW」。) 範圍( 「A2」)Select' 喜歡該???? –

回答

2

這應該爲你工作...

Sub LastRowAutofill() 
    On Error Resume Next 
    Dim wsSheet1 As Worksheet: Set wsSheet1 = Worksheets("Sheet1") 
    Dim LastRow As Long 
    LastRow = wsSheet1.Columns(1).Find("*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row 

    Dim i As Long 
    With Worksheets("Sheet2") 
     For i = 1 To 11 
      If i <> 6 Then .Cells(2, i).AutoFill Destination:=.Range(.Cells(2, i), .Cells(LastRow, i)) 
     Next i 
    End With 

End Sub 

或者您可以使用以下內容,如果您想要整張sheet1中的最後一行:

LastRow = wsSheet1.UsedRange.Find("*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row 
0

假設你的原始數據是在Sheet1和要複製到Sheet2,這裏是你會怎麼做:

Dim intLastrow As Integer 

intLastrow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row 
Sheets("Sheet2").Range("A2").Select 
Selection.AutoFill Destination:=Range(cells(2,"A"),cells(intLastrow, "A")) 
Sheets("Sheet2").Range(Cells(2, "A"), Cells(intLastrow, "A")).Select 

intLastrow = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row  
Sheets("Sheet2").Range("B2").Select 
Selection.AutoFill Destination:=Range(cells(2,"B"),cells(intLastrow, "B")) 
Sheets("Sheet2").Range(Cells(2, "B"), Cells(intLastrow, "B")).Select