Home » excel » excel – How do iterate through a number of rows and call vba depending on which line is flagged

excel – How do iterate through a number of rows and call vba depending on which line is flagged

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a table that retrieves a list of available reports to the user. The user then enters an ‘X’ in a specific column next to the report they wish to generate.

How do I iterate through each available row and work out if the user has entered ‘X’?

A          |         B          |       C          |  D
001        | REPORT A           |      fnReportA   |  X
002        | REPORT B           |      fnReportB   |  <NULL>
003        | REPORT C           |      fnReportC   |  <NULL>

OR

How do I iterate through each row when the list is retrieved and put a BUTTON at the end of each row that links to the vba to generate the report?

How to&Answers:

For Each will do the type of search you are looking to do

A quick example:

Option Explicit

Sub testit()
Dim c As Range
For Each c In Range("D2:D10").Cells
    If c.Value = "X" Then
        Debug.Print c.Row
    End If
Next
End Sub

This will print the row number of each cell that has an X.