Run-time error 1004

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
69
Good morning everyone
i have been having a running battle with Run-time error 1004 on this code, while i had tried many online solutions, yet i am stuck with this error.
Summarily, after trying the vlookup function, i also tried index & match function and get the same error code, so i was thinking may worksheet function dont want to work for my pet project, please kindly help this novice out. below is my vba code and thank you for your support & help.

VBA Code:
Private Sub cmbItemID_Change()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("ProductMaster")

If Me.cmbItemID.Value = "" Then Me.txtRate.Value = ""

Else
    Me.txtRate.Value = Application.WorksheetFunction.VLookup(Me.cmbItemID.Value, sh.Range("B:F"), 5, False)
End If

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,402
Office Version
  1. 365
Platform
  1. Windows
Tell us what is in cmbItemID. Ill guess a number. Make sure you are looking up the same thing. A textual numberfor example will not be found in a list of real numbers by vlookup.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,915
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Private Sub cmbItemID_Change()
Dim sh As Worksheet
Dim x As Variant
Set sh = ThisWorkbook.Sheets("ProductMaster")

If Me.cmbItemID.Value = "" Then Me.txtRate.Value = ""

Else
    x = Application.VLookup(Val(Me.cmbItemID.Value), sh.Range("B:F"), 5, False)
    If Not IsError(x) Then Me.txtRate.Value = x
End If

End Sub
 

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
69

ADVERTISEMENT

How about
VBA Code:
Private Sub cmbItemID_Change()
Dim sh As Worksheet
Dim x As Variant
Set sh = ThisWorkbook.Sheets("ProductMaster")

If Me.cmbItemID.Value = "" Then Me.txtRate.Value = ""

Else
    x = Application.VLookup(Val(Me.cmbItemID.Value), sh.Range("B:F"), 5, False)
    If Not IsError(x) Then Me.txtRate.Value = x
End If

End Sub

Thanks, it returned compile error: Else without If
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,915
Office Version
  1. 365
Platform
  1. Windows
Try
VBA Code:
Private Sub cmbItemID_Change()
Dim sh As Worksheet
Dim x As Variant
Set sh = ThisWorkbook.Sheets("ProductMaster")

If Me.cmbItemID.Value = "" Then
   Me.txtRate.Value = ""
Else
    x = Application.VLookup(Val(Me.cmbItemID.Value), sh.Range("B:F"), 5, False)
    If Not IsError(x) Then Me.txtRate.Value = x
End If

End Sub
 
Solution

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
69
Try
VBA Code:
Private Sub cmbItemID_Change()
Dim sh As Worksheet
Dim x As Variant
Set sh = ThisWorkbook.Sheets("ProductMaster")

If Me.cmbItemID.Value = "" Then
   Me.txtRate.Value = ""
Else
    x = Application.VLookup(Val(Me.cmbItemID.Value), sh.Range("B:F"), 5, False)
    If Not IsError(x) Then Me.txtRate.Value = x
End If

End Sub
Thanks a million, it worked 👍👍👍
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,915
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,214
Messages
5,640,910
Members
417,178
Latest member
HelpMeExcelExperts

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
Top