I'm trying to set up a userform where the value chosen in the combobox determines the range of an offset match function. The lookup value is entered within a textbox (TextBox1) in the same userform.
If ComboBox1 = "Something" Then
TryRun = Evaluate("Offset(Sheets('Sheet1').Range('B13'),MATCH(TextBox1.Value,Sheets('Sheet1').Range('B14:B33'),0),5)")
End If
Currently this returns the value of cell G15, what I need it to do however is to simply select that cell and paste the value of Textbox (TextBox2) in the userform.
This doesn't seem to work,
Cells(TryRun).Value = TextBox2,
because it cannot find a defined object for 'TryRun'. However, seeing as this refers to a cell, I wouldn't know what to define it as.
Could anyone help me out?
I've tried Ahmed's U Vlookup solution to get this:
Private Sub OkButton_Click()
If Combobox1 = "Something" Then
TryRun = WorksheetFunction.VLookup(Val(Textbox1.Text), Range("B14:G33"), 6, False)
If IsEmpty(TryRun) Then
Msgbox "nope"
Textbox2.Text = "No Match found"
Else
Msgbox "almost..."
TextBox2.Value = TryRun
End If
End If
End Sub
This results in Textbox2 returning value = 0 within the userform whilst nothing gets pasted through the vlookup function.
By removing the on error lines, the code stops working all together, with the faulty line being TryRun = WorksheetFunction.VLookup(Val(Textbox1.Text), Sheets("Stuff").Range("B14:G33"), 6, False). The error this results in is: Error 424, object required.
EDIT:
After removing Dim TryRun As Long I've come across the next stumbling block: Currently I'm returning "No Match Found" Instead of Try Run's value in the designated cell address.
EDIT 2:
Somehow, I'm returning the second messagebox now in the else statement. Still no value being pasted in TryRun though.
UserFormat all. Nor that your sheet isSheet1,B13,TextBox1, etc. For a MCVE hard-code all these values to something easy to reproduce. Or look at the address formula and try to write it somewhere. Have fun! :) - VityataEvaluateis for running worksheet function formulas, not for VBA. - Tim Williams