在Range属性

时间:2017-11-17 21:14:31

标签: excel vba excel-vba show-hide

我有大约236个命名范围(列)用于大型数据表。尝试拆分分隔命名范围的长代码行时出现此错误:

  

运行时错误'1004'应用程序定义的错误或对象定义的错误

E.g:

Worksheets("Sheet1").Range("foo1,foo2" _
    & "foo3,foo4" _
    & "..." _
    & "foo235,foo236")

我正在尝试根据特定条件(命名范围)过滤和取消过滤列。一切似乎工作正常(对于只有1行长度的小字符串),直到我必须将代码分成多行,因为它到达窗口的末尾..

代码 -

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$B$3" Then
        Worksheets("Sheet1").Range("Fruit," _
            & "Months,Colour").EntireColumn.Hidden = Target.Value = "CustomView"
    End If

    If Target.Address = "$B$3" Then
        Worksheets("Sheet1").Range("Colour,Number" _
            & "Months").EntireColumn.Hidden = Target.Value = "Custom2View"
    End If
End Sub

此代码似乎不起作用。我认为它与引号和excel如何读取有关,但我还没有找到修复程序。

根据评论中的建议进行测试的新代码导致错误运行时错误“1004”应用程序定义或对象定义错误

Private Sub Worksheet_Change(ByVal Target As Range)

Dim arr, i As Long, rng As Range

If Target.Address = "$B$3" Then
    arr = Split("foo1,foo2,foo3,...,foo266,foo267", ",")
 Set rng = Worksheets("Database").Range(arr(0))
 For i = 1 To UBound(arr)
 Set rng = Application.Union(rng, Worksheets("Database").Range(arr(i)))
 Next i
 rng.EntireColumn.Hidden = (Target.Value = "CustomView")
End If

End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用Application.Union建立一个范围,然后一次隐藏/显示该范围。

编辑:基于你的第二个共享文件,我认为你需要这样的东西。您以前的代码没有检查“视图名称”单元格的值,并且正在应用所有视图,留下最后一个...

例如:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim arr, q As Long, rng As Range, sht As Worksheet

    Set sht = Worksheets("Database")

    If Target.Address = "$B$3" Then

        'unhide all columns forst
        sht.UsedRange.EntireColumn.Hidden = False

        Select Case Target.Value
            Case "CustomView"
                arr = Split("A,B,C_,X,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM," & _
                  "AN,AO,AP,AQ,AR,AS,AT,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG," & _
                  "BH,BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ,CA," & _
                  "CB,CC,CD,CE,CF,CG,CH,CI,CJ,CK,CL,CU,CV,CW,CX,CY,CZ,DA,DB,DC", ",")
            Case "XX100View"
                arr = Split("D,E,F,G,X,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO," & _
                   "AP,AQ,AR,AS,AT,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG,BH,BI,BJ," & _
                   "BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ,CA,CB,CC,CD,CE," & _
                   "CF,CG,CH,CI,CJ,CK,CL,CU,CV,CW,CX,CY,CZ,DA,DB,DC", ",")
            Case "OtherView"
                arr = Split("A,B,D,E,F,G,H,I,X,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM," & _
                    "AN,AO,AP,AQ,AR,AS,AT,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG,BH," & _
                    "BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ,CA,CB," & _
                    "CC,CD,CE,CF,CG,CH,CI,CJ,CK,CL,CU,CV,CW,CX,CY,CZ,DA,DB,DC", ",")
        End Select

        If Not IsEmpty(arr) Then
            Set rng = sht.Range(arr(0))
            For q = 1 To UBound(arr)
                Set rng = Application.Union(rng, sht.Range(arr(q)))
            Next q
            rng.EntireColumn.Hidden = True '<<edited
        End If 'got a view

    End If 'is view name cell
End Sub

PS - 您的范围名称不需要包含您的所有数据:单个单元格可以正常使用,因为您使用EntireColumn将其扩展到整个工作表高度。