Getting user input through vba

与世无争的帅哥 提交于 2020-01-17 07:52:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!