希望在excel VBA中创建坐标数组
我想在excel中创建一个变量,即坐标数组,这样我就可以使用.x和.y引用x或y坐标值数组 乙二醇 昨天很久了,今天早上一个小时了,我一直试图在谷歌上研究坐标阵列,但一无所获。也一直在尝试一个属性语句,但也一无所获 如果可以的话,谁能告诉我怎么做希望在excel VBA中创建坐标数组,vba,excel,Vba,Excel,我想在excel中创建一个变量,即坐标数组,这样我就可以使用.x和.y引用x或y坐标值数组 乙二醇 昨天很久了,今天早上一个小时了,我一直试图在谷歌上研究坐标阵列,但一无所获。也一直在尝试一个属性语句,但也一无所获 如果可以的话,谁能告诉我怎么做 谢谢这里有一个使用集合的选项: Sub Cooordindates() Dim Coords As Collection Set Coords = New Collection Coords.Add Array(1, 2,
谢谢这里有一个使用
集合的选项
:
Sub Cooordindates()
Dim Coords As Collection
Set Coords = New Collection
Coords.Add Array(1, 2, 3, 4, 5), "x"
Coords.Add Array(2, 4, 6, 8, 10), "y"
Debug.Print Coords.Item("x")(0), Coords.Item("y")(0) '~~> Prints: 1 2
End Sub
接受一个.Add
和项
。在这里指定数组和键键
- 您可以使用索引在
和x
中访问数组y
'确定多维数组是否已分配(或为空)
'适用于任何维度数组,即使是一维数组
公共函数isArrayAllocated(ByVal aArray作为变量)作为布尔值
出错时继续下一步
isArrayAllocated=IsArray(aArray)而不是iSeries(lBond(aArray,1))和lBond(aArray,1)如何使用它?您想要一个同时保存x
和y
坐标的单一数据结构吗?我想是的;首先,我只需要一个包含坐标列表的变量,例如,它可以有100对,这样我就可以引用一个单独的值,比如coords(98).x等等;或者我可能希望成对地操作列表,coords(98)=coords(05),从而一次性更改一对的x和y值。我可以用一个多维数组以一种不那么简单的方式来实现这一点,但是为了避免在我的例程中迷失方向,当我想要操纵y时,我会创建变量,这些变量被明确定义为带有.x和.y属性的坐标。谢谢,这对于创建一个x和y坐标列表很有帮助,但是当我尝试操作这些值时,什么也不会发生,例如在debug.print之前放置Coords.Item(“x”)(0)=Coords.Item(“y”)(1)要将打印更改为4,2您不能直接编辑集合中某个项的值。请参阅以寻求帮助。
Sub Cooordindates()
Dim Coords As Collection
Set Coords = New Collection
Coords.Add Array(1, 2, 3, 4, 5), "x"
Coords.Add Array(2, 4, 6, 8, 10), "y"
Debug.Print Coords.Item("x")(0), Coords.Item("y")(0) '~~> Prints: 1 2
End Sub
'To determine if a multi-dimension array is allocated (or empty)
'Works for any-dimension arrays, even one-dimension arrays
Public Function isArrayAllocated(ByVal aArray As Variant) As Boolean
On Error Resume Next
isArrayAllocated = IsArray(aArray) And Not IsError(LBound(aArray, 1)) And LBound(aArray, 1) <= UBound(aArray, 1)
Err.Clear: On Error GoTo 0
End Function
'To determine the number of dimensions of an array
'Returns -1 if there is an error
Public Function nbrDimensions(ByVal aArray As Variant) As Long
Dim x As Long, tmpVal As Long
If Not IsArray(aArray) Then
nbrDimensions = -1
Exit Function
End If
On Error GoTo finalDimension
For x = 1 To 65536 'Maximum number of dimensions (size limit) for an array that will work with worksheets under Excel VBA
tmpVal = LBound(aArray, x)
Next x
finalDimension:
nbrDimensions = x - 1
Err.Clear: On Error GoTo 0
End Function
'*****************************************************************************************************************************
'To return an array containing al the coordinates from a specified two-dimension array that have the searched item as value
'Returns an empty array if there is an error or no data
'Returns coordinates in the form of x,y
'*****************************************************************************************************************************
Public Function makeArrayFoundXYIn2DimArray(ByVal itemSearched As Variant, ByVal aArray As Variant) As Variant
Dim tmpArr As Variant, x As Long, y As Long, z As Long
tmpArr = Array()
If IsArray(aArray) Then
If isArrayAllocated(aArray) And nbrDimensions(aArray) = 2 Then
z = 0
For x = LBound(aArray, 1) To UBound(aArray, 1)
For y = LBound(aArray, 2) To UBound(aArray, 2)
If itemSearched = aArray(x, y) Then
If z = 0 Then
ReDim tmpArr(0 To 0)
Else
ReDim Preserve tmpArr(0 To UBound(tmpArr) + 1)
End If
tmpArr(z) = CStr(x) + "," + CStr(y)
z = z + 1
End If
Next y
Next x
End If
End If
makeArrayFoundXYIn2DimArray = tmpArr
Erase tmpArr
End Function