IF, ELSEIF Loop Not working. PLEASE HELP!

navidadi28

New Member
Joined
Oct 7, 2015
Messages
12
Hello,

I am attempting a loop vlookup with an IF statement. The Vlookup is matching loans # (in i,4) from the active sheet to the lookup sheet "CADM" to return the "Commitment Amounts" to the active sheet cell (i,5) and down. I have the Vlookup working just fine. The problem is when I want to do the IF statement. The reason for the IF statement is that not all the loan # will match and return a Commitment Amount. So IF the return value is 0 (this is in cell i,5) then have the cell (i,5) be the value of the cell (i,6). Then I have an ElseIf being if cell(i,5) return #N/A then have cell(i,5) be the value of cell(i,6).

Please note for the IF/ElseIF statement the values "0" and "#N/A" will be in random order. N/A might come first for this month but next month the 0 will come first.

Sub CADMVlookup()


Dim LastRow As Long
Dim i As Integer

LastRow = Cells(Rows.Count, 4).End(xlUp).Row
For i = 8 To LastRow
'This is the vlookup that is working correctly
Cells(i, 5).Value = Application.vlookup(Cells(i, 4), Worksheets("CADM").Range("B:C"), 2, False)

This is where my problem starts.
'This is the start of the my If statement.
If (Cells(i, 5).Value = 0) Then
Cells(i, 5).Value = Cells(i, 6).Value
If Cells(i, 5).Value = "#N/A" Then
Cells(i, 5).Value = Cells(i, 6).Value
End If
Next i


End Sub


Thank you so much for the help!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
For reference the error that is given is "Type mismatch" with the following line highlighted.

If (Cells(i, 5).Value = 0) Then
 
Upvote 0
One thought is to change the second if statement into an "ElseIf" statement.

Code:
If (Cells(i, 5).Value = 0) Then
Cells(i, 5).Value = Cells(i, 6).Value
ElseIf Cells(i, 5).Value = "#N/A" Then
Cells(i, 5).Value = Cells(i, 6).Value
End If
 
Upvote 0
One thought is to change the second if statement into an "ElseIf" statement.

Code:
If (Cells(i, 5).Value = 0) Then
Cells(i, 5).Value = Cells(i, 6).Value
ElseIf Cells(i, 5).Value = "#N/A" Then
Cells(i, 5).Value = Cells(i, 6).Value
End If


Still shows the same error. Any other suggestion/ Thank you!
 
Upvote 0
Code:
If IsError(Cells(i, 5).Value) Then
 Cells(i, 5).Value = Cells(i, 6).Value
ElseIf Cells(i, 5) = 0 Then
 Cells(i, 5).Value = Cells(i, 6).Value
End If
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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