Vlookup vba code not working
Can anyone solve my vba Vlookup problem? I have a workbook with multiple worksheets and a user form that has two combo boxes, the first one fills from a named range and when a user selects a value from this combo box it fills another combo box. When a user selects a value from this combo box I need Vlookup to find the value in a named range and column 1 (left) is the same column that fills the combo box so the value that is being searched for has to be identical to the value found but the result always comes back empty?
I'm passing two variables to Vlookup - the search name (from the selected combo box) and an integer for the column offset. The first sub is activated when the user clicks button CmdDetails on the form.
Here is the code
[/CODE]
Private Sub CmdDetails_Click()
Dim Name As String
Dim ColoffSet As Integer
Name = CmbArea.Value
ColoffSet = 5
Call AreaChange(Name, ColoffSet)
End Sub
[/CODE]
[/CODE]
Private Sub AreaChange(Name, ColoffSet)
Dim R As Range
Dim T As Range
Dim Result As Variant
On Error Resume Next
Err.Clear
For Each R In Range("Table")
Result = Application.WorksheetFunction.VLookup(Name, R, ColoffSet, False)
T.AddItem Result
'If Err.Number = 0 Then (I took the error handling out because the vbYesNoCancel button won't cancel and I have to end the program to get out of it using Ctrl Alt Delete, 'but if I leave it in it says value not found)
'Else
'MsgBox ("result not found"), vbYesNoCancel
'End If
Next R
Call AddRoom(T)
End Sub
[/CODE]
'Forget the call to AddRoom... I don't get that far!
If I toggle a breakpoint on the For each line I can see that the name value is being passed to the function (hovering the mouse over the variables) and when it cycles through the table R (named range) I can see that the value is found but not passed to the result variable which remains empty.
Any ideas?
Recent comments
5 years 41 weeks ago
6 years 27 weeks ago
6 years 39 weeks ago
6 years 42 weeks ago
6 years 43 weeks ago
6 years 48 weeks ago
7 years 4 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago