2010-04-07 5 views
0

단추를 클릭 할 때마다 새 데이터 행을 추가하는 목록을 만들려고합니다. 나는 클릭하면 버튼에 할당 된 다음 코드가 있습니다VBA 단추를 사용하여 새 행 채우기

PurchaseDate = InputBox("Enter Purchase Date:") 
    Sheets("TrackRecord").Select 
    i = 0 
    Row = i + 1 
    Range("A2").Select 
    ActiveCell.FormulaR1C1 = Row 
    Range("B2").Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C4*(1/Dashboard!R26C12)" 
    Range("C2").Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C2" 
    Range("D2").Select 
    ActiveCell.FormulaR1C1 = PurchaseDate 
    Range("E2").Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C8 + R2C4" 
    Range("F2").Select 
    ActiveCell.FormulaR1C1 = "=Waterfall!R[8]C[5]" 
    Range("F2").Select 
    Selection.AutoFill Destination:=Range("F2:I2"), Type:=xlFillDefault 
    Range("F2:I2").Select 
End Sub 

이 코드는 잘 작동을하지만, 나는 그것이 같은 행에게 버튼을 클릭 할 때마다 덮어 쓰는 대신 아래의 다음 행을 채우는 싶습니다. "Range ("A2 ") .select"섹션을 반복해야한다는 것을 알고 있습니다. "Range ("A2 ")."-> "Range ("B2 ")를 선택하십시오."..하지만 Excel 용 VBA에서이 작업을 수행하는 방법을 모르겠습니다. 그것이 내가 여러분에게 묻는 이유입니다.).

감사합니다,

답변

1

당신이 엑셀을 닫을 경우에도 다음 행을 지속 다시 열려면, 그것은 마지막 행 각 시간을 찾을하는 것이 좋습니다.

Dim Row As Long 

    Row = GetNextRow(Sheets("TrackRecord"), "A") 

    PurchaseDate = InputBox("Enter Purchase Date:") 

    Sheets("TrackRecord").Select 

    Range("A" & Row).Select 
    ActiveCell.FormulaR1C1 = Row 
    Range("B" & Row).Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C4*(1/Dashboard!R26C12)" 
    Range("C" & Row).Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C2" 
    Range("D" & Row).Select 
    ActiveCell.FormulaR1C1 = PurchaseDate 
    Range("E" & Row).Select 
    ActiveCell.FormulaR1C1 = "=Dashboard!R26C8 + R2C4" 
    Range("F" & Row).Select 
    ActiveCell.FormulaR1C1 = "=Waterfall!R[8]C[5]" 
    Range("F" & Row).Select 
    Selection.AutoFill Destination:=Range("F" & Row & ":I" & Row), Type:=xlFillDefault 
    Range("F" & Row & ":I" & Row).Select 
End Sub 


Private Function GetNextRow(sheet As Worksheet, column As String) As Long 

    'Look for the first empty row in the specified column 

    Dim Row As Long 

    For Row = 1 To 65535 
     If sheet.Range(column & Row).Formula = "" Then 
      GetNextRow = Row 
      Exit For 
     End If 
    Next Row 

End Function 
+0

내가 생각하기에이 코드는 내 문제를 해결하지 못한다. (동일한 행을 반복해서 쓰는). 변수를 "행"영구적 인, 단추를 클릭 한 후 행 번호가 저장되도록 필요하다고 생각합니다. 그대로,이 코드가 실행될 때마다 변수 "i"는 0으로 초기화됩니다. "행"변수를 영구적으로 만드는 방법을 모르겠습니다. 어떤 아이디어? 감사. – AME

+0

아! 여기 : 이제는 시트의 끝에 A 열을 찾아 다음 빈 행을 반환하여 항상 추가됩니다. –

+0

완벽하게 작동합니다. – AME