Input Box Assistance

DAyotte

Board Regular
Joined
Jun 23, 2011
Messages
84
Hello All,

I'm just getting started with VBA - trying to teach myself. I was lent a book by a colleague and that's helped a lot, but I have an issue for this particular project that I can figure out. I'm sure it's a bit complex for a first time project, but I wanted to tackle something that would be useful.

There is a particular report we receive on a daily basis that has 2 separate worksheets. Each worksheet is fairly different, and we have to format them into a uniform template for upload into Access. I started with a user form, and it simply asks for the date of the report. I want the macro to pause until that information is received from the user, and then continue. Now, the date is needed in the formatting of both worksheets, so I would like to call on the same user form in in each macro - otherwise I would just write it into the "OK" button.

I hope this makes sense. Also, if it's not too much to ask - could any code provided be explained? It's easy to copy and paste - but I would like to understand it as well.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi DAyotte
If you are using a UserForm, then it would be assumed that either a label or text box is being used for the user to enter the date. If both sheets require the same date to be entered, it can be done in one shot with two lines in the underlying code to the UserForm, as and example:
Code:
Sheets(1).Range("B2") = CDate(Me.TextBox1.Text) 'The ranges are arbitrary for illustration only
Sheets(2).Range("C2") = CDate(Me.TextBox1.Text)
The UserForm is independent of any sheet, so it can be called at any time, in any code, so long as its functionallity (based on its underlying code) will support the intent of the calling code.
 
Upvote 0
I guess I should have mentioned this, but sometimes, there is only one sheet. Total, there are two types.
For example:

Mon: Sheet A, Sheet B
Tues: Sheet B only
Wed: Sheet A only
Thurs: Sheet A, Sheet B
Fri: Sheet A only.
(This will vary)

Will that code still work? I feel like this would be a variable - something in the code of the macro operating to format the sheet to call on the stored data. Maybe it's just me being naive.
 
Upvote 0
This should get you started:

Code:
Public dtDate As Date

Private Function SheetExists(SheetName As String) As Boolean
    ' Returns TRUE if a sheet exists in the active workbook
    Dim x As Worksheet
        On Error Resume Next
            Set x = ActiveWorkbook.Sheets(SheetName)
                If Err = 0 Then SheetExists = True _
                Else SheetExists = False
End Function

Sub MyDate()
    dtDate = InputBox("Please enter a Report Date in mm/dd/yy format", "Report Date", Date)
    Sheets("SheetA").Range("A1").Value = dtDate
    If SheetExists("SheetB") Then
        Sheets("Sheet2").Range("A1").Value = dtDate
    End If
End Sub

HTH,
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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