1

I have 2 macros.

Macro1 when complete calls Macro2.

Macro2 has an inputbox where I have to place a value. (The value doesn't really matter for this question).

I would like to avoid placing a value in the inputbox (skipping a manual step) when calling from Macro1 by automatically assigning a value to the variable in Macro2.

In my mind, I was thinking something like Call Macro2(inputbox = 1)

I'm clearly missing something, but not really sure what exactly I'm searching for, and all my searches don't seem to answer the question.

Any help would be greatly appreciated.

Rarblack
  • 4,559
  • 4
  • 22
  • 33
Dan
  • 21
  • 6
  • 1
    You can either declare a global variable which is filled in macro1 or as you said, ask for the variable when callind the macro2. Here for the global variable: https://stackoverflow.com/questions/46772270/excel-vba-global-variable Here on how to pass variables to another macro: https://stackoverflow.com/questions/29858176/how-to-call-a-macro-from-a-button-and-pass-arguments – Damian Oct 08 '18 at 16:46
  • 1
    Care to show your code? – urdearboy Oct 08 '18 at 16:51
  • 1
    Are you familiar with [Arguments](https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-named-arguments-and-optional-arguments)? – Alex K. Oct 08 '18 at 16:52
  • 1
    These were all useful and thank you for your help. I could have avoided making a fool of myself if I knew some basic terms haha. – Dan Oct 08 '18 at 17:15

1 Answers1

0

This is pretty simple. Probably in the first chapter of how to code using VBA.

Sub Macro1()

    Dim sArg1 As String

    Rem: code here

    Rem Grab inputbox value to pass to macro2
    sArg1 = Inputbox("Type your value here")

    Macro2 sArg1        Rem Pass your argument in the call itself
    '  -- or --         ' (Notice in Macro2, your argument of arg1)
    Call Macro2(sArg1)

End Sub

Sub Macro2(arg1 As String)

    rem your inputbox value was passed to macro2 as the argument 'arg1'
    debug.print arg1

End Sub
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • Thank you. I was getting thrown off cause when moving through the macro it went to the inputbox again, so I pulled that piece of code out so it moves through smoothly. I no longer can see the macro in my dropdown, but I should be okay not being able to access it. – Dan Oct 08 '18 at 17:13