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
You need to pass the variable to the other subs:
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: