This question already has answers here:
How to error handle 1004 Error with WorksheetFunction.VLookup?
(3 answers)
Closed 4 years ago.
I am trying to develop a form to track invoices as they come in. The form will have a combobox where I can click on and select a vendor number. I want the textbox to automatically fill in based on the vendor number selected from the combobox. Here's what I have so far:
Private Sub ComboBox1_Change()
'Vlookup when ComboBox1 is filled
Me.TextBox1.Value = Application.WorksheetFunction.VLookup( _
Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
End Sub
Worksheet 3 is from which the information is being drawn (the vendor number and name).
When I go back to the form to test the code, I get the following error:
Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class
How do I fix this?
Try below code
I will recommend to use error handler while using vlookup because error might occur when the lookup_value is not found.
Private Sub ComboBox1_Change()
On Error Resume Next
Ret = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
On Error GoTo 0
If Ret <> "" Then MsgBox Ret
End Sub
OR
On Error Resume Next
Result = Application.VLookup(Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
If Result = "Error 2042" Then
'nothing found
ElseIf cell <> Result Then
MsgBox cell.Value
End If
On Error GoTo 0
I was having the same problem. It seems that passing Me.ComboBox1.Value as an argument for the Vlookup function is causing the issue. What I did was assign this value to a double and then put it into the Vlookup function.
Dim x As Double
x = Me.ComboBox1.Value
Me.TextBox1.Value = Application.WorksheetFunction.VLookup(x, Worksheets("Sheet3").Range("Names"), 2, False)
Or, for a shorter method, you can just convert the type within the Vlookup function using Cdbl(<Value>).
So it would end up being
Me.TextBox1.Value = Application.WorksheetFunction.VLookup(Cdbl(Me.ComboBox1.Value), Worksheets("Sheet3").Range("Names"), 2, False)
Strange as it may sound, it works for me.
Hope this helps.
I was just having this issue with my own program. I turned out that the value I was searching for was not in my reference table. I fixed my reference table, and then the error went away.
Related
I have read about Application.WorksheetFunction.Sum but I was wondering if there is a specific method or property of the Range-object that does the job. I have looked into Range's members but I didn't find anything.
Is there a way to add without the use of a worksheet function? And without having to loop through each cell. Something like:
Sub test()
Range("A1") = 1
Range("A2") = 0
Range("A3") = 2
Range("A4") = Range("A1:A3").Add
MsgBox Range("A4")
End Sub
With output 3.
go on then, a 4th way :o)
evaluate(join(application.transpose(range("a1:a5").Value),"+"))
In general, there is a third way, which you did not mention:
Application.Sum
It is a bit different than WorksheetFunction.Sum, in the fact that it is a bit more discrete - it does not throw errors with a MsgBox, even when it should.
Something like this will have only 2 errors thrown with MsgBox and you will get 2 errors in the immediate window:
Option Explicit
Public Sub WaysOfSumming()
'This is Div/0 Error:
Range("A1").Formula = "=5/0"
Debug.Print Excel.WorksheetFunction.Sum(Range("A1"), 3, 5)
Debug.Print Application.Sum(Range("A1"), 3, 5)
Debug.Print Excel.WorksheetFunction.Sum(Range("A1"), 3, 5)
Debug.Print Application.Sum(Range("A1"), 3, 5)
End Sub
I am creating a userform where based on a drop down list of items called "ContractsList" I would like a Vlookup formula to return a text data in "TextBox 1".
I get an error message saying "Run-time error'1004': Unable to get the Vlookup property of the worksheetfunction class
Not sure what I am doing wrong, here is my code if anyone can spot the error.
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Sheet2.Range("A:A"),Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.TextBox1, ("B5:B72"), 2, 0)
End With
End Sub
Finally got it to work as below:
Private Sub ContractsList_AfterUpdate()
If WorksheetFunction.CountIf(Sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Sheet2.Range("A5:E72"), 2, 0)
End With
End Sub
I just needed to add "Sheet2.Range("A5:E75")
Thank you all for your help.
I keep getting this error when I am running my code and I am unsure where else to look.
The Error message Reads: Run-Time Error '1004' - Unable to get the Vlookup Property of the WorksheetFunction Class
Here is my code, varMajor is declared as a global variable in another worksheet. Any help would be greatly appreciated!
Private Sub cmdRank_Click()
Dim strMajorRank As String
'Declare worksheet as an object variable
Dim shtRankings As Worksheet
'Set the variable
Set shtRankings = Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings")
varMajor = InputBox("Please Enter Your Major", "Where Does Your Major Rank?", vbOKOnly)
'Set rngMajorRank = Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings").Range("H2")
'Check to see if the major entered is listed in the Majors column in the data on the Rankings Worksheet using VLookup
If varMajor = "Finance" Then
strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
MsgBox "Your major is among the 20 most popular at CU!"
ElseIf varMajor = "Management" Then
strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
MsgBox "Your major is among the 20 most popular at CU!"
ElseIf varMajor = "Marketing" Then
strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
MsgBox "Your major is among the 20 most popular at CU!"
Else
MsgBox "Your major is still a good one to have!"
End If
'Go to worksheet with top 20 majors at CU Application.Workbooks("MGMT 3210 Final Project.xlsm").Worksheets("Rankings").Activate
shtRankings.Range("H2").Value = "strMajorRank"
End Sub
So it looks like your syntax is correct, this error actually just means that the Vlookup is not returning a value, i.e. in Excel this Vlookup would return N/A#, you can handle this with error handling. If the formula is correct you'd want to put "On Error Resume Next" and "On Error Goto 0" around your uses of Vlookup.
Your code's a bit unclear, assuming you're trying to determine if the value the user enters is in the list as you say, then it should be:
'Check to see if the major entered is listed in the...
On Error Resume Next 'Goes to next line of code on error
strMajorRank = Application.WorksheetFunction.VLookup(varMajor, shtRankings.Range("B2:B21"), 1, True)
On Error Goto 0 'Returns error handling to default
If strMajorRank <> "" Then
MsgBox "Your major is among the 20 most popular at CU!"
Else
MsgBox "Your major is still a good one to have!"
End If
...
This question already has answers here:
How to error handle 1004 Error with WorksheetFunction.VLookup?
(3 answers)
Closed 4 years ago.
I am trying to develop a form to track invoices as they come in. The form will have a combobox where I can click on and select a vendor number. I want the textbox to automatically fill in based on the vendor number selected from the combobox. Here's what I have so far:
Private Sub ComboBox1_Change()
'Vlookup when ComboBox1 is filled
Me.TextBox1.Value = Application.WorksheetFunction.VLookup( _
Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
End Sub
Worksheet 3 is from which the information is being drawn (the vendor number and name).
When I go back to the form to test the code, I get the following error:
Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class
How do I fix this?
Try below code
I will recommend to use error handler while using vlookup because error might occur when the lookup_value is not found.
Private Sub ComboBox1_Change()
On Error Resume Next
Ret = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
On Error GoTo 0
If Ret <> "" Then MsgBox Ret
End Sub
OR
On Error Resume Next
Result = Application.VLookup(Me.ComboBox1.Value, Worksheets("Sheet3").Range("Names"), 2, False)
If Result = "Error 2042" Then
'nothing found
ElseIf cell <> Result Then
MsgBox cell.Value
End If
On Error GoTo 0
I was having the same problem. It seems that passing Me.ComboBox1.Value as an argument for the Vlookup function is causing the issue. What I did was assign this value to a double and then put it into the Vlookup function.
Dim x As Double
x = Me.ComboBox1.Value
Me.TextBox1.Value = Application.WorksheetFunction.VLookup(x, Worksheets("Sheet3").Range("Names"), 2, False)
Or, for a shorter method, you can just convert the type within the Vlookup function using Cdbl(<Value>).
So it would end up being
Me.TextBox1.Value = Application.WorksheetFunction.VLookup(Cdbl(Me.ComboBox1.Value), Worksheets("Sheet3").Range("Names"), 2, False)
Strange as it may sound, it works for me.
Hope this helps.
I was just having this issue with my own program. I turned out that the value I was searching for was not in my reference table. I fixed my reference table, and then the error went away.
I have this code:
Sub addDropdown(Name)
ActiveSheet.DropDowns.Add(74.25, 60, 188.25, 87.75).Select
Set n = ActiveSheet.DropDowns(Name)
If Not (n Is Nothing) Then
ActiveSheet.DropDowns(Name).Delete
End If
With Selection
.ListFillRange = "$K$15:$M$19"
.LinkedCell = "$K$8:$L$11"
.DropDownLines = 6
.Display3DShading = False
.Name = Name
End With
ActiveSheet.DropDowns(Name).Display3DShading = True
End Sub
Which results in "Runtime error 1004: Unable to get the DropDowns property of the Worksheet class"
I am a VBA noob, so why is it refering to a property? According to the Object Browser DropDowns is a function (although that doesnt rime with the .Add later on).
Also, I can access this exact thing later on after having added something to DropDowns. I just dont get it.
What I want to do, is to delete any pre-existing dropdown with the same name.
You need to handle the error if the named Dropdown does not exist
on error resume next
Set n = ActiveSheet.DropDowns(Name)
on error goto 0