简体   繁体   中英

incremental nested looping if statement vba

I have a piece of code to perform a nested If statement. Easy to nest in Excel but difficult in VBA.

I want to automate that nested If into code that can increment to a last row and change the range with each pass.
I have looked through the answers here but am stumped.

The Excel If is =IF(C2 > 1.42, A2, IF(D2 > 1.42, B2, "FAIL")) .

My VBA attempt thus far is:

Sub ResultData()
    If Range("C2").Value > 1.42 Then
       Range("E2") = Range("A2")

    ElseIf Range("D2").Value > 1.42 Then
        Range("E2") = Range("B2")

    Else: Range("E2") = "FAIL"

    End If
End Sub

Just want the code to run down the rows to the last row, in this case at row 53.

目前的数据

Something like this should work for you:

Sub ResultData()
  For i = 2 to 53
    If Cells(i, 3).Value > 1.42 Then
      Cells(i, 5).Value = Cells(i, 1).Value
    ElseIf Range("D2").Value > 1.42 Then
      Cells(i, 5).Value = Cells(i, 2).Value
    Else
      Cells(i, 5).Value = "FAIL"
    End If
  Next
End Sub

Since your question has already been answered, I thought I should focus a bit on the good practices you should adopt.

Also, since the cells method has been shown, I figured it might be useful to also show how to work with ranges instead.

The comments should guide you through the basic logic.

Option Explicit 'Always add this line. It prevents you from using a variable without having declared it.
Sub ResultData()
Dim wb As Workbook 'declare a workbook variable which can hold a workbook object
Dim sht As Worksheet 'declare a worksheet variable which can hold a worksheet object
Dim i As Long 'declare a variable that will hold the row index. This should always be of type Long
Set wb = ThisWorkbook 'assign the workbook where your code is, to your workbook variable.
Set sht = wb.Worksheets("The name of your Worksheet") 'assign the worksheet where your data is, to your worksheet variable.

For i = 2 To 53 Step 1 'loop through rows 2 to 53
                                                    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If sht.Range("C" & i).Value > 1.42 Then         'Refer to ranges and cells by using explicit references to the                  '
        sht.Range("E" & i) = sht.Range("A" & i)     'worksheets to which they belong. So, sht.range("A1") refers to cell A1 of sht. '
    ElseIf sht.Range("D" & i).Value > 1.42 Then     'You can dynamically refer to a range by concatenating the column's index letter'
        sht.Range("E" & i) = sht.Range("B" & i)     'with a dynamically changing variable which represents the row's index.         '
    Else                                            'So sht.Range("D" & "3") for example refrs to sht.Range("D3").                  '
        sht.Range("E" & i) = "FAIL"                 '                                                                               '
    End If                                          '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Next i

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