在Subs之间引用公共变量

时间:2018-03-24 12:55:27

标签: excel-vba variables vba excel

我正在尝试定义一些变量(用于行/列引用)并在多个子中使用它但我遇到了一些问题:

更新 - 修复了之前的问题。现在我可以让列循环。但是,当列AE可见并且有人点击下一步时,我希望该过程隐藏当前行,重置为列E,即5,并显示下一行,然后再次循环到列AE。目前,当我显示AE时点击“下一步”,它只是隐藏当前行而不显示下一行或隐藏列D:AE ....

它的代码是:

If Columns("AE:AE").EntireColumn.Hidden = False Then
Columns("D:AE").EntireColumn.Hidden = True
Rows(Question).EntireRow.Hidden = True
Question = Question + 1
Rows(Question).EntireColumn.Hidden = False
Brand = 5
End If

完整代码:

   Public Brand As Integer
' this will hold the current visible column
Public Question As Integer
' this will hold the current visible row

Sub StartQuestionnaire()

' this sends the user to two different questionnaire formats
If ThisWorkbook.Team = 1 Then Call BrandManager
If ThisWorkbook.Team = 0 Then Call CrossFunctionalTeam
End Sub

Sub BrandManager()
'Questionnaire format 1
Brand = 5
Question = 10
Columns(Brand).EntireColumn.Hidden = False
Rows(Question).EntireRow.Hidden = False

End Sub

Sub CrossFunctionalTeam()
'Questionnaire format 2
If ThisWorkbook.Team = 0 Then
Columns("E:AE").EntireColumn.Hidden = False
Rows("10:10").EntireRow.Hidden = False
ActiveSheet.Range("E10").Select
End If
End Sub

Sub NextButton()
'if a Brand Manager, cycle to the next column question until column AE is reached, then move to next row and show first question only
If ThisWorkbook.Team = 1 Then
If Columns("AE:AE").EntireColumn.Hidden = False Then
Columns("D:AE").EntireColumn.Hidden = True
Rows(Question).EntireRow.Hidden = True
Question = Question + 1
Rows(Question).EntireColumn.Hidden = False
Brand = 5
End If
If Columns("AE:AE").EntireColumn.Hidden = True Then
Columns(Brand).EntireColumn.Hidden = True
Brand = Brand + 1
Columns(Brand).EntireColumn.Hidden = False
End If
End If

'if a Cross Funtional team user, Next button should cycle to the next row of questions
If ThisWorkbook.Team = 0 Then
Question = 10
Rows(Question).EntireRow.Hidden = True
Question = Question + 1
Rows(Question).EntireRow.Hidden = False
End If

End Sub

1 个答案:

答案 0 :(得分:0)

您想对Range(Row)说些什么?说,Range(25)
如果我没有误解,根据你的代码,它必须看起来像:

Sub NextButton()
' ...
If Range("AE:AE").EntireColumn.Hidden = False Then
' ...
Rows(Row).Hidden = True ' Range(Row).EntireRow.Hidden 
Row = Row + 1
Columns(Row).Hidden = False ' Range(Row).EntireColumn.Hidden
' ...