select/分配多个列和行到数组
我有类似于下面的列结构
Author Book ---- ---- ---- ---- Vicky Book1 Vicky Book2 Giri Book1 Giri Book3
author
列在column B
而Book
列可能会根据其他列数据进入任何列
我想在这两列中select值
我试过了
fileArray = Range("B4:B" & finalRow & "," & endColumn).Value
这里第一部分工作正常, finalRow
包含logging在那里的行号,endColumn是书籍细节可用的地方
如果我使用
fileArray = Range("B4:B" & finalRow )
它正在获取值,但是当我尝试添加更多的列是抛出错误
我也尝试了下面的一个
fileArray = Range("B4:B9,S4:S9").Value
但它没有得到S4:S9
如何获得所需的值?
你可以这样做:
Dim fileArray Dim finalRow As Long Dim targetCol As Long With Sheets("SheetName") ' change to your actual sheet name finalRow = .Range("B" & .Rows.Count).End(xlUp).Row ' assuming "Book" is the column header and headers are in 3rd row ' find the correct column number targetCol = .Range("B3").EntireRow.Find("Book").Column fileArray = Array(.Range("B4:B" & finalRow), _ .Range(.Cells(2, targetCol), .Cells(finalRow, targetCol))) End With
获取值:
Debug.Print fileArray(0)(1) ' returns Vicky Debug.Print fileArray(1)(1) ' returns Author1
所以就像第一个圆括号所包含的数字是列号,第二个是行。