VBA code with IF and Vlookup formula's

clau818

New Member
Joined
Oct 31, 2016
Messages
32
Hello all!

First I'd like to thank everyone in advance who helps me out with this! I don't know where I'd be in life without this forum, you guys are all absolute geniuses!

So now to my questions.

I am trying to create some vba code and I'm fairly new to this. I am trying to create a code that uses a vlookup formula based on a certain condition (IF statement).

There are two parts to my code.

First, I want the code to scan all of my data and if the word "Freight" appears in column F, I want a Vlookup to be performed in the same row but in column H. In the rows where the word "Freight" does not appear, I don't want anything done because there will already be data in those cells.

Here is what the formula would look like if not done in VBA. This causes a circular reference that I would prefer not to have.

=IF($F1="Freight",VLOOKUP($E1,'Summary Invoicing'!$F:$P,11,FALSE),$H1)

In the second part of my code I want it to once again scan all of my data and if the word "Freight" appears in column F, I want a Vlookup to be performed in the same row but in column I. If the word "Freight" does not appear in column F, I want it to remain blank.

Here is what the formula would look like if not done in VBA.

=IF($F1="Freight",VLOOKUP($E1,'HJ Tracking Numbers'!$B:$F,5,FALSE)," ")


Thanks so much!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello,

Do not hesitate to post your current code ...

Cheers
 
Upvote 0
Don't have Excel at the moment, so UNTESTED..

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For r = 1 To lr
    If Range("F" & r).Value = "Freight" Then
        Range("H" & r).Formula = "=VLOOKUP($E" & r & ",'Summary Invoicing'!$F:$P,11,FALSE)"
        Range("I" & r).Formula = "=VLOOKUP($E" & r & ",'HJ Tracking Numbers'!$B:$F,5,FALSE)"
    End If
Next r
End Sub
 
Upvote 0
Don't have Excel at the moment, so UNTESTED..

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For r = 1 To lr
    If Range("F" & r).Value = "Freight" Then
        Range("H" & r).Formula = "=VLOOKUP($E" & r & ",'Summary Invoicing'!$F:$P,11,FALSE)"
        Range("I" & r).Formula = "=VLOOKUP($E" & r & ",'HJ Tracking Numbers'!$B:$F,5,FALSE)"
    End If
Next r
End Sub

Hi Michael,

Like I already said, this worked perfectly! I am trying to learn more and more so that someday I could do this all on my own so, I was wondering if you or anyone else on here can explain to me exactly what every command in this code is actually doing. Thanks again!
 
Upvote 0
Do the comments next to each line help ??
If you paste this in your module, it will still work, but also provides comments in green text

Code:
Sub MM1()
Dim lr As Long, r As Long '**********declare your variables
lr = Cells(Rows.Count, "B").End(xlUp).Row ' ************this finds the last row in column "B"
For r = 1 To lr ' **********this loops through each row from 1 to the lastrow
    If Range("F" & r).Value = "Freight" Then '*************look in column "F" on each row in the loop to see if it contains "Freight"...If it does then process the next 2 lines
        Range("H" & r).Formula = "=VLOOKUP($E" & r & ",'Summary Invoicing'!$F:$P,11,FALSE)" '**********place the formula on each row in column "H"
        Range("I" & r).Formula = "=VLOOKUP($E" & r & ",'HJ Tracking Numbers'!$B:$F,5,FALSE)" '***********place the formula on each row in column "I"
    End If '**********we have finished that if statement..move to the next line
Next r '*****************tells us to go back to the start of the loop and do the next row
End Sub
 
Last edited:
Upvote 0
Do the comments next to each line help ??
If you paste this in your module, it will still work, but also provides comments in green text

Code:
Sub MM1()
Dim lr As Long, r As Long '**********declare your variables
lr = Cells(Rows.Count, "B").End(xlUp).Row ' ************this finds the last row in column "B"
For r = 1 To lr ' **********this loops through each row from 1 to the lastrow
    If Range("F" & r).Value = "Freight" Then '*************look in column "F" on each row in the loop to see if it contains "Freight"...If it does then process the next 2 lines
        Range("H" & r).Formula = "=VLOOKUP($E" & r & ",'Summary Invoicing'!$F:$P,11,FALSE)" '**********place the formula on each row in column "H"
        Range("I" & r).Formula = "=VLOOKUP($E" & r & ",'HJ Tracking Numbers'!$B:$F,5,FALSE)" '***********place the formula on each row in column "I"
    End If '**********we have finished that if statement..move to the next line
Next r '*****************tells us to go back to the start of the loop and do the next row
End Sub


This is excellent! Thank you so much Michael!
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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