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 

让我知道是否有不同的解决scheme。

我认为你是间接尝试创build一个数组,这不是一个可以轻松解释的单个post,但是这里有一些代码可以让你开始。

我将假设你设置的rnData范围是正确的。 从那里,最简单的方法就是遍历范围内的所有单元格。 你可以编写比下面更精确的代码,但这应该可以帮助你看到除了你正在尝试的一些想法。

最重要的是我想你正在寻找一种方法来创build一个数组。 我希望这有帮助。

 Sub testCoutinho() Dim Rcell As Range Dim rnData As Range 'you'll have to set this up... Dim YesLetsDoAnArray As Boolean: YesLetsDoAnArray = False 'or change to false to just make a new sheet with values If YesLetsDoAnArray Then ReDim This_is_your_Array(0) As Variant 'Create Array Dim x As Integer Else 'putting values on a new worksheet in file Dim CleanWS As Worksheet: Set CleanWS = ThisWorkbook.Sheets.Add End If For Each Rcell In rnData.Cells If Rcell.EntireRow.Hidden = False Then If YesLetsDoAnArray Then ReDim Preserve This_is_your_Array(x) This_is_your_Array(x) = Rcell.Value x = x + 1 Else CleanWS.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Rcell.Value End If End If Next Rcell 'If you used an array, you'll know have variable(s) that contain all your data. 'your first one is This This_Is_Your_Array(0), followed by This_Is_Your_Array(1)... etc. 'you can play around. this will print them all. If YesLetsDoAnArray Then Dim i As Integer For i = 0 To x - 1 Debug.Print This_is_your_Array(i) Next i End If End Sub 

在@DirkReichel的答案

这是我工作的代码:

 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 'Get values for each row With rnData For Each rngArea In .SpecialCells(xlCellTypeVisible).Areas For Each row In rngArea.Rows cell1Value= row.range(1, columns("My Column Header 1").Index).value cell2Value= row.range(1, columns("My Column Header 2").Index).Value Next 'lCount = lCount + rngArea.Rows.Count 'Removed this. Next End with 'Also no need the second part of code with the For..Next loop.