循环行范围 - 如何获取特定单元格?

时间:2018-04-22 01:14:40

标签: excel vba excel-vba loops excel-2007

With Worksheets("Tripdata")
    For filtercount = 1 To 1
        Worksheets("Tripdata").Range("A1").AutoFilter Field:=4, Criteria1:=stationidentify(filtercount, 1)
        For Each rowIndex In .UsedRange.Rows
            If (rowIndex.Hidden) Then
            'nothing
            Else
                If (firstpass) Then
                    firstpass = False
                Else
                    MsgBox rowIndex(2, 2)
                    test = test + 1
                    Dim currentstat As Double, finalstat As Double
                    currentstat = Worksheets("Tripdata").Cells(rowIndex, 3).Value
                    finalstat = Worksheets("Tripdata").Cells(rowIndex, 7).Value

The tripdata visual look - I wanna somehow get the names on the fourth and seventh cells

嗨,我正在尝试使用上面的循环来获取特定的单元格。我认为rowIndex是.Cells(行,列)上的rownumber;但它似乎不是。我试图用过滤器循环数据  任何人都可以帮我这个吗? 提前致谢。

1 个答案:

答案 0 :(得分:1)

您正在处理单行,因此如果引用工作表上的单元格,如果直接处理rowIndex或rowIndex.row,则任何单元格(行,列)引用应该为1。

'put these outside the loop
Dim currentstat As Double, finalstat As Double
For Each rowIndex In .UsedRange.Rows
...
Else
    MsgBox rowIndex(1, 2)  'column B value within rowIndex
    MsgBox rowIndex.cells(1, 2)  'column B value within rowIndex
    test = test + 1
    currentstat = Worksheets("Tripdata").Cells(rowIndex.row, 3).Value
    finalstat = Worksheets("Tripdata").Cells(rowIndex.row, 7).Value
    'you're inside a With ... End With block so these are the same
    currentstat = .Cells(rowIndex.row, 3).Value  'column C value within rowIndex
    finalstat = .Cells(rowIndex.row, 7).Value  'column G value within rowIndex
    debug.print currentstat
    debug.print finalstat