Trying to replace an input box with a user form

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
810
Office Version
  1. 365
Platform
  1. Windows
I'm trying to replace an Input box which requires input of a Monday (19/11/2018) or (26/11/2018) or (12/11/2018) for instance, any Monday, but only monday.

I want this to go on a Userform but I've never used them before.


The form needs to have a manual box for inputs where a user can input a monday of any point in the past by manually writing it in (which is the way it is now) in addition to five buttons which have dynamically updating labels (these buttons will have the Monday displayed as the week day like this (-2 Weeks | -1 Weeks | Current Week | +1 Weeks | +2 Weeks) which allow the user to press the button and have that date input.

Finally, I have a bit of VBA code here which converts a date like (12/11/2018) into its Excel Timestamp code, this is below:

Code:
wcd = Int(CDbl(DateValue(InputBox("Enter Date in format 'DD/MM/YYYY'"))))


I've got command buttons in but these will just open a macro instead of input a value when clicked. I have a text-box but I don't know if this works in the same way as an Input box, I'm totally out of my depth here :(
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
First, the code you showed has superfluous conversions. You should have wcd dimensioned as Date, and then you can simply use

Code:
wcd = DateValue(InputBox("Enter Date in format 'DD/MM/YYYY'"))

You will create a TextBox. Let's call it TextBoxMonday. The user will enter a value in the texbox, and then needs a way to signal that he has done so. So you need another button that we'll call CommandButtonEnter. In the code for the form you will need something like this:

Code:
Private Sub CommandButtonEnter_Click()

   Dim UserDate As Date

   On Error GoTo BadDate
   UserDate = DateValue(TextBoxMonday)
   On Error GoTo 0

   If Weekday(UserDate) = 2 Then
      ' Do whatever action you need to do with the date here

      Me.Hide ' you may close the form here if desired

   Else
      msgbox TextBoxMonday & " is not a Monday."

   End If

   Exit Sub

BadDate:
   MsgBox """" & TextBoxMonday & """ is not a valid date"
   
End Sub
 
Upvote 0
To add:

InputBox is a function. You call the function, it asks the user for the value, then returns that value.

A textbox is totally different. It is a control. It has events that trigger calls to code, but in your case you don't need to use them. The user can type values into it and you can refer to that value by using the name of the control in your code.
 
Upvote 0
To add:

InputBox is a function. You call the function, it asks the user for the value, then returns that value.

A textbox is totally different. It is a control. It has events that trigger calls to code, but in your case you don't need to use them. The user can type values into it and you can refer to that value by using the name of the control in your code.

Ah see, that's where I was getting mixed up because everything is contained within the InputBox, so the form is totally modular. Ok that makes more sense. I'll give your suggestions a whirl and report back.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top