Macro code needed for combination of (If,Countif,vlookup)

dharmadme

New Member
Joined
Mar 19, 2014
Messages
17
I am creating multiple macro function workbook. Below formula is required for full fill my macro.
Please provide Macro VB code for below Excel formula

This formula is populate in sheet name "Worksheet 1" vlookup with "Worksheet 2"

=IF(COUNTIF(A1:$A$5000,A1)=1,VLOOKUP(A1,'Worksheet 2'!$A$1:$J$5000,10,0),"")
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I was not sure from your question whether you wanted a macro to carry out the functionality in your formula or whether you wanted the formula to be copied down using a macro. If it is the latter then I have a solution.

If you want the former then I should point out that a macro solution wll generally be slower than a worksheet formula solution.

Code:
Sub InsertFormula()
    Dim ws As Worksheet
    Set ws = Sheet1
    
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim myRng As Range
     
    With ws
        Set my_range = .Range("C1:C" & LastRow)
        myForm = "=IF(COUNTIF(A1:$A$" & LastRow & ",A1)=1,VLOOKUP(A1,Sheet2!$A$1:$J$5000,10,0),"""")"
        my_range.Formula = myForm
    End With
End Sub

If you want something else please add more information.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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