我已经存入此代码,下标超出范围错误我认为这是因为数字太大(LBound(DataArr, 20)
?
For i = LBound(DataArr, 20) To UBound(DataArr, 20) 'change 1->2
DataArr(i, 86) = "" 'change 3->4 '86
Next i
对于i = LBound(DataArr,20)到UBound(DataArr,20)'更改1-> 2
如果我使用LBound(DataArr, 20)
下标超出范围错误,则上面是我的行错误但是如果我使用LBound(DataArr, 1)
或2或3它正在工作..
但是我要计算的列是Column T = 20
还有其他方法吗?
我的完整代码:(已编辑)
Public Sub Selection()
Dim file2 As Excel.Workbook
Dim Sheet2 As Worksheet, data(), i&
Dim myRangeColor As Variant, myRangeMonthValue
Dim MstrSht As Worksheet
Dim DataArr As Variant
Dim ColorArr As Variant
Dim MonthCol As Collection
Dim CloseToDate As Date
Dim MaxDate As Date
Dim c As Long
Set Sheet2 = Workbooks.Open(TextBox2.Text).Sheets(1)
Set Sheet5 = Workbooks.Open(TextBox5.Text).Sheets(1)
DataArr = Sheet2.Range("A2:CI" & Sheet2.Cells(Rows.Count, 1).End(xlUp).Row) 'change 1->2
'Find distinct colors
ColorArr = ReturnDistinct(Sheet2.Range("T2:T" & Sheet2.Cells(Rows.Count, 1).End(xlUp).Row)) 'change a->b 1->2
'Remove any values in the arrays third column
For i = LBound(DataArr, 1) To UBound(DataArr, 1) 'change 1->2
DataArr(i, 86) = "" 'change 3->4 '86
Next i
'Loop Each Color
For c = LBound(ColorArr) To UBound(ColorArr)
Set MonthCol = New Collection
MaxDate = 0
For i = LBound(DataArr, 1) To UBound(DataArr, 1) 'change 1->2
If DataArr(i, 1) = ColorArr(c) Then 'change 1->2
'Load the colors months into a collection
On Error Resume Next
MonthCol.Add Month(DataArr(i, 71)), CStr(Month(DataArr(i, 71))) 'change 2->3
On Error GoTo 0
'Find Max Date
If DataArr(i, 71) Then 'change 2->3
MaxDate = Application.WorksheetFunction.Max(MaxDate, DataArr(i, 71)) 'change 2->3
End If
End If
Next i
'If the color were found in three or more seperate months then the row with date closest to CloseToDate gets flagged
If MonthCol.Count > 2 Then
For i = LBound(DataArr, 1) To UBound(DataArr, 1) 'change 1->2
If DataArr(i, 1) = ColorArr(c) And DataArr(i, 71) = MaxDate Then 'change 1->2 2->3
DataArr(i, 86) = "1" '86
DataArr(i, 87) = "1" '87
End If
Next i
End If
Next c
'Print results to sheet
Sheet2.Range("A2:CI" & Sheet2.Cells(Rows.Count, 1).End(xlUp).Row) = DataArr 'change 1->2
Function ReturnDistinct(InpRng As Range) As Variant
Dim Cell As Range
Dim i As Integer
Dim DistCol As New Collection
Dim DistArr()
'Add all values to collection
For Each Cell In InpRng
On Error Resume Next
DistCol.Add Cell.Value, CStr(Cell.Value)
On Error GoTo 0
Next Cell
'Write collection to array
ReDim DistArr(1 To DistCol.Count)
For i = 1 To DistCol.Count Step 1
DistArr(i) = DistCol.Item(i)
Next i
ReturnDistinct = DistArr
End Function
答案 0 :(得分:4)
对于i = LBound(DataArr,20)到UBound(DataArr,20)'更改1-> 2
您正在询问Excel," DataArr中第20个排名的下限和上限是多少?"
问题是 - 以及下标超出范围错误的原因 - DataArr中没有第20个等级。 DataArr实际上只包含2个排名。这意味着LBound和UBound表达式会引发错误,因为它们是使用无效参数调用的。
我不确定您需要访问哪个等级,但20
是您必须更改的内容 - 以及您现在设置数组的方式,该数字必须为1或2。 / p>
编辑:为了您的休闲,这是一个由Chip Pearson编写的快速实用程序,它允许您以编程方式验证数组中的排名数:
Private Function NumberOfArrayDimensions(arr As Variant) As Integer
' By Chip Pearson
Dim Ndx As Integer
Dim Res As Integer
On Error Resume Next
Do
Ndx = Ndx + 1
Res = UBound(arr, Ndx)
Loop Until Err.Number <> 0
NumberOfArrayDimensions = Ndx - 1
End Function
根据您的评论编辑:
我想计算来自列T的数据,我将其从1更改为&gt; 20
我不是100%的意思,但是要从数组中的第T列访问数据(第20列),这就是语法:
someValue = DataArr(i, 20)
在这种情况下,i
是(行号-1)。
例如,DataArr(1, 20)
将包含来自Range("T2")
(或Cells(2, 20)
)的数据
根据您的意见编辑:
这就是我正在尝试的但却是colum的故事。它的专栏是My logic
同样的结果,但现在我要更改列而不是A它 列T而不是B im将其与列BS进行比较
更改
For i = LBound(DataArr, 20) To UBound(DataArr, 20) 'change 1->2
到
For i = LBound(DataArr, 1) To UBound(DataArr, 1) 'change 1->2
由于:
第一个排名是你的行,第二个排名是你的列。如前所述,没有第20名。根据您的描述,听起来您需要将第86列(我猜是#34; BS&#34;)中的每个单元格设置为空。在这种情况下,上述更改是正确的。