1
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

Hi, I am trying to get a specific cell with the loop above. I thought the rowIndex was the rownumber on the .Cells(row,column); but it seem like it is not. I am trying to loop through data with filters.
Can anyone help me with this ?
Thanks in advance.

3
  • I don't understand MsgBox rowIndex(2, 2). Why do you want the cell one row down and in column B from the current rowIndex? Commented Apr 22, 2018 at 1:20
  • @Jeeped I am currently trying to understand what is actually rowIndex gets by writing bunch of stuff. That's just wrong. My problem is not knowing how to access the cells on that row with for each loop. Commented Apr 22, 2018 at 1:22
  • For filtercount = 1 To 1? Also, (firstpass) should be just filterpass Commented Apr 22, 2018 at 5:19

1 Answer 1

1

You are dealing with a single row so any Cells(row, column) reference should have 1 if dealing directly with rowIndex or rowIndex.row if referencing a cell on the worksheet.

'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 
Sign up to request clarification or add additional context in comments.

4 Comments

I think now I understand. The use of the with confused me. Thank you. I hope I didn't understand the difference between rowIndex(1,2) and rowIndex.Cells(1,2).
There's no difference. The former is shorthand for the latter by omitting .cells which is the default property.
It worked perfectly. Trying to code for scheduling optimization. I am new to the VBA syntax. Thank you so much for your help.
btw, the Worksheets("Tripdata").Range("A1").AutoFilter could just be .Range("A1").AutoFilter since it is inside the With ... End With block as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.