Prompt User for Number, then pass to another Sub procedure.

zero269

Board Regular
Joined
Jan 16, 2023
Messages
219
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to figure out a way to prompt a user to input a number and to be able to use that input with a different sub procedure.
My reasoning is that I want to reuse this User Prompt in different scenarios instead of baking it into several sub procedures.

For example, I have a sub procedure that prompts me to enter a number which is then used to Insert Table Rows. I want to split the two tasks:
  1. Get Input (possibly a Function?)
  2. Insert Rows
Here's what I'm currently using to perform both tasks in a single sub procedure:
Any advice would be greatly appreciated.

VBA Code:
Sub InsertRows()
'Prompt User for number of rows to insert
    Dim iPrompt As Integer
    iPrompt = InputBox("Enter number of rows to insert", "Insert Rows at end of Table", 1)

    If iPrompt < 1 Then
        Exit Sub
    End If

' Loop Insert Rows
    Dim iCounter As Integer
    iCounter = 0
    Do While iCounter < iPrompt  'Inner Loop
        ActiveSheet.ListObjects(1).ListRows.Add 'Using Table Index
        iCounter = iCounter + 1 ' Increment iCounter
    Loop

' Select first new row
    ActiveSheet.ListObjects(1).TotalsRowRange(1).Select 'Select 1st column in totals row
    ActiveCell.Offset(-iPrompt).Select 'Offsets to select first new row
End Sub

My failed attempt at a function:
I'm not even sure if a function here is the right approach.
VBA Code:
Function GetNum(iPrompt As Integer)
    MsgBox iPrompt
End Function
 
When you execute the first module, you should be able to see that the 'iPrompt' value is being passed to both of the other two modules.
Hi Johnny,
I was able to test your code just now, and I can see that it worked as you've stated.
I ran Module1Sub and the Message Boxes reported back from Mod2&3 the correct number that I inputted.
Thank you! (y)
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

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