2016-12-12 28 views
2

我目前正在努力實現以下目標:Excel的VBA - 日期,直到最後一排

A列中包含數據(例如,橙,蘋果,芒果)和B欄是空的。我想實現一個框,在輸入日期後它會在一個範圍內自動填充。

這裏是我到目前爲止的代碼:

Public Sub DATERES() 
Dim strUserResponse As String 

strUserResponse = InputBox("Enter Date") 

ActiveSheet.Range("B1:B100").Value = strUserResponse 

End Sub 

到目前爲止好,不過,我的問題是,在一個數據可以從1到500-600的時候。因此,我想修改它,以便它首先檢查列A,並且只在列B中輸入日期直到最後一行,而不是給它一個更大的範圍(如B1:B1000)。

任何幫助和建議表示讚賞。

祝您有美好的一天!

回答

1
Public Sub DATERES() 
Dim strUserResponse As String 
dim lastrow as long 

strUserResponse = InputBox("Enter Date") 

lastrow = Cells(Rows.Count,1).end(xlup).row 'the 1 will get the last row for column A. Change if needed 


ActiveSheet.Range("B1:B"& lastrow).Value = strUserResponse 

End Sub 

它的安全使用引用的ActiveSheetSheetsRange!而非。

' modify "Sheet1" to your sheet's name 
With Sheets("Sheet1") 

    lastrow = .Cells(.Rows.count, 1).End(xlUp).Row ' the 1 will get the last row for column A. Change if needed 

    .Range("B1:B" & lastrow).Value = strUserResponse 
End With 
+2

您LASTROW在看B列,裏面是空的(閱讀後),你需要填寫日期(在A欄)。因此您需要搜索A列中的最後一行。 –

+0

Ooops。我還沒有喝咖啡。誤讀了。固定! – Moacir

+1

感謝您的迅速回復! :)工程太棒了! – llorcs

1

僅供參考,如果你希望你的ImputBox強制用戶在Date格式輸入一個值,使用下面的臺詞:

Dim strUserResponse As Date 

strUserResponse = Application.InputBox("Enter Date", , FormatDateTime(Date, vbShortDate), Type:=1)