简体   繁体   English

Vba循环通过一个数组

[英]Vba Loop through an array

I was looking for the answers for some time already, but I keep having the different errors whatever I try. 我已经有一段时间寻找答案,但无论我尝试什么,我都会遇到不同的错误。

I had working code: 我有工作代码:

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

For Each x In arkwyn
....
Next

But I need to use similar approach in more complex worksheet. 但我需要在更复杂的工作表中使用类似的方法。 In addition it needs to be quick as it will be the part of more complex Update procedure that constantly keeps track of many fields in the worksheet. 此外,它需要快速,因为它将是更复杂的更新过程的一部分,不断跟踪工作表中的许多字段。

Could you please take a look and help me to do it properly? 你能看看并帮我做好吗?

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

Thx for the answer, I tried to implement it further and I created sth like this: 谢谢答案,我试图进一步实现它,我创建了这样的:

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

The idea is that the cell value is supposed to unhide certain number of rows. 我们的想法是,单元格值应该取消隐藏一定数量的行。 I keep getting "Run time error 91: Object variable or With block variable not set" Where do I make mistake? 我一直得到“运行时错误91:对象变量或没有设置块变量”我在哪里弄错了?

EDIT - fixed types and Rowno function 编辑 - 固定类型和Rowno功能

'Return the row for a given value, or zero if not found
Private Function RowNo(ByVal text1 As String) As Long
    Dim f As Range
    Set f = Columns(2).Find(text1, Lookat:=xlWhole)
    If Not f Is Nothing Then
        RowNo = f.Row
    Else
        RowNo = 0
    End If
End Function

'...
Dim t1r As Variant 
Dim t1 As Long, r As Long

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)
    r = RowNo(t1r(t1))
    If r > 0 Then
        Rows(r + 1).Hidden = (UCase(Cells(r, 3).Value)<>"X")
    Else
        Debug.Print "'" & t1r(t1) & "' was not found!"
    End If
Next t1
'...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM