Vba循环通过一个数组

时间:2015-10-22 18:37:17

标签: excel-vba vba excel

我已经找了一段时间的答案,但无论我尝试什么,我都会遇到不同的错误。

我有工作代码:

Dim arkwyn As Variant
arkwyn = Array(1, 2, 3, "stats-obl")

For Each x In arkwyn
....
Next

但我需要在更复杂的工作表中使用类似的方法。此外,它需要快速,因为它将是更复杂的更新过程的一部分,不断跟踪工作表中的许多字段。

请你看看并帮我正确地做到这一点?

Private Function RowNo(ByVal text1 As String)
    RowNo = Columns(2).Find(text1, Lookat:=xlWhole).Row
End Function

Dim t1r As Variant 
Dim t1 As Integer

t1r = Array("1.2", "1.3", "1.4", "1.5", "1.6.2", "1.8", "1.9", "1.13.1.1", _
"1.13.1.2", "1.13.2")

For t1 = LBound(t1r) To UBound(t1r)
    Select Case UCase(Cells(RowNo(t1), 3).Value)
        Case "x"
            Rows(RowNo(t1) + 1).Hidden = False
        Case Else
            Rows(RowNo(t1) + 1).Hidden = True
    End Select
Next t1

答案的答案,我试图进一步实施它,我创建了这样的事情:

Dim ColAn As Long           
ColAn = 4
Dim YtQ1Ar As Variant       
Dim Y1q, rY1q As Long        


YtQ1Ar = Array("1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.7.1", "1.7.2", _
"1.7.3", "1.7.4", "1.7.5", "1.7.6", "1.7.7", "1.7.8", "1.7.9", "1.7.10", "1.7.11")

    For Y1q = LBound(YtQ1Ar) To UBound(YtQ1Ar)
        rY1q = RowNo(YtQ1Ar(Y1q))
        Rows(rY1q).Hidden = (UCase(Cells(RowNo("1."), ColAn).Value) <> "TAK")
    Next Y1q

这个想法是单元格值应该取消隐藏一定数量的行。我一直得到“运行时错误91:对象变量或没有设置块变量” 我在哪里弄错了?

1 个答案:

答案 0 :(得分:2)

编辑 - 固定类型和firstname.lastname@company.com功能

Rowno