主要目標是:使用列引用名稱從篩選表中的每一行中檢索特定的單元格值。VBA:從Range.Area中的每一行中檢索單元值
到目前爲止,我有以下的代碼
Dim table As listObject
Dim columns As ListColumns
Dim row As ListRow
Dim rnData As range
Dim rngArea As range
Set table = Sheets(sheetName).ListObjects(TableName)
Set columns = table.ListColumns
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range
'Notice that sheetName and TableName are function arguments. No need to pay attention. Consider any string values.
'Filter my table
table.range.AutoFilter Field:=7, Criteria1:=Array("filtervalue1", "filtervalue2"), Operator:=xlFilterValues
'Set the filtered table in a new Range object
Set rnData = ThisWorkbook.Worksheets(sheetName).ListObjects(TableName).range
'Count all rows of my filtered table
With rnData
For Each rngArea In .SpecialCells(xlCellTypeVisible).Areas
lCount = lCount + rngArea.Rows.Count
Next
End with
現在我想我的循環過濾表(我的「rnData」範圍),我想在這些Range.Areas各行的單元格值。
我在想這樣的事情,但我在使用VBA困難做到這一點:
For iRowNo = 2 To (lCount - 1) 'Start at 2 because 1 is the table header
'This does not work once it gets another row from the entire table. Not the filtered one. Help here!
Set row = table.ListRows(iRowNo)
'Something close to this - Help Here!
Set row = rnData.SpecialCells(xlCellTypeVisible).Areas
''Would like to have the code like this to get the values
cell1Value= row.range(1, columns("My Column Header 1").Index).value
cell2Value= row.range(1, columns("My Column Header 2").Index).Value
Next iRowNo
讓我知道,如果有不同的解決方案莫過於此。
爲什麼不裏面的'對於每個rngArea ...'循環運行另一個'對於每一個[變量]在rngArea.rows'循環?這樣你就可以得到所有的行(只要記住你指的是整行的限制,所以如果你想要到一個單元格,你需要'[variable] .cells(1,y)') –
不清楚(對不起我英語不好),你能添加你有什麼和你想要的圖片嗎?我想,對其他人來說也會更容易。 – AntiDrondert
@DirkReichel謝謝你帶來了!解決了這個問題。我會回答我自己的問題。 –