1)识别第2列)识别列中的值3)将数据从该行插入到数组中

我试图收集水平数据(从左到右)从单元格I10开始 – 我首先尝试对macros进行编码以在列I中标识正确的分类账户。列I在下面I:10列出我想要的列, Dividend Income

 Ledger Account Prior Shares Outstanding Current Shares Outstanding Current Share Activity Previous Net Assets Current Net Assets Net Asset Change Market Value Trading Gain Loss Dividend Income 

我有下面的代码,我试图将数据插入到数组中的I10(I:10到Z:10)的右侧。

 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(1) 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 

我发现你的问题令人困惑。

例1:我是I:10 to Z:10 ? 你的意思是I10 to Z10还是I10:Z10 ? 冒号在两个单独的单元格地址之间定义一个范围。

示例2:文本暗示帐户名称列表位于列I(= 9)中,但您的代码search列1(= A)。

在这个答案中,我向你提供的信息,我希望能指出你在正确的方向。


考虑: Set ws = ThisWorkbook.Sheets(1)

用户可以打开几个工作簿。 让我们呼叫A,B,C和D.用户可以让A活动,但在D中调用一个macros。当这种情况发生时,可以使用ActiveWorkbook来引用活动工作簿和ThisWorkbook来引用包含macros。 如果您认为用户可能会从另一个工作簿中调用macros,则您是正确的,可以明确指出您需要包含该macros的工作簿中的工作表。 否则Set ws = Sheets(1)会给你一个工作表内的活动工作簿。

避免Sheets(1)Sheets(2) 。 在这里,1和2是指用户可以有意或无意地改变标签行内的顺序。 创build一个新的工作簿并将此代码复制到一个模块:

 Option Explicit Sub Test1() Dim InxWsht As Long For InxWsht = 1 To Worksheets.Count Debug.Print Worksheets(InxWsht).Name Next End Sub 

如果您的Excel的英文版本具有默认的页数,则此macros将向立即窗口输出以下内容:

 Sheet1 Sheet2 Sheet3 

现在移动Sheet2,使其位于Sheet3之后,然后重新运行macros。 现在的输出是:

 Sheet1 Sheet3 Sheet2 

始终按名称标识工作表; 例如:工作表(“分类帐”),以便用户不会意外地破坏您的macros。


考虑Set aCell = .Columns(1).Find . . . Set aCell = .Columns(1).Find . . .

正如我所说,这将search第1列,虽然您的文字说第一列。这可能只是一个印刷错误,但是最好避免在代码的主体中使用字面值。 当新列被添加或者用户想要新的序列时,列移动。 通过代码search试图确定哪些数字指向一列,以便更新它们可以是一个很容易避免的噩梦。

 Const ColName As Long = 9 

要么

 Const ColName As String = "I" 

允许你写

 Set aCell = .Columns(ColName).Find . . . 

现在,如果包含分类帐名称的列移动,则顶部的一个更改会更新代码。 还有一个好处就是“ColName”对未来的维护程序员来说意味着什么(可能是你)。 如果你曾经尝试过更新一个作者用数字来引用20个不同列的macros,你就会明白为什么我喜欢这个名字。


我把你的名字列表加载到工作表“Ledgers”的列“A”中。 下面是你的macros的修改版本。 研究我的变化,试图理解为什么我做了他们。 回答问题的必要性,但越多,你可以为自己工作,你会发展得越快。

 Sub Sample() Const ColName As Long = 1 Const ColFirstWanted As Long = 2 Dim ws As Worksheet Dim MyAr As Variant Dim SearchString As String Dim aCell As Range Dim ColCrnt As Long ' Avoid names like i; they can get very confusing. Dim ColLast As Long SearchString = "Dividend Income" '~~> Change this as applicable Set ws = Worksheets("Ledgers") With ws Set aCell = .Columns(ColName).Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then ' Find last used column in row ColLast = .Cells(aCell.Row, Columns.Count).End(xlToLeft).Column '~~> Amend as applicable '~~> Store the values From say Col B to E in the array '## Note: Transpose is slow when called from VBA. MyAr = .Range(.Cells(aCell.Row, ColFirstWanted), .Cells(aCell.Row, ColLast)).Value '~~> Check what is in the array ' ## Note: Although the worksheet row is not 1 and the first column is not 1, ' ## the lower bounds are both 1. This can be confusing. I suggest ' ## loading the entire row so the worksheet column numbers match the array ' ## column numbers. A few extra columns in the array is a small price to pay ' ## reduced confusion. For ColCrnt = LBound(MyAr, 2) To UBound(MyAr, 2) Debug.Print ColCrnt & " " & MyAr(1, ColCrnt) Next ColCrnt End If End With End Sub