2800 not equal to "2800" ?

YCJ

New Member
Joined
Feb 28, 2011
Messages
29
Hi there

I'm puzzled with the value that I got from my active cell.
When my cell contains "2800" (without the double quotes), and my coding is
Code:
freq = ActiveCell.value
the result will be 2800.
But when my cell contains "2800-7200" (without the double quotes), and my coding is still
Code:
freq = ActiveCell.value
the result will be "2800-7200". (with double quotes)

I've separated the "2800-7200" using
Code:
intPos = InStr(1, freq, "-", vbTextCompare)
freq1 = Mid(freq, 1, intPos - 1)
freq2 = Mid(freq, intPos + 1)
In this case, my freq1 is "2800" and freq2 is "7200",
I need to use these two frequency to look through a list of frequencies.
For example,
Code:
If ActiveCell.value = freq1 Then
This will result in 2800 = "2800", and it is seen as not fulfilling the condition.

Where does the double quotes "" come out from? :confused:

CJ
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Mid returns a text value. Try like this

Code:
freq1 = Val(Mid(freq, 1, intPos - 1))
 
Upvote 0
2800-2700 in the cell is a string of characters not a numeric result so you end up comparin the string "2800" to the numeric value 2800 and VBA says they don't match. You can convert the string into a numeric value eg by using CInt or CDbl:

Code:
If CInt(Activecell.Value) = 2800 Then '...

and

Code:
freq1 = CInt(Split(freq,"-")(0))

freq2 =  CInt(Split(freq,"-")(1))
 
Upvote 0
Wow, works like a charm. Thanks guys for your prompt reply, at least I know what's wrong now. :biggrin: Have a nice day!

Cheers,
CJ
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

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
Back
Top