将单元格文本添加到VBA中的数组

我一直在试图研究一下现在。 我试图编写一个函数,它将find列B中的最后一个已填充的行(从B9开始),并将列B中的string值添加到数组中。

任何帮助将不胜感激,谢谢大家。

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中的所有参数,而不是创build一个数组。

 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,所以似乎没有find匹配。

您已经错误地设置了for循环。

 For i = 9 To i = LastRow 

应该

 For i = 9 To LastRow 

我不确定这是否只是问题,但让我们从那里开始