简体   繁体   中英

If statement within a For-Loop

I'm trying to have a for loop go through a column to find "No" and if "No" is not found it outputs "Yes" in a different cell. So my thought process is having the for-loop and then an if statement saying that if the cell says "Yes" or "No Requirement" then it continues looking for a "No" and if a "No" isn't found it outputs "Yes" and if it is found the loop stops and it outputs "No".

I'm making a tool to pull data from different sheets on the same excel workbook.

Sub PleaseWork()

Dim matrix(i, j) As Double


For i = 26 To 33
    For j = 8 To 8
        If Worksheets("Calculations").Range(i, j) = "Yes" Or "No Requirement" Then
            Worksheets("Calculations").Range("H33").Value = "Yes"
            Next i
        Else
            Worksheets("Calculations").Range("H33").Value = "No"
        End If



End Sub

This code won't even run, I get a compile error every time I try to run it. I'm new to VBA and know this is probably super easy but I just can't seem to get it.

Next i needs to be outside your If-End If and you'd need to add Next j within your For i loop for the code to even run. Additionally, if j = 8 your second loop will only ever run once and you can eliminate that entirely.

it would be much easier to say

For i = 26 To 33
    If Worksheets("Calculations").Cells(i, 8) = "Yes" Or Worksheets("Calculations").Cells(i, 8) = "No Requirement" Then
        Worksheets("Calculations").Range("H33").Value = "Yes"
    Else
        Worksheets("Calculations").Range("H33").Value = "No"
    End If
Next i

I'm not sure why you'd use a matrix here if you're just looking through the one column.

The below code will check through the column (I can make it a dynamic range if you so choose) and stop at the first cell with value "No Requirement" - it then outputs the address of the cell with the value and "No" and exits the loop.

Sub RandomCol()

For Each cell In Worksheets("Calculations").Range("A1:A50")

    If cell.Value = "Yes" Or cell.Value = "No Requirement" Then

    ElseIf cell.Value = "No" Then
        Range("H33").Value = (cell.Address + " " + "No")
        Exit For
    End If

Next cell

End Sub

I hope this helps.

The following should work for you purposes:

    Sub RunIt()
      '          A, E, 1, 5
      PleaseWork 1, 5, 1, 5
    End Sub

    Sub PleaseWork(intFromCol As Integer, intToCol As Integer, intFromRow As Integer, _
                   intToRow As Integer)

        Dim intI As Integer
        Dim intJ As Integer
        Dim blnFoundNo As Boolean
        Dim strOut As String

        strOut = "Yes"
        blnFoundNo = False
        For intI = intFromCol To intToCol
            For intJ = intFromRow To intToRow
                '                                     Row , Col
                With Worksheets("Calculations").Cells(intJ, intI)
                    If LCase(Trim(.Value)) = "no" Then
                        strOut = Replace(Cstr(.Address), "$", "") & " = No"
                        blnFoundNo = True
                        Exit For
                    End If
                End With
            Next intJ
            If blnFoundNo Then Exit For
        Next intI

        Worksheets("Calculations").Range("H33").Value = strOut

    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM