将整数传递到Excel VBA中的子例程时发生ByRef错误
我试图使用在StackOverflow上其他地方找到的名为将整数传递到Excel VBA中的子例程时发生ByRef错误,excel,vba,Excel,Vba,我试图使用在StackOverflow上其他地方找到的名为Quicksort的代码,这是一种对数组元素进行排序的方法 当我将整数值传递给快速排序子例程时,会出现ByRef错误 我的代码当前为: (在主子程序中) (在单独的模块中) 此时,我在ByVal intRowtoSort As Integer行上得到一个错误,它说Compile error:ByRef参数类型不匹配 有人能解释这件事吗?如果删除第二个参数(intRowtoSort),则不会发生错误 ******编辑****** 下面是一些
Quicksort
的代码,这是一种对数组元素进行排序的方法
当我将整数值传递给快速排序
子例程时,会出现ByRef
错误
我的代码当前为:
(在主子程序中)
(在单独的模块中)
此时,我在ByVal intRowtoSort As Integer
行上得到一个错误,它说Compile error:ByRef参数类型不匹配
有人能解释这件事吗?如果删除第二个参数(intRowtoSort),则不会发生错误
******编辑******
下面是一些注释,下面是代码的完整部分
ReDim arrDataToSort(1 To 3, 2 To countRows) As Variant 'declare an array to be used to sort the data - don't know dimensions yet, so make too big...
'done the above as transpose as can only ReDim second dimension of an array...
Dim intCol As Integer: intCol = 2 'defines the next blank col in arrDataToSort
For i = 2 To 1000 'countRows
If formulaarray1(i, uniqueCOL) = 1 Then 'i.e. unique patient ID
arrDataToSort(1, intCol) = Sheets(sheet_DATA).Cells(i, IDCOL).Value
arrDataToSort(2, intCol) = Sheets(sheet_DATA).Cells(i, FreqCOL).Value
arrDataToSort(3, intCol) = Sheets(sheet_DATA).Cells(i, FreqCOL2).Value
intCol = intCol + 1 'increment to identify next blank row
End If
Next i
ReDim Preserve arrDataToSort(1 To 3, 2 To intCol - 1) 'respecify the size of the array whilst preserving the data
'attempt to sort the data by the UniqueFreqCOL values using QuickSort subroutine
Call subQuickSort(arrDataToSort,2)
补充意见:
- 我还设法在函数的第二个参数被删除的情况下得到了相同的ByRef错误,所以问题似乎出在我的数组上
- 我正在使用
而不使用先前的ReDim
,因为这允许我使用一个变量来指定数组的维度-我现在从所做的注释中了解到我不需要这样做:)Dim
选项显式
公共子快速排序(ByRef var1作为变体)
Dim intRowtoSort作为整数:intRowtoSort=2
Dim varPivot作为变体
暗发光尽可能长,高发光尽可能长,低发光开始尽可能长,高发光开始尽可能长
lngLowStart=LBound(var1,2)
lngHighStart=UBound(变量1,2)
lngLow=lngLowStart
lngHigh=lngHighStart
varPivot=var1(intRowtoSort,(lngLowStart+lngHighStart)\2)
While(lngLow lngLowStart)
lngHigh=lngHigh-1
温德
如果(lngLow)在这里查看MS对错误的解释-您的问题似乎是最初声明时用于arrDataToSort
的类型上的问题?感谢您的回复,我已经查看了此帮助文件,但正在努力实现有效的更改。第一个(也是之前的一个)arrDatatoSort
isReDim arrDatatoSort的声明(1到3,2到countRows)作为Variant
您不能ReDim
一些尚未Dim
'd的内容,我建议您再次检查?可能会发布整个主子程序,以便有更多内容on@Dave可以-如果使用Dim arrDataToSort(),数组的第一个声明可以是ReDim
statement.FYI作为变量
之后,您可以像当前一样使用变量
ReDim
。
Public Sub subQuickSort(ByRef var1 As Variant, _
ByVal intRowtoSort As Integer)
end sub
ReDim arrDataToSort(1 To 3, 2 To countRows) As Variant 'declare an array to be used to sort the data - don't know dimensions yet, so make too big...
'done the above as transpose as can only ReDim second dimension of an array...
Dim intCol As Integer: intCol = 2 'defines the next blank col in arrDataToSort
For i = 2 To 1000 'countRows
If formulaarray1(i, uniqueCOL) = 1 Then 'i.e. unique patient ID
arrDataToSort(1, intCol) = Sheets(sheet_DATA).Cells(i, IDCOL).Value
arrDataToSort(2, intCol) = Sheets(sheet_DATA).Cells(i, FreqCOL).Value
arrDataToSort(3, intCol) = Sheets(sheet_DATA).Cells(i, FreqCOL2).Value
intCol = intCol + 1 'increment to identify next blank row
End If
Next i
ReDim Preserve arrDataToSort(1 To 3, 2 To intCol - 1) 'respecify the size of the array whilst preserving the data
'attempt to sort the data by the UniqueFreqCOL values using QuickSort subroutine
Call subQuickSort(arrDataToSort,2)
Option Explicit
Public Sub subQuickSort(ByRef var1 As Variant)
Dim intRowtoSort As Integer: intRowtoSort = 2
Dim varPivot As Variant
Dim lngLow As Long, lngHigh As Long, lngLowStart As Long, lngHighStart As Long
lngLowStart = LBound(var1, 2)
lngHighStart = UBound(var1, 2)
lngLow = lngLowStart
lngHigh = lngHighStart
varPivot = var1(intRowtoSort, (lngLowStart + lngHighStart) \ 2)
While (lngLow <= lngHigh)
While (var1(intRowtoSort, lngLow) < varPivot And lngLow < lngHighStart)
lngLow = lngLow + 1
Wend
While (varPivot < var1(intRowtoSort, lngHigh) And lngHigh > lngLowStart)
lngHigh = lngHigh - 1
Wend
If (lngLow <= lngHigh) Then
subSwap var1, lngLow, lngHigh
lngLow = lngLow + 1
lngHigh = lngHigh - 1
End If
Wend
If (lngLowStart < lngHigh) Then
subQuickSort var1, lngLowStart, lngHigh (***)
End If
If (lngLow < lngHighStart) Then
subQuickSort var1, lngLow, lngHighStart
End If
End Sub
Private Sub subSwap(var As Variant, intRowtoSort As Integer, lngItem1 As Long, lngItem2 As Long)
Dim varTemp As Variant
varTemp = var(intRowtoSort, lngItem1)
var(intRowtoSort, lngItem1) = var(intRowtoSort, lngItem2)
var(intRowtoSort, lngItem2) = varTemp
End Sub