Excel VBA:如何在相同长度的多个变量数组中循环并将它们转换为对象
我目前有3个独立的Excel VBA:如何在相同长度的多个变量数组中循环并将它们转换为对象,excel,vba,Excel,Vba,我目前有3个独立的变体,它们都是由13行和1列组成的数组。一个变量表示名称,另一个变量表示更改,最后一个变量表示出现次数。请参阅下面我的启动程序代码: Sub watchList() Dim names As Variant names = Sheets("Watch Calculations").Range("B4:B16") Dim changes As Variant changes = Sheets(&q
变体
,它们都是由13行和1列组成的数组。一个变量表示名称
,另一个变量表示更改
,最后一个变量表示出现次数
。请参阅下面我的启动程序代码:
Sub watchList()
Dim names As Variant
names = Sheets("Watch Calculations").Range("B4:B16")
Dim changes As Variant
changes = Sheets("Watch Calculations").Range("G4:G16")
Dim occurances As Variant
occurrences = Sheets("Watch Calculations").Range("G22:G34")
End Sub
Dim i As Integer
Dim MyArray(1 To 13) As Integer
For i = 1 To UBound(MyArray)
'psuedo code stars here
create new object of class counterParty
set object.Name = names(i,1)
set object.changeStatus = changes(i,1)
set object.negativeOccurences= occurrences.get(i,1)
add object to array of counterParty objects
Next i
我还有一个名为的类,包含以下字段:
Public Name As String
Public changeStatus As String
Public negativeOccurences As Integer
如何同时循环所有3个变量,并根据每个变量的行号将值输入到类的对象中。请参阅下面的psuedo代码:
Sub watchList()
Dim names As Variant
names = Sheets("Watch Calculations").Range("B4:B16")
Dim changes As Variant
changes = Sheets("Watch Calculations").Range("G4:G16")
Dim occurances As Variant
occurrences = Sheets("Watch Calculations").Range("G22:G34")
End Sub
Dim i As Integer
Dim MyArray(1 To 13) As Integer
For i = 1 To UBound(MyArray)
'psuedo code stars here
create new object of class counterParty
set object.Name = names(i,1)
set object.changeStatus = changes(i,1)
set object.negativeOccurences= occurrences.get(i,1)
add object to array of counterParty objects
Next i
试试这个
第一类模块:
Private pName As String
Private pchangeStatus As String
Private pnegativeOccurrences As Long
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(lName As String)
pName = lName
End Property
Public Property Get changeStatus() As String
changeStatus = pchangeStatus
End Property
Public Property Let changeStatus(lchangeStatus As String)
pchangeStatus = lchangeStatus
End Property
Public Property Get negativeOccurrences() As Long
negativeOccurrences = pnegativeOccurrences
End Property
Public Property Let negativeOccurrences(lnegativeOccurrences As Long)
pnegativeOccurrences = lnegativeOccurrences
End Property
然后,模块:
Dim names As Variant
names = Sheets("Watch Calculations").Range("B4:B16")
Dim changes As Variant
changes = Sheets("Watch Calculations").Range("G4:G16")
Dim occurrences As Variant
occurrences = Sheets("Watch Calculations").Range("G22:G34")
Dim i As Long
Dim clsarr(1 To 13) As Object 'You can use lbound and ubound on names() to get dynamic values
Dim mycls As Class1
For i = 1 To UBound(names)
Set mycls = New Class1 'Overwrite current object
'assign values to the class properties
mycls.Name = names(i, 1)
mycls.changeStatus = changes(i, 1)
mycls.negativeOccurrences = occurrences(i, 1)
Set clsarr(i) = mycls
Next i
试试这个
第一类模块:
Private pName As String
Private pchangeStatus As String
Private pnegativeOccurrences As Long
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(lName As String)
pName = lName
End Property
Public Property Get changeStatus() As String
changeStatus = pchangeStatus
End Property
Public Property Let changeStatus(lchangeStatus As String)
pchangeStatus = lchangeStatus
End Property
Public Property Get negativeOccurrences() As Long
negativeOccurrences = pnegativeOccurrences
End Property
Public Property Let negativeOccurrences(lnegativeOccurrences As Long)
pnegativeOccurrences = lnegativeOccurrences
End Property
然后,模块:
Dim names As Variant
names = Sheets("Watch Calculations").Range("B4:B16")
Dim changes As Variant
changes = Sheets("Watch Calculations").Range("G4:G16")
Dim occurrences As Variant
occurrences = Sheets("Watch Calculations").Range("G22:G34")
Dim i As Long
Dim clsarr(1 To 13) As Object 'You can use lbound and ubound on names() to get dynamic values
Dim mycls As Class1
For i = 1 To UBound(names)
Set mycls = New Class1 'Overwrite current object
'assign values to the class properties
mycls.Name = names(i, 1)
mycls.changeStatus = changes(i, 1)
mycls.negativeOccurrences = occurrences(i, 1)
Set clsarr(i) = mycls
Next i
当你写名称时。get(i)
你可能是指名称(i,1)
?@JohnSUN是的,这绝对是我的意思(抱歉混淆,我习惯java)这些变量(名称
,更改
,出现次数
)不都应该是类型吗?@Paul No,它存储数组中每个单元格的值,而不是设置范围。@Warcupine-范围也是对象的一种类型;通过将变量定义为范围()。使用变体会在代码中引入问题,因为VBA在某些情况下倾向于“假设”。当您编写名称时,get(i)
您可能指的是名称(i,1)
?@johnson是的,这绝对是我的意思(很抱歉,我习惯于java),这些变量不都应该吗(名称
,更改
,出现次数
)是否属于类型?@Paul否,它是在数组中存储每个单元格的值,而不是设置范围。@Warcupine-范围也是一种对象类型;通过将变量定义为范围()。使用变量会在代码中引入问题,因为VBA在某些情况下倾向于“假定”。