InputBox - make last value the default value

HURTMYPONY

Board Regular
Joined
Oct 27, 2003
Messages
159
Hello, all!

I have an input box with two required entries - "Date" and "New or Used".

I would like the InputBox to use the last values the user entered for both entries as the default value for the InputBox the next time the InputBox is called.

It only has to remember these defaults until the user changes one or both or the workbook is closed.

Is this possible?

Thank you for the help.

My InputBox code is below for reference....

VBA Code:
Sub BBInsertFill()
Dim dt As String
Dim tp As String
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
dt = InputBox("Enter Date", "DATE")
tp = InputBox("New or Used?", "N/U")
    If dt = "" Then Exit Sub
Columns(3).Insert
Range("C2") = dt
ActiveSheet.Range("C2:C2").Copy ActiveSheet.Range("C2:C" & LR)
Columns(4).Insert
Range("D2") = tp
ActiveSheet.Range("D2:D2").Copy ActiveSheet.Range("D2:D" & LR)

ActiveSheet.UsedRange.Select

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can create/use Global Variables, so these values will be saved during your Excel session.
Note that, however, that means it will only remember values entered in during that Excel session.
If you want it to remember values entered the last time you had the file open, you would need to store those value somewhere on one of your Excel sheets in the file.
 
Upvote 0
Thank you for the reply, Joe4.

Forgive my ignorance, but when I substitute "Public" [or "Global"] for "Dim", I get this error:

"Compile error: Invalid attribute in Sub or Function"

Which surely means I am overlooking or misusing some syntax...


VBA Code:
Sub BBInsertFill()
Public dt As String
Public tp As String
'Dim dt As String
'Dim tp As String
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
dt = InputBox("Enter Date", "DATE")
tp = InputBox("New or Used?", "N/U")
    If dt = "" Then Exit Sub
Columns(3).Insert
Range("C2") = dt
'Range("C2", Range("C" & Rows.Count).End(xlUp)).Offset(, 1) = dt
ActiveSheet.Range("C2:C2").Copy ActiveSheet.Range("C2:C" & LR)
Columns(4).Insert
Range("D2") = tp
ActiveSheet.Range("D2:D2").Copy ActiveSheet.Range("D2:D" & LR)

ActiveSheet.UsedRange.Select

End Sub
 
Upvote 0
Public (Global) variable must be declared outside of any procedure or function, usually at the very top, i.e.
Excel Formula:
Public dt As String
Public tp As String

Sub BBInsertFill()
...
They are only declared once. Do not declare them again inside any of the procedures.
 
Upvote 0
Thanks!

It looks like I have it working with your help!

It took me a bit to realize I also need to add the default option my InputBox for them to actually display, grin...
 
Upvote 0
Excellent! Glad to hear it is working the way you want now.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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