setting table array in vlookup using find

jkicker

Board Regular
Joined
Jun 7, 2018
Messages
79
Hi all,
New to VBA, learning a lot already. Here's my situation:
I'm trying to write a vlookup where I know what the text in the first and last cells in my table array are, but not their cell address.
I've tried to fix it by using a variable for my table array.

Code:
Sub vl()

Dim vl1 As Range
Set vl1 = Range("A:A").Find("C.I.")

Dim vl2 As Range
Set vl2 = Range("B:B").Find("17200")

Dim vl1a As Range
set vl1a = range(&vl1, &vl2)


MsgBox vl1a


ActiveCell.Formula = "=VLOOKUP(G5," & vl1a & ",2)*H5"


End Sub

I get "compile error: expected: expression" at the & when setting vla1. i'll accept other solutions. the goal is for the table array portion of vlookup to find itself based on the fact that the first cell will say C.I. and the last cell will say 17200. these cells will be located in A:A and B:B respectively
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this.
Code:
Option Explicit

Sub vl()
Dim vl1 As Range
Dim vl2 As Range
Dim vl1a As Range

    Set vl1 = Range("A:A").Find("C.I.")

    Set vl2 = Range("B:B").Find("17200")

    Set vl1a = Range(vl1, vl2)

    MsgBox vl1a.Address

    ActiveCell.Formula = "=VLOOKUP(G5," & vl1a.Address & ",2)*H5"

End Sub
 
Upvote 0
I wasted a day on a problem that was solved in under a minute. Thank you so much. Can i give you internet points somehow?
 
Upvote 0
well, it was working until i added it to the rest of my code. Run-time error '1004': method 'range' of object '_global' failed
 
Upvote 0
i changed the formula to r1c1 format

Code:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2]," & vl1a.Address & ",2)*RC[-1]"
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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