使用函数中的全局变量隐藏列

时间:2018-04-09 18:06:02

标签: excel vba variables

我想:

用户选择国家'国家/地区' sheet =这会设置全局变量' CountryVariable'在Module1中 其他页面将通过调用' CountryVariable'的Worksheet_Activate()函数根据此选择显示/隐藏列。代码如下。

我在创建全局变量并正确引用它时遇到问题。

任何人都可以帮忙进行此引用吗?

第1单元:

'define the variable
    Public CountryVariable As String

表("国家&#34):

'set variable CountryVariable on Module1 depneding on country selected, then move to next tab
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("K11:L20")) Is Nothing Then
            If ActiveCell.Value = "UK" Then
            CountryVariable = "H:AG"
            End If
            If ActiveCell.Value = "Brazil" Then
            CountryVariable.Value = "AI:AZ"
            End If
            If ActiveCell.Value = "India" Then
            CountryVariable.Value = "BB:CB"
            End If
            If ActiveCell.Value = "Indonesia" Then
            CountryVariable.Value = "CD:DH"
            End If
            If ActiveCell.Value = "Turkey" Then
            CountryVariable.Value = "DJ:EE"
            End If
        Sheets("Team").Visible = True
        Sheets("Country").Visible = False
        Sheets("Team").Activate
        Sheets("Team").Select
        Sheets("Team").Range("K9").Select
        End If
    End If
End Sub

示例标签:

 'on any sheet where this is present, the columns will be selected according to the chosen country
    Private Sub Worksheet_Activate()
    Call ResetButton1
    Call StartButton1
    ActiveSheet.Columns("H:FA").EntireColumn.Hidden = True
    ActiveSheet.Columns(CountryVariable).EntireColumn.Hidden = False
    ActiveSheet.Range("G9").Select
    ActiveWindow.Zoom = 80
    End Sub

2 个答案:

答案 0 :(得分:0)

不确定整件事情。目前有一个On Error Resume Next,好像CountryVariable被保留为vbNullString的初始值,它将会出错。

var principal = System.Threading.Thead.CurrentPrincipal;
var userName = principal.Identity.Name;

答案 1 :(得分:0)

试试这段代码: 将HideColumnsTeam Sub放在单独的模块中。 每次调用HideColumnsTeam sub时,都会传递列变量。

Sub HideColumnsTeam(ByVal CountryVariable As String)
    Call ResetButton1
    Call StartButton1
    ThisWorkbook.Worksheets("Team").Activate
    ThisWorkbook.Worksheets("Team").Columns("H:FA").EntireColumn.Hidden = True
ThisWorkbook.Worksheets("Team").Columns(CountryVariable).EntireColumn.Hidden = False
    ThisWorkbook.Worksheets("Team").Range("G9").Select
    ActiveWindow.Zoom = 80
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim CountryVariable As String

If Selection.Count = 1 Then
    If Not Intersect(Target, Range("K11:L20")) Is Nothing Then
        If ActiveCell.Value = "UK" Then
        CountryVariable = "H:AG"
        End If
        If ActiveCell.Value = "Brazil" Then
        CountryVariable = "AI:AZ"
        End If
        If ActiveCell.Value = "India" Then
        CountryVariable = "BB:CB"
        End If
        If ActiveCell.Value = "Indonesia" Then
        CountryVariable = "CD:DH"
        End If
        If ActiveCell.Value = "Turkey" Then
        CountryVariable = "DJ:EE"
        End If
    If CountryVariable <> "" Then
        Sheets("Team").Visible = True
        Sheets("Country").Visible = False
        Call HideColumnsTeam(CountryVariable)
    End If
    Stop
End If
End If
End Sub