Run-time error ' 1004': Application-defined or object defined error

dversloot1

Board Regular
Joined
Apr 3, 2013
Messages
113
I'm trying to get the ROW number from a specific sheet where a cell value has been found.
Cnt = 5
ID = 479
Row = 0 --> This should return 19 but for some reason it is returning a 0.

My code is:

Private Sub CommandButton1_Click()
Dim ID As Variant
Dim Row As Long
Dim Cnt As Long
Dim Analyst As String

Cnt = InStr(1, ListBox2.Value, "-")
ID = Trim(Left(ListBox2.Value, Cnt - 1))
On Error Resume Next
Row = Application.WorksheetFunction.Match(ID, Sheets("Data").Range("B1:B2000"), 0)
On Error GoTo 0

Analyst = "A" & Row

Sheets("Sheet1").Range("B4").Value = Sheets("Data").Range(Analyst).Value


End Sub

Anyone know why this is the case?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Trim and Left functions return TEXT strings, even if they are numeric characters.

Try changing
ID = Trim(Left(ListBox2.Value, Cnt - 1))
to
ID = Val(Trim(Left(ListBox2.Value, Cnt - 1)))
 

dversloot1

Board Regular
Joined
Apr 3, 2013
Messages
113

ADVERTISEMENT

The VAL() doesn't seem to work when the ID is alphanumeric.
Thoughts?
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Inconsistent data is the root of all evil in Excel.

So sometimes the ID will be a number, and sometimes it will be a Text String ?

Try
ID = Trim(Left(ListBox2.Value, Cnt - 1))
If IsNumeric(ID) Then ID = Val(ID)
 

dversloot1

Board Regular
Joined
Apr 3, 2013
Messages
113
Luckily there was a way around this inconsistent data.

Thanks again! Your solution worked.
 

Forum statistics

Threads
1,137,351
Messages
5,680,988
Members
419,948
Latest member
Sbakker1

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