Create formula below specific text

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi all, i would like to create a VBA code so that to run through row "1" and where find the text "Bank list' should create the below formula:

"=VLOOKUP(RC[-10],'[PAYMENT LIST.xlsx]Sheet1'!R4C1:R5000C32,25,0)"

Kindly note that the formula should begin from row "2" till last fulfill row based on col. "K".
Below is an extract of my spreadsheet in which i want to add the above formula below Bank list column.

Thanking you in advance


KLM
1CODEBank list
Variance
22002
32008

42010
52011

<colgroup><col style="mso-width-source:userset;mso-width-alt:2389;width:50pt" width="67"> <col style="mso-width-source:userset;mso-width-alt:2844;width:60pt" width="80"> <col style="mso-width-source:userset;mso-width-alt:4380; width:92pt" width="123" span="2"> </colgroup><tbody>
</tbody>
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Dim colNum As Integer

With Workbooks(NAME).Worksheets(NAME) 'edit names
LRow = .Cells(.Rows.Count, "K").End(xlUp).Row

colNum = WorksheetFunction.Match("Bank list", ActiveWorkbook.Sheets(sheetname).Range("1:1"), 0) 'edit the sheet name
.Range(Cells(2,colNum), Cells(LRow,colNum).FormulaR1C1 = "=VLOOKUP(RC[-10],'[PAYMENT LIST.xlsx]Sheet1'!R4C1:R5000C32,25,0)"

end with

Let me know if this helps you
 
Upvote 0
Hi Tim, It doesn't work. It appears a message which write the belows:

Compile error
Syntax error
Also the below command appears as red.

.Range(Cells(2,colNum), Cells(LRow,colNum).FormulaR1C1 = "=VLOOKUP(RC[-10],'[PAYMENT LIST.xlsx]Sheet1'!R4C1:R5000C32,25,0)"

Many thanks for your support
 
Last edited:
Upvote 0
Code:
Dim colNum As Integer


With ThisWorkbook.Worksheets(1) 'edit references to your preferences
LRow = .Cells(.Rows.Count, "K").End(xlUp).Row


colNum = WorksheetFunction.Match("Bank list", .Range("1:1"), 0) 
.Range(.Cells(2, colNum), .Cells(LRow, colNum)).FormulaR1C1 = "=VLOOKUP(RC[-10],'[PAYMENT LIST.xlsx]Sheet1'!R4C1:R5000C32,25,0)"




End With

Use this line instead, tired Tim wrote the first code which has been improved by lesser tired Tim. Tested and works.
 
Upvote 0
Hi Tim, it blocks on the below point.

colNum = WorksheetFunction.Match("Bank list", .Range("1:1"), 0)

Please note that i prefer it to run on "ActiveWorkbook" and "ActiveSheet" not using workbook name or sheets names.

Thanks once again
Hv a nice day
 
Last edited:
Upvote 0
What exactly is the error it is giving you? If it is a Subscrip Out of Range error, then you may check if the value you are looking for is actually "Bank list". This error would mean Excel is not finding that value in the top row.

Furthermore, I would not recommend using Activeworkbook, as it is asking for errors. I would endorse the use of variables or the actual name if referring to anything.
 
Upvote 0
Apologies Tim, i had a mistake to description Bank List. i had "List" with capital the first letter. it works perfect now and thank you so much for your time spent for my project. Hv a great day!
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,803
Members
449,337
Latest member
BBV123

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