2016-07-20 5 views
0
Sub AutoFill() 

    Dim x As Long 
    Dim y As Long 
    Dim lastrow As Long 
    Dim lastcolumn As Long 

    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count 
    lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count 

    For x = 2 To lastrow 
     If Cells(x, 2).Value = "" Then 
      Cells(x, 2).Value = Cells(x - 1, 2).Value 
      Cells(x, 3).Value = Cells(x - 1, 3).Value 
      Cells(x, 5).Value = Cells(x - 1, 5).Value 
     End If 
    Next x 

    Application.ScreenUpdating = True 

End Sub 

위의 코드를 사용하면 셀이 채워지지만 마지막 행이 Excel 시트 끝까지 채워집니다. 엑셀 시트 란에 CE이 이미 채워져있는 D은 자동으로 아래로 채워야합니다. 코드의 변경 사항은 무엇입니까?Excel에서 셀 자동 채우기 VBA 매크로

+1

왜'ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count' ?? HTTP : //stackoverflow.com/questions/71180/how-can-i-find-last-row- 그 - 포함 - 엑셀 - 시트 -와 - 매크로 – cyboashu

+0

귀하의 코드는 내 PC에 귀하의 요구 사항에 따라 노력하고 있습니다. [확인] (https://www.dropbox.com/s/kg380esjoyxb0o9/autofill_sample.xlsm?dl=0) – skkakkar

+1

lastRow의 경우 3 열을 비교하고 거기에서 마지막 행을 가져옵니다. 'UsedRange'는 제 경험상 항상 기대했던 범위를 사용하지는 않습니다. lastRow = 셀 (rows.count, 2) .End (xlUp) .Row, cells (rows.count, 3) .End (xlUp) .Row, cells (rows.count, 5) .End (xlUp) .Row' * 또는 * LastRow = Cells.Find ("*", searchorder : = xlByRows, searchdirection : = xlPrevious) .Row' – BruceWayne

답변

1

Excel VBA Last Row: The Complete Tutorial To Finding The Last Row In Excel With VBA (And Code Examples)Cells.Find을 사용하여 마지막을 결정할 때 LookIn:=xlFormulas을 사용할 것을 권장합니다.

lastrow = Find(What:=” * ”, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 

당신이 열 D 이미 내가 사용에 가득 것을 주장하기 때문에 : 열 E가에 작성되지

lastrow = Range("D" & Rows.Count).End(xlUp).Row 

경우 Cells(x, 2).Value<> ""을해야합니다.

Sub AutoFill() 
    Dim x As Long 
    Dim y As Long 
    Dim lastrow As Long 
    Dim lastcolumn As Long 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count 
    lastrow = Range("D" & Rows.Count).End(xlUp).Row 

    For x = 2 To lastrow 

     If Cells(x, 2).Value = "" Then Cells(x, 2).Value = Cells(x - 1, 2).Value 
     If Cells(x, 3).Value = "" Then Cells(x, 3).Value = Cells(x - 1, 3).Value 
     If Cells(x, 5).Value = "" Then Cells(x, 5).Value = Cells(x - 1, 4).Value 

    Next x 
    Application.ScreenUpdating = True 

End Sub 
+0

고마워요 :-) –

+0

굉장! 내가 도울 수있어서 기뻐. –