VBA:从Range.Area

时间:2017-08-25 03:06:26

标签: excel vba excel-vba

主要目标是:使用列引用名称从筛选表中的每一行检索特定单元格值。

到目前为止,我有以下代码

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

如果有不同的解决方案,请告诉我。

2 个答案:

答案 0 :(得分:3)

关注@DirkReichel回答

以下代码对我有用:

$class = "";
if($result['dress_quantity'] < 10) { $class='isLess'; }
<tr <?php echo $class; ?> >

答案 1 :(得分:1)

我认为你是间接尝试创建一个不能轻易解释单个帖子的数组,但这里有一些代码可以帮助你入门。

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

最重要的是我认为你正在寻找一种创建阵列的方法。我希望这会有所帮助。

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
相关问题