问题
I have a set of dates in Col K in sheet "Latency". I need the user to input the current date and then the code should find all the dates before the entered date, copy the entire row and paste it in a different sheet named "Previous".
I'm stuck with how to get input from user and integrate it in code and execute the above. Any suggestions are welcome.
回答1:
You could use an input box to get the user to input a date, check that what is entered is actually a date, and then run the code you want:
Sub test()
Dim userdate
userdate = InputBox("Please enter a date", "Enter Date", Date)
If IsDate(userdate) Then
'Do stuff here
End If
End Sub
回答2:
Here is how to get input from the user:
Range("A1").Value = InputBox("Give me some input")
Read more here: http://www.excel-easy.com/vba/examples/inputbox-function.html
回答3:
You could try something like the code below (added some basic error-handling)
Sub InputDateBox()
Dim myDateString As String
Dim myDate As Date
myDateString = InputBox("Please enter a date", "Enter Date", Format(Date, "dd/mm/yyyy"))
If IsDate(myDateString) Then
myDate = myDateString
'Do the rest of your coding here
Else
MsgBox "Not a valid date format!"
End If
End Sub
来源:https://stackoverflow.com/questions/42245780/getting-user-input-through-vba