如何循环移位的阵列上VBA [英] How to Shift an array circularly on VBA
本文介绍了如何循环移位的阵列上VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有阵列(1,2,3),我愿做一个循环移位,以获得例如(3,1,2)或(2,3,1)。在Matlab中,我知道怎么做,用下面的code:
Y = circshift(A,K)
u能请帮助我完成这个任务?我倒是AP preciate任何样品$ C $ ... CS
解决方案
显式的选项
选项基本1子shiftCircArray()
昏暗iInputArray(3)作为整数
iInputArray(1)= 1
iInputArray(2)= 2
iInputArray(3)= 3
昏暗iArray2()作为整数
iArray2 = RotateArrayRight(iInputArray)
结束小组功能RotateArrayRight(ArrayToRotate)
昏暗objNewArray()作为整数,iOldArrayPos整数,iNewArrayPos整数,iArrayLength
作为整数
昏暗iPlacesToRotate作为整数
检查要被处理的阵列已初始化
iPlacesToRotate = Application.CountA(ArrayToRotate)
如果iPlacesToRotate<> 0,则
检查的地方以旋转数大于零 - 运行
具有值功能
'的地方转动小于零会引起一些问题,并可能导致
'功能崩溃
如果iPlacesToRotate> 0,则
获取数组旋转的长度,我们将使用它几次这样会
它加载到一个局部变量的函数的开始
iArrayLength = Application.CountA(ArrayToRotate)
阵列从0被初始化为ArrayLength -1
所以它将包含ArrayLength元素
使用ReDim objNewArray(iArrayLength)
这将通过数组中删除任何额外的旋转完成
Mod运算符返回一个整数除法运算的余
'初始化数组的索引位置
iOldArrayPos = iPlacesToRotate
iNewArrayPos = 1
复制从一个对象数组下
首先开始于iPlacesToRotate到旧数组
并复制到新阵列的开始。
虽然iOldArrayPos< iArrayLength + 1
objNewArray(iNewArrayPos)= ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
WEND
iOldArrayPos = 1
从旧阵列的开始复制到的结束
新阵
虽然iOldArrayPos< iPlacesToRotate
objNewArray(iNewArrayPos)= ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
WEND
其他
MSGBOX(值地旋转必须大于零。)
万一
其他
MSGBOX(不能旋转的空数组。)
万一
RotateArrayRight = objNewArray()
结束功能
I have the array (1,2,3) and I would like to do a circular shift in order to obtain for example (3,1,2) or (2,3,1). In Matlab I know how to do that using the following code:
Y = circshift(A,K)
Can u please help me with this task? I'd appreciate any sample codes...
解决方案
Option Explicit
Option Base 1
Sub shiftCircArray()
Dim iInputArray(3) As Integer
iInputArray(1) = 1
iInputArray(2) = 2
iInputArray(3) = 3
Dim iArray2() As Integer
iArray2 = RotateArrayRight(iInputArray)
End Sub
Function RotateArrayRight(ArrayToRotate)
Dim objNewArray() As Integer, iOldArrayPos As Integer, iNewArrayPos As Integer, iArrayLength
As Integer
Dim iPlacesToRotate As Integer
' Check that the array to be processed has been initialized
iPlacesToRotate = Application.CountA(ArrayToRotate)
If iPlacesToRotate <> 0 Then
' Check that the number of places to rotate is greater than zero - running the
' function with a value
' of places to rotate which is less than zero would cause problems, possibly causing
'the function to crash
If iPlacesToRotate > 0 Then
' get the length of the array to rotate, we'll be using it a few times so will
' load it into a local variable at the start of the function
iArrayLength = Application.CountA(ArrayToRotate)
' Array will be initialised from 0 to ArrayLength -1
' so it will contain ArrayLength elements
ReDim objNewArray(iArrayLength)
' This will remove any extra complete rotations through the array
' The mod operator returns the remainder of an integer divide operation
' Initialise the array position indexes
iOldArrayPos = iPlacesToRotate
iNewArrayPos = 1
' Copy objects from one array to the next
' First start at iPlacesToRotate into the old array
' and copy to the start of the new array.
While iOldArrayPos < iArrayLength + 1
objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
Wend
iOldArrayPos = 1
' Copy from the start of the old array into the end of the
' new array
While iOldArrayPos < iPlacesToRotate
objNewArray(iNewArrayPos) = ArrayToRotate(iOldArrayPos)
iOldArrayPos = iOldArrayPos + 1
iNewArrayPos = iNewArrayPos + 1
Wend
Else
MsgBox ("Values for 'Places to Rotate' must be greater than zero.")
End If
Else
MsgBox ("Cannot rotate an null array.")
End If
RotateArrayRight = objNewArray()
End Function
这篇关于如何循环移位的阵列上VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文