VBA - If then statement within For Next is not working

OnlyJus50

New Member
Joined
Apr 7, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have created a UserForm. I written code to update button. I am using a For Next loop to loop through the rows with if then statement. Example For i = 2 to 10, if the data is in row 2 or 3, it will update but if the data is in row 4 or high, it will not execute the if /then he statements. I have debug and it will only update the 1st line after the THEN statement. Any help would be appreciated.


VBA Code:
Sub Update()
Dim sh As Worksheet, iRow As Long, i As Integer

Set sh = ThisWorkbook.Sheets("Database")

iRow = sh.Range("A" & Rows.Count).End(xlUp).Row + 1

For i = 2 To iRow

If sh.Cells(i, 22).Value = frmForm.TxtLookup.Text Then
 
    sh.Cells(i, 2) = frmForm.TxtTruck.Value
    sh.Cells(i, 3) = frmForm.CmbVendor.Value
    sh.Cells(i, 4) = frmForm.TxtPO.Value
    sh.Cells(i, 5) = frmForm.TxtBOL.Value
    sh.Cells(i, 6) = frmForm.CmbCarrier.Value
    sh.Cells(i, 7) = frmForm.TxtPallet.Value
    sh.Cells(i, 8) = IIf(frmForm.OptNo.Value = True, "No", "Yes")
    sh.Cells(i, 9) = frmForm.TxtTemp.Value
    sh.Cells(i, 10) = frmForm.TxtEmployee.Value
    sh.Cells(i, 11) = frmForm.TxtGuardTime.Value
    sh.Cells(i, 12) = frmForm.TxtDockTime.Value
    sh.Cells(i, 13) = frmForm.TxtStartTime.Value
    sh.Cells(i, 14) = frmForm.TxtEndTime.Value
    sh.Cells(i, 15) = frmForm.TxtRcvStart.Value
    sh.Cells(i, 16) = frmForm.TxtRcvComplete.Value
    sh.Cells(i, 17) = frmForm.TxtReceiver.Value
    sh.Cells(i, 18) = frmForm.CmbBio.Value
    sh.Cells(i, 19) = frmForm.CmbCage.Value
    sh.Cells(i, 20) = IIf(frmForm.CmbBio.Value = "All", frmForm.TxtPallet.Value, IIf(frmForm.CmbBio.Value = "", 0, frmForm.CmbBio.Value))
    sh.Cells(i, 21) = IIf(frmForm.CmbCage.Value = "All", frmForm.TxtPallet.Value, IIf(frmForm.CmbCage.Value = "", 0, frmForm.CmbCage.Value))

End If
Next
End Sub
Call Reset

End Sub
 
Last edited by a moderator:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
That can't be the real code as you have two End Sub statements.
 
Upvote 0
You are also using col A to determine the last row of data, but your code does not add anything to col A. Should you be using col B to determine the last row?
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top