使用SearchDirection识别垂直单元格

我需要确定列分类帐帐户中的股息收入行。 这是从Column A Cell A1开始的数据:

 Ledger Account Prior Shares Outstanding Current Shares Outstanding Current Share Activity Dividend Income 

最终,我想把一个数组中的相邻数据在红利收入行(即偏移量1,2,3细胞等)。

这是我有什么 – 我怎么能修改方向,以确定适当的列(帐户帐户)后,向下search和存储在行5(股息收入行)中的值在一个数组?

 cName = "Ledger Account" cA = Sheets(1).Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column 

这是你正在尝试? 我已经评论了代码,所以你不应该有理解它的问题。 但是,如果你仍然这样做,然后简单地问

 Sub Sample() Dim ws As Worksheet Dim MyAr As Variant Dim SearchString As String Dim aCell As Range Dim i As Long SearchString = "Ledger Account" '~~> Change this as applicable Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Columns(1).Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then '~~> 5 Denotes the 5th column ie Column "E" '~~> Amend as applicable '~~> Store the values From say Col B to E in the array MyAr = Application.Transpose( _ .Range(.Cells(aCell.Row, 2), _ .Cells(aCell.Row, 5) _ ).Value _ ) '~~> Check what is in the array For i = LBound(MyAr) To UBound(MyAr) Debug.Print MyAr(i, 1) Next i End If End With End Sub 

这是所有你需要从股息收入行的值到一个数组:

 Public Sub Sample() Dim v v = [index(a:a,match("dividend income",a:a,))].EntireRow End Sub 

通过以上所述, v变成一个二维数组,其中包含了该行的所有值。 例如, v(1, 1)包含“股利收入”, v(1, 2)包含右边的值,依此类推。

这是一个有效的方法。 一个variables(这是你想要的数组),只需要一个通过Excel和VBA之间的边界的调用。

上面直接关注如何获得数组。 当然,在实践中应该考虑到search文本没有find的情况,所以这个版本是这样做的:

 Public Sub Sample() Dim v On Error Resume Next v = [index(a:a,match("dividend income",a:a,))].EntireRow If Err Then MsgBox "No match." End Sub