Excel 将范围值指定给数组会导致类型不匹配
我在一行单元格中循环,试图将这些单元格中的值分配给数组,但这会导致类型不匹配错误。我的代码的相关部分如下:Excel 将范围值指定给数组会导致类型不匹配,excel,vba,Excel,Vba,我在一行单元格中循环,试图将这些单元格中的值分配给数组,但这会导致类型不匹配错误。我的代码的相关部分如下: Dim queryaddress As Range Dim notoffsetnum As Integer Dim anotherarrayofnumbers() As Integer Dim c As Range For Each queryaddress In worksheetname.Range("B2:B21") Set queryrow = queryaddress.Entir
Dim queryaddress As Range
Dim notoffsetnum As Integer
Dim anotherarrayofnumbers() As Integer
Dim c As Range
For Each queryaddress In worksheetname.Range("B2:B21")
Set queryrow = queryaddress.EntireRow
notoffsetnum = 0
For Each c In queryrow
If c.Interior.Color <> 192 And Not IsEmpty(c.Value) Then
notoffsetnum = notoffsetnum + 1
ReDim Preserve anotherarrayofnumbers(notoffsetnum)
anotherarrayofnumbers(notoffsetnum) = c.Value
'The above line errors
End If
Next c
Next queryaddress
Dim查询地址作为范围
Dim NOTFFSETNUM作为整数
将另一个ArrayOfNumbers()设置为整数
调光范围
对于工作表名称范围(“B2:B21”)中的每个查询地址
设置queryrow=queryaddress.EntireRow
notoffsetnum=0
对于queryrow中的每个c
如果c.Interior.Color 192不为空(c.Value),则
notoffsetnum=notoffsetnum+1
ReDim保留其他数组编号(notoffsetnum)
其他数组编号(notoffsetnum)=c.值
"以上几行错误,
如果结束
下一个c
下一个查询地址
Afor each
循环一个集合。您有一个称为查询行的范围。你有一个称为c的范围。您所做的是在queryrow中的每个范围中循环…这意味着c将只是查询行
你想要
查询单元格中每个c的
另外,请注意,这将尽可能地降低效率,因为它将循环遍历所有65000个左右的列,而不仅仅是实际包含数据的相对较少的列
编辑:我不知道为什么这仍然会让你犯错误。不过,您还有其他逻辑错误。如果我从B2:H21中输入一些数据,这对我来说是执行的(也是出于善意,缩进!),例如:
Sub test()
Dim worksheetname As Worksheet
Set worksheetname = ActiveWorkbook.ActiveSheet
Dim queryaddress As Range
Dim notoffsetnum As Integer
Dim anotherarrayofnumbers() As Integer
Dim c As Range
For Each queryaddress In worksheetname.Range("B2:B21")
Dim queryrow As Range
Set queryrow = queryaddress.EntireRow
notoffsetnum = 0
For Each c In queryrow.Cells
If c.Interior.Color <> 192 And Not IsEmpty(c.Value) Then
notoffsetnum = notoffsetnum + 1
ReDim Preserve anotherarrayofnumbers(notoffsetnum)
anotherarrayofnumbers(notoffsetnum - 1) = c.Value
End If
Next c
Next queryaddress
Dim i As Integer
For i = 0 To UBound(anotherarrayofnumbers) - 1
Debug.Print anotherarrayofnumbers(i)
Next i
End Sub
我的代码中有问题的地方是:
Dim另一个数组fnumbers()为整数
这导致在以下方面出现错误:
anotherarrayofnumbers(notoffsetnum)=c.Value
这是因为我的一些c.Value
值实际上不是整数
解决此问题的一种方法是将数组更改为变量类型:
Dim anotherarrayofnumbers()作为变量
但这对我来说不起作用,因为我后来不得不对数组执行整数运算(例如WorksheetFunction.Quartile
)。相反,我只是将格式化应用于那些不是整数值的c.Value
值,以便将它们从数组中筛选出来。这解决了我的问题
因此,我对If
块的条件现在如下所示:
如果c.Interior.Color 192和c.Interior.Color 177不为空(c.Value),则
其中,附加的内部颜色是我将非整数值格式化为的颜色。是否所有循环的非空单元格都包含整数值?发生错误时,使用立即窗口检查notoffsetnum的值,然后查看相应窗口中的值cell@barrowc这就是问题所在,我没有一个
c.Value
的整数值。过滤掉这一个值终止了错误。
Sub BetterSolution()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
Dim firstCell As Range
Set firstCell = ws.Range("B2")
Dim lastCol As Integer
lastCol = firstCell.End(xlToRight).Column
Dim lastRow As Integer
lastRow = firstCell.End(xlDown).Row
Dim lastCell As Range
Set lastCell = ws.Cells(lastRow, lastCol)
Dim arr() As Integer
Dim rng As Range
Dim index As Integer
index = 0
For Each rng In ws.Range(firstCell, lastCell).Cells
index = index + 1
ReDim Preserve arr(index + 1)
arr(index) = rng.Value
Next rng
End Sub