Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vba中遍历类的属性_Vba_Excel - Fatal编程技术网

在vba中遍历类的属性

在vba中遍历类的属性,vba,excel,Vba,Excel,我创建了这个类 Interval.cls public x as new collection public y as new collection public z as string 我希望遍历这些属性,因为我希望用户在表单中选择输入x,y,z,以便在我的sub sub test() dim inter as new Intervals inter.x.add "a" inter.x.add "a" inter.x.add "b" inter.x.add "b" inter.x.add

我创建了这个类

Interval.cls
public x as new collection
public y as new collection
public z as string
我希望遍历这些属性,因为我希望用户在表单中选择输入x,y,z,以便在我的sub

sub test()

dim inter as new Intervals

inter.x.add "a"
inter.x.add "a"
inter.x.add "b"
inter.x.add "b"
inter.x.add "b"

userString1 = x
userString2 = a
我想让它变得动态,这样无论用户想要什么,我都可以提供结果。 '我只是想使比较userString和我的属性成为可能

for each i in inter
'所以我希望这个循环中的i是我的属性x,y,z,这样我就可以做if语句了 如果(i=userString1),则

我知道我可以在课堂上做一个小动作,让它变得更容易,我不知道怎么做

感谢您在课堂上给予的任何帮助
'in class
Public Property Get Item(i As Integer) As Variant

   Select Case ndx
      Case 1: Item = Me.x
      Case 2: Item = Me.y
      Case 3: Item = Me.z
   End Select

End Property

'in sub
Dim c as Collection
Dim s as String

For i = 1 to 3
   if i < 3 then 
      set c = inter.Item(i)
      'iterate through collection
   else
      s = inter.Item(i)
   end if
next i
公共属性获取项(i作为整数)作为变量 选择案例ndx 案例1:Item=Me.x 案例2:Item=Me.y 案例3:Item=Me.z 结束选择 端属性 "在潜艇上, Dim c作为集合 像线一样变暗 对于i=1到3 如果我小于3,那么 集合c=中间项目(i) '遍历集合 其他的 s=中间项目(i) 如果结束 接下来我
这可能是最简单的方法,我没有测试它,但希望它至少能让你开始

这是你想要的吗

'in class
Public x As New Collection
Public y As New Collection
Public z As String
Public Property Get Item(i As Integer) As Variant

   Select Case i
      Case 1: Item = Me.x
      Case 2: Item = Me.y
      Case 3: Item = Me.z
   End Select

End Property

Sub try()

Dim userString2 As String
Dim userString1 As String
Dim inter As Interval
Dim i As Integer

Set inter = New Interval

inter.x.Add "a"
inter.x.Add "a"
inter.x.Add "b"
inter.x.Add "b"
inter.x.Add "b"

userString2 = "aabbb"

For i = 1 To inter.x.Count

       userString1 = userString1 & inter.x.Item(i)
Next i

 If userString1 = userString2 Then
 '<do whatever>
 End If
End Sub

首先使用
选项Explicit.
您的代码与现在没有任何意义,当您定义
userString1
时,它实际上没有设置任何内容,因此您的比较永远不会为真,即使您的类支持它,当你使用
interval
时,你的定义也没有意义。…@endrland为什么没有意义,只是想简单一点。我有一个具有a、b、c、d、e、f、g属性的类,我有一个用户输入,告诉我a、b或c中某个值的百分比。因此,我遍历该属性并找到结果。通过在属性中进行一个循环,查看哪一个与输入匹配。因此,当用户字符串是a时,我希望通过循环themNice找到属性a,我将测试它并返回!不起作用,它表示c的类型不匹配,当我尝试添加一个手表并检查intt.item(1)时,它表示为空,但我可以在手表中看到intt,它看起来好像在intercollection中有i=1到3的每个intt的数据,如果i<3,则设置c=intt.item(i)'遍历集合Else s=intt.Item(i)End If Next i Next inttsorry,不确定昨天它是如何出错的,但今天它工作正常,我只需要将c更改为一个对象,而不是其他任何东西。谢谢伟大的很高兴它为你解决了:)想选择它作为答案吗?但是你显式地指定了属性x inter.x,这不是get item(i)获取属性x,y,z而不显式地声明它们的全部目的,这样我就可以每次循环通过x,y,z,并且如果将来我有超过x,y,z的属性,就不必更改代码。正确的?
'in class
Public x As New Collection
Public y As New Collection
Public z As String
Public Property Get Item(i As Integer) As Variant

   Select Case i
      Case 1: Item = Me.x
      Case 2: Item = Me.y
      Case 3: Item = Me.z
   End Select

End Property

Sub try()

Dim userString2 As String
Dim userString1 As String
Dim inter As Interval
Dim i As Integer

Set inter = New Interval

inter.x.Add "a"
inter.x.Add "a"
inter.x.Add "b"
inter.x.Add "b"
inter.x.Add "b"

userString2 = "aabbb"

For i = 1 To inter.x.Count

       userString1 = userString1 & inter.x.Item(i)
Next i

 If userString1 = userString2 Then
 '<do whatever>
 End If
End Sub
Sub tryWithArray(ByRef StringArray() As String)

Dim userString2 As String
Dim i As Integer

userString2 = "b"


For i = 1 To UBound(StringArray())
    If userString2 = StringArray(i) Then
        'do something
    End If
Next i

End Sub