Arrays 在VBA中将单元格文本添加到数组
我已经试着研究了一段时间了。我试图编写一个函数,从B9开始查找B列中最后填充的行,并将B列中的字符串值添加到数组中Arrays 在VBA中将单元格文本添加到数组,arrays,excel,vba,Arrays,Excel,Vba,我已经试着研究了一段时间了。我试图编写一个函数,从B9开始查找B列中最后填充的行,并将B列中的字符串值添加到数组中 Option Explicit Function locationSum2(location) As Integer Dim LastRow As Integer LastRow = LastRowInColumn(2) Dim i As Integer For i = 9 To i = LastRow If location = Cells(i, 2).Text T
Option Explicit
Function locationSum2(location) As Integer
Dim LastRow As Integer
LastRow = LastRowInColumn(2)
Dim i As Integer
For i = 9 To i = LastRow
If location = Cells(i, 2).Text Then
locationSum2 = locationSum2 + Cells(i, 11).Value
End If
Next i
locationSum2 = locationSum2
任何帮助都将不胜感激,谢谢大家
Option Explicit
Function locationSum2(location) As Integer
Dim arrayLength As Integer
arrayLength = LastRowInColumn(2) - 9
Dim arrayPosition As Integer
Dim locationColumn() As String
ReDim locationColumn(0 To arrayLength) As String
Dim celltext As String
Dim i As Integer
For i = 0 To i = arrayLength
celltext = Cells((9 + i), 2).Text
locationColumn(i) = celltext
Next i
For arrayPosition = 0 To arrayPosition = UBound(locationColumn)
If location = locationColumn(arrayPosition) Then
locationSum2 = locationSum2 + Cells(arrayPosition + 9, 11).Value
End If
Next arrayPosition
locationSum2 = locationSum2
End Function
在注释之后,我还将代码切碎,基本上只是一个循环,用于根据B列中的所有内容检查参数,而不是生成数组
Option Explicit
Function locationSum2(location) As Integer
Dim LastRow As Integer
LastRow = LastRowInColumn(2)
Dim i As Integer
For i = 9 To i = LastRow
If location = Cells(i, 2).Text Then
locationSum2 = locationSum2 + Cells(i, 11).Value
End If
Next i
locationSum2 = locationSum2
不管出于什么原因,它似乎仍然没有正确地阅读B列。它返回的值为0,因此似乎找不到匹配项。您错误地设置了for循环
For i = 9 To i = LastRow
应该是
For i = 9 To LastRow
我不确定这是否是你唯一的问题,但让我们从这里开始这个问题的措辞有点难以理解。对应整数=计数?或者它是一个哈希值/表中的某个值?-似乎用更多的词来表达更少的清晰,萝莉倾向于这样做,对不起。我把问题简化为核心问题,并在另一个回答之后添加了一些内容。为什么要将单元格添加到数组中?如果您的唯一目的是对照一个值进行检查,那么为什么不遍历单元格,并在有匹配项时更新新列中的单元格?这似乎是唯一的问题。谢谢你,好先生,我希望这只是我的一个笔误。很好,简短的解决方案。德国劳埃德船级社