set cell reference using vlookup vba

maxx_daddy

Board Regular
Joined
Dec 3, 2010
Messages
74
Trying to reference a cell using vlookup in vba, this way I can offset from the cell and paste data from a userform text box.

Heres what I thought may work, but it didnt. Thoughts?


Dim acell As RangeSet acell = Application.VLookup(Val(TextBox1.Text), Sheets("thesheet").Range("a1:d42"), 1, False)</pre>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
that worked! thanks a bunch. vlookup vba cell reference, offset, text boxes,

Code:
Private Sub CommandButton1_Click()
' textbox1 is the search field, treat it like a cell for a vlookup
'textbox 2, 3, & 4 will be like vlookup results, in this case the vlookup is a work order # and the results are customer name, address
' and the amount of hours we sold the job for
TextBox2.Text = Application.VLookup(Val(TextBox1.Text), Sheets("thesheet").Range("a1:d42"), 4, False)
TextBox3.Text = Application.VLookup(Val(TextBox1.Text), Sheets("thesheet").Range("a1:d42"), 2, False)
TextBox4.Text = Application.VLookup(Val(TextBox1.Text), Sheets("thesheet").Range("a1:d42"), 3, False)
'acell is the same as the result from the initial vlookup, by setting a refernce to acell we can offset from there to paste additional
' data from a userform, in this case the name of the technician and the actual amount of time it took to complete the job.
Dim acell As Range
Set acell = Sheets("thesheet").Range("A:A").Find(Val(TextBox1.Text))
'ecell will work like any referenced textbox, i will set the refernce cell on the text box to be ecell and kill the refernce when closing.
'bam!
Dim ecell As Range
Set ecell = acell.Offset(0, 5)
ecell.Value = "do it cohagen"
End Sub

This should work for you:
Set acell = Sheets("thesheet").Range("A:A").Find(Val(TextBox1.Text))
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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