Can't use variable from input box in other subs in same module

1k views Asked by At

So I am trying to get a variable from an input box. I then use the variable to name my sheet. But I cannot reference the sheet in other subs in order to switch back to it. It gives me an error. My code is probably sloppy because I am new and I separate my subs probably more than I need to but I have been testing them a piece at a time. Anyway here it is:

Dim name As String
name = InputBox("Please enter the date of the report. Ex: 7-28 to 8-25-17. This will show up as: All HCM changes 7-28 to 8-25-17 for the tab name.", "Tab Name Date")
If Len(name) = 0 Then 'Checking if Length of name is 0 characters
    MsgBox "Valid date not entered. Please Re-Run the Macro to input the date.", vbCritical
Else
    MsgBox "The tab will now be named, All HCM changes " & name & "."
End If

Sheets("Sheet1").Select
Sheets("Sheet1").name = ("All HCM changes " & name)

Call Change_Header_Colors
Call Insert_Columns
Call Create_LEGEND
Call Sort_by_Action_then_Last_Name
Call Freeze_Panes

In those calls, I am referencing the name variable. But it is giving me an error. I tried declaring the variable before the first sub in the module but that didn't work. This is how I am trying to use it in those calls.

Sub Sort_by_Action_then_Last_Name()
'
' This is ACTUALLY sorting by action then by person #. Which is what we 
wanted. Can change it easily.
'

'
Dim name As String

ActiveWorkbook.Worksheets("All HCM changes " & name).Select
Cells.Select
ActiveWorkbook.Worksheets("All HCM changes " & name).SORT.SortFields. _
    Clear
ActiveWorkbook.Worksheets("All HCM changes " & name).SORT.SortFields. _
    Add Key:=Range("A2:A246"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("All HCM changes " & name).SORT.SortFields. _
    Add Key:=Range("E2:E246"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("All HCM changes " & name).SORT
    .SetRange Range("A1:U246")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
2

There are 2 answers

8
Scott Craner On BEST ANSWER

You need to pass the variable to the other subs:

Sub Sort_by_Action_then_Last_Name(name as String)

and Remove the Dim name as String line from the secondary sub as you are declaring the variable in the call.

Then when you call it you would call it like:

Call Sort_by_Action_then_Last_Name(name)
2
tk78 On

Either make your procedures (Sub) accept a parameter for handing over the worksheet Name, i.e.

Sub Main()

    Dim name As String

    name = InputBox("Please enter worksheet name")

    'call your other procedures
    Change_Header_Color name

End Sub

Sub Change_Header_Color(ByVal name As String)

    'do some stuff here

    'use the input from the parameter in some way
    Worksheets("foo").Range("A1").Value = name

End Sub

Alternatively, you can define a global variable and use if across the different Subs.

Private name As String

Sub Main()

    name = InputBox("Please enter worksheet name")

    'call your other procedures
    Change_Header_Color

End Sub

Sub Change_Header_Color()

    'do some stuff here

    'use the input from the parameter in some way
    Worksheets("foo").Range("A1").Value = name

End Sub