VBA For循环初始化工作在一个sub,几乎相同的sub不工作

时间:2017-12-01 01:27:42

标签: vba for-loop initialization

我试图通过查找标题名称然后隐藏这些列来遍历VBA中的列标题(可能会被移动)。 隐藏部分工作正常,但我决定做一个简单的'取消隐藏'并抛出一个错误: 运行时错误'92':For loop not initialized。

我唯一改变的是我是否隐藏当前选择(是或否)。我尝试更改变量名称,认为可能通过在子站点之间共享它们而变得混乱,但这没有帮助。

WORKING'clide'子代码如下:

Public Sub Activate_Print_Mode()

 Dim SearchArray() As Variant SearchArray = Array("ISBN", "Sub Title",
 "Paper Cut Off", "Despatch Date (ExW)", "Printer Location", "UK WH
 ETA", "Suggested Pub ExW", "Suggested Pub ExUK", "INDENT / STATUS",
 "UK VAT Price", "FX", "GB Net Price", "AU Price + Freight", "S/A",
 "Discount", "PRICE NOTES", "ORDERED", "Budget Value", "Misc Specs")

 Dim element As Variant

 For Each element In SearchArray
     ' perform search, hide column
     'counter = counter + 1
     Rows("3:3").Select
     Selection.Find(What:=element, After:=ActiveCell, LookIn:=xlFormulas, _
         LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
         MatchCase:=True, SearchFormat:=False).Activate
     ActiveCell.Offset(-1, 0).Select


     Selection.EntireColumn.Hidden = True


     Next element
      ActiveSheet.Range("A1").Select

 End Sub

非工作'show'子项如下:

 Public Sub DeActivate_Print_Mode()

 Dim DispSearchArray() As Variant SearchArray = Array("ISBN", "Sub
 Title", "Paper Cut Off", "Despatch Date (ExW)", "Printer Location",
 "UK WH ETA", "Suggested Pub ExW", "Suggested Pub ExUK", "INDENT /
 STATUS", "UK VAT Price", "FX", "GB Net Price", "AU Price + Freight",
 "S/A", "Discount", "PRICE NOTES", "ORDERED", "Budget Value", "Misc
 Specs")

 Dim DispElement As Variant


 For Each DispElement In DispSearchArray
     ' perform search, hide column
     'counter = counter + 1
     Rows("3:3").Select
     Selection.Find(What:=element, After:=ActiveCell, LookIn:=xlFormulas, _
         LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
         MatchCase:=True, SearchFormat:=False).Activate
     ActiveCell.Offset(-1, 0).Select
     Selection.EntireColumn.Hidden = False

     Next DispElement
      ActiveSheet.Range("A1").Select

 End Sub

是什么给出了?

2 个答案:

答案 0 :(得分:0)

Find()找不到隐藏列/行中的值

我会做这样的事(未经测试)

Sub hidethem()
    Set_Print_Mode True
End Sub
Sub showthem()
    Set_Print_Mode False
End Sub

Public Sub Set_Print_Mode(doHide As Boolean)

    Dim SearchArray As Variant
    Dim m, rng As Range, sht As Worksheet
    Dim element As Variant

    Set sht = ActiveSheet

    SearchArray = Array("ISBN", "Sub Title", _
    "Paper Cut Off", "Despatch Date (ExW)", "Printer Location", "UK WHETA", _
    "Suggested Pub ExW", "Suggested Pub ExUK", "INDENT / STATUS", _
    "UK VAT Price", "FX", "GB Net Price", "AU Price + Freight", "S/A", _
    "Discount", "PRICE NOTES", "ORDERED", "Budget Value", "Misc Specs")

    For Each element In SearchArray
        m = Application.Match(element, sht.Rows(3), 0)
        If Not IsError(m) Then
            If rng Is Nothing Then
                Set rng = sht.Columns(m)
            Else
                Set rng = Application.Union(rng, sht.Columns(m))
            End If
        End If
    Next element

    If Not rng Is Nothing Then rng.Hidden = doHide

    sht.Range("A1").Select

 End Sub

通过为标题行添加参数,为标题数组添加一个参数,可以使其更加通用......

答案 1 :(得分:0)

这对我有用:

Public Sub UnHideColumns()

Dim ws As Worksheet
Dim lastCell As Integer
Dim DispSearchArray() As Variant

Set ws = Sheets("Sheet1")
lastCell = ws.Range("1:1").End(xlToRight).Column
DispSearchArray = Array("ISBN", "Sub Title", "Paper Cut Off", "Despatch Date (ExW)", "Printer Location", "UK WH ETA", "Suggested Pub ExW", "Suggested Pub ExUK", "INDENT / STATUS", "UK VAT Price", "FX", "GB Net Price", "AU Price + Freight", "S/A", "Discount", "PRICE NOTES", "ORDERED", "Budget Value", "Misc Specs")

For Each Header In DispSearchArray
    For j = 1 To lastCell
        If Cells(1, j) = Header Then
            ws.Cells(1, j).Select
            Selection.EntireColumn.Hidden = False
        End If
    Next j
Next Header

Set ws = Nothing

End Sub

我觉得它更透明,但那只是我。试一试!