所以我试图从输入框中获取变量。然后我使用变量来命名我的工作表。但我无法在其他潜艇中引用该表以便切换回它。它给了我一个错误。我的代码可能很草率,因为我是新手,我将我的潜艇分开的可能比我需要的多,但我一直在测试它们。无论如何它是:
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
在这些调用中,我引用了name变量。但它给了我一个错误。我尝试在模块中的第一个子元素之前声明变量但是没有用。这就是我试图在这些调用中使用它的方法。
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
答案 0 :(得分:0)
您需要将变量传递给其他潜艇:
Sub Sort_by_Action_then_Last_Name(name as String)
并在您在通话中声明变量时从辅助子中删除Dim name as String
行。
然后当你打电话时,你会称之为:
Call Sort_by_Action_then_Last_Name(name)
答案 1 :(得分:0)
使您的程序(Sub)接受用于移交工作表名称的参数,即
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
或者,您可以定义一个全局变量,并在不同的Subs。
中使用ifPrivate 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