Use data from single inputbox twice

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I have this little bit of code that will ask the user which quarter, based on their input a particular marco is called. Further down the script I would like to again call the same particular macro based on their previous input without having to prompt them a second time for the same information.

BTW I'm not sure if it matters or not, but the prompt is a macro in itself that is called from my main script.

Basically, how do I go about storing their answer to be used later on?

Thank you for for the help.
VBA Code:
Private Sub which_quarter()
    myValue = InputBox("Which Quarter? 3,4", "", 1)
        If myValue = "3" Then
            Call Q3_1
        ElseIf myValue = "4" Then
            Call Q4_1
        End If
End Sub
 
?

VBA Code:
Public named As String
Sub tester()

named = "hello"

Application.Run named
End Sub

Sub hello()

MsgBox (named)

End Sub
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you want to call a macro 2nd time, just call it a second time :p

With the structure of your code, you can declare the variable at the module level. As other's have said, you need to put it at the top before any procedures then. If you want the variable to be available for all procedures only within this module then "Dim myValue as Integer" will be enough.

See Understanding scope and visibility (VBA) and Understanding the lifetime of variables (VBA).
 
Upvote 0
Are you saying you want to call your macro again from a different sub ?

but your 1st sub is the one naming your naming the macro to call. ?
 
Upvote 0
If you want to call a macro 2nd time, just call it a second time :p

With the structure of your code, you can declare the variable at the module level. As other's have said, you need to put it at the top before any procedures then. If you want the variable to be available for all procedures only within this module then "Dim myValue as Integer" will be enough.

See Understanding scope and visibility (VBA) and Understanding the lifetime of variables (VBA).
Sounds simple enough, if I knew what I was doing :p

I like your idea of "Dim myValue as integer", because I do only need it within the same module.

VBA Code:
Private Sub which_quarter()
    
    Dim myValue As Integer
    myValue = InputBox("Which Quarter? Q3, Q4", "")

End Sub

I tried running which_quarter at the start for the user to be prompted which quarter to run

VBA Code:
Sub Area82()
    Call which_quarter
VBA Code:
    Sheets("Sheet1").Select
    Application.Run myValue
    Rows("14:30").Delete
    Call Sales_Format
    Range("A1:E13").Select
    Sheets("Sheet1").Name = "82a Sales"
VBA Code:
    Sheets("Sheet5").Select
    Application.Run myValue
    Rows("19:30").Delete
    Call Sales_Format
    Range("A1:E18").Select
    Sheets("Sheet5").Name = "82b Sales"
 
Upvote 0
Are you saying you want to call your macro again from a different sub ?

but your 1st sub is the one naming your naming the macro to call. ?
I have my main sub, the inputbox is outside in it's own sub
 
Upvote 0
You have to put it outside of any procedures, at the very top of the module.

Example (I have "Option Explicit" which requires me to declare all variables):

VBA Code:
Option Explicit

Dim myValue as Integer

Private Sub which_quarter()

' ...

End Sub

' ...
 
Upvote 0
By the way, I believe I made a mistake above, as you are using string values. Check this code in a new module (can be a new xlsm workbook):

VBA Code:
Option Explicit

Dim myValue As String

Private Sub Main()
    
    myValue = ""
    
    Debug.Print "Before which_quarter: " & myValue
    Call which_quarter
    Debug.Print "After which_quarter: " & myValue
    Call which_quarter

End Sub

Private Sub which_quarter()
    
    If myValue = "" Then
        myValue = InputBox("Which Quarter? 3,4", "", 1)
        If myValue = "" Then End
    End If
    
    If myValue = "3" Then
        Call Q3_1
    ElseIf myValue = "4" Then
        Call Q4_1
    End If
    
End Sub

Private Sub Q3_1()

    Debug.Print "Q3_1"

End Sub

Private Sub Q4_1()

    Debug.Print "Q4_1"

End Sub
 
Upvote 0
This is what did the trick
VBA Code:
Option Explicit
Sub Qtr_Input()
Dim strQtr As String
strQtr = InputBox("enter Quarter here (1,2,3,4)")
Area82 strQtr
End Sub

In my main Sub I had to add (strQtr As String) to the Sub header

VBA Code:
Sub Area82(strQtr As String)

Then use the following throughout Sub Area82

VBA Code:
Select Case strQtr
Case Is = "3"
Call Q3
Case Is = "4"
Call Q4
End Select
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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