Arrays 将数组作为函数输入VBA传递
经过一些试验和研究后,我无法想出如何将数组作为函数的输入进行传递 这是我的代码:Arrays 将数组作为函数输入VBA传递,arrays,vba,input,Arrays,Vba,Input,经过一些试验和研究后,我无法想出如何将数组作为函数的输入进行传递 这是我的代码: Function read_coordinates() '''This function reads the coordinates (X,Y) of the machines in the sheet !layout and stores them in an N*2 array number_of_machines = Count_WT() 'I use an temporary array since I
Function read_coordinates()
'''This function reads the coordinates (X,Y) of the machines in the sheet !layout and stores them in an N*2 array
number_of_machines = Count_WT()
'I use an temporary array since I cannot manage to define well the array_read_coordinates.
ReDim WTG_coord(1 To number_of_machines, 1 To 2) As Double
For i = 1 To number_of_machines
WTG_coord(i, 1) = Application.ThisWorkbook.Worksheets("Layout").Cells(5 + i, "H").Value
WTG_coord(i, 2) = Application.ThisWorkbook.Worksheets("Layout").Cells(5 + i, "I").Value
Next
read_coordinates = WTG_coord
Set WTG_coor = Nothing
End Function
现在,我有另一个函数,我想用它来改变旋转后的坐标值:
Function Rotate_coordinate(coord_system() As Variant, theta As Long)
''' This function determine the coordinates of the machines after a rotation of an angle of 270 - theta
'coord_system is the array of the initial coordinates of the machines
'theta is the angle of view
'We define the rotation matrix for the angle 270-theta
Dim M(1 To 2, 1 To 2) As Double
M(1, 1) = -Sin(theta)
M(1, 2) = Cos(theta)
M(2, 1) = -Cos(theta)
M(2, 2) = -Sin(theta)
x = UBound(coord_system, 1) 'We read the length of the matrix (ie get the number of WTG)
Dim Rotate_coordinate(1 To x, 1 To 2)
For i = 1 To x
Rotate_coordinate(i, 1) = M(1, 1) * coord_system(i, 1) + M(1, 2) * coord_system(i, 2)
Rotate_coordinate(i, 2) = M(2, 1) * coord_system(i, 1) + M(2, 2) * coord_system(i, 2)
Next
End Function
我想将此函数用作通用函数,因为我以后将不得不将其用于多个coord_系统
提前谢谢你的建议 应该使用ByRef传递函数、子函数或属性中的数组
声明:
Function MyFunction (ByRef MyArray() As long)
'Code goes here
End Function
我使用标准右手旋转,以便根据输入返回新的旋转数组:
Public Function RotateCoordinatesDegree(ByRef Coords() As Double, ByVal angle_deg As Double) As Double()
Dim rot() As Double, cx As Double, sx As Double, x As Double
Dim i As Long, N As Long
N = UBound(Coords, 1)
ReDim rot(1 To N, 1 to 2)
x = WorksheetFunction.Radians(angle_deg)
sx = Sin(x): cx = Cos(x)
For i = 1 To N
rot(i, 1) = Coords(i, 1) * cx - Coords(i, 2) * sx
rot(i, 2) = Coords(i, 1) * sx + Coords(i, 2) * cx
Next i
RotateCoordinates = rot
End Function
Public Sub TestCoord()
Dim cs1() As Double, cs2() As Double, theta, i As Long
ReDim cs1(1 To 10, 1 To 2)
For i = 1 To 10
cs1(i, 1) = CDbl(i) / 10#: cs1(i, 2) = 1#
Next i
theta = 15
cs2 = RotateCoordinatesDegree(cs1, 270 - theta)
End Sub
编辑1
如果只想修改现有坐标,请尝试以下操作
Public Sub RotateCoordinatesDegree(ByRef Coords() As Double, ByVal angle_deg As Double)
Dim cx As Double, sx As Double, ang As Double, x as Double, y as Double
Dim i As Long, N As Long
N = UBound(Coords, 1)
ang = WorksheetFunction.Radians(angle_deg)
sx = Sin(ang): cx = Cos(ang)
For i = 1 To N
x = Coords(i, 1) * cx - Coords(i, 2) * sx
y = Coords(i, 1) * sx + Coords(i, 2) * cx
Coords(i,1) = x : Coords(i,2) = y
Next i
End Sub
Public Sub TestCoord()
Dim cs1() As Double, theta, i As Long
ReDim cs1(1 To 10, 1 To 2)
For i = 1 To 10
cs1(i, 1) = CDbl(i) / 10#: cs1(i, 2) = 1#
Next i
theta = 15
RotateCoordinatesDegree cs1, 270 - theta
End Sub
我认为你需要θ作为双倍体来正确使用Cos和Sin。你还需要将度转换成弧度。在你的代码中,你写的是Rotate_坐标,但是你永远不会在函数之外使用它。你的意思是在最后写coord_system=Rotate_coordinate吗?参见我关于如何从函数返回数组的回答。否则,您需要一个Sub来修改现有数组。是的,我打算在最后的Sub中使用这些函数。我现在无法访问我的代码,但我会尽快尝试您的答案。我的意思是,如果您想更新coords的内容而不是声明函数,则需要将Sub Rotate_coordinateByRef coords设置为Double。在VBA中未指定ByRef时,会暗示ByRef。我测试了您的代码,但仍然存在无法解释的问题。在您的第一个代码中,在sub TestCoord中,我无法获得cs2的值。。例如,Debug.Print cs21,1生成错误消息:错误9:下标超出范围。如果您无法解释,我们将无法帮助您。阅读一些关于如何解释你的问题的提示。另一个问题是我无法管理管道指令。例如,我使用第一个函数读取坐标,所以我写coord1=read_坐标。如果我将坐标1作为rotateCoordesgree的输入传递,那么它就不起作用了。相反,我必须创建一个新的数组coord2,并复制coord2i,j=coord1i,jt的值,以将其作为rotateCoordesgree的输入传递。你知道为什么吗?请通过添加额外的要求和你想做的事情的示例来编辑你的问题。比如cs=旋转坐标系,270-θ,然后cs=旋转坐标系,270-psi?除非我看到你有问题的代码,否则我真的不能评论,