How do you convert a string to a double?

  • Thread starter Thread starter Philosophaie
  • Start date Start date
P

Philosophaie

How do you convert a string to a double? This does not work:

string1 = Right(Left(data(k), 21), 2)
double1 = CDbl(string1)

It gives a "Type mismatch" error every time.
 
Hi,

I think you need to tell us what data(k) is holding but in the meantime isn't

string1 = Mid(data(k), 20, 2)

the same as
string1 = Right(Left(data(k), 21), 2)
and a bit more simple

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
Philosophaie said:
How do you convert a string to a double? This does not work:

string1 = Right(Left(data(k), 21), 2)
double1 = CDbl(string1)

It gives a "Type mismatch" error every time.

What is the literal value of string1 ?

string1 = "1.23456789E+123"
double1 = CDbl(string1)

Works fine every time. But

string1 = "1.23456789 E+123"

will fail every time.

It doesn't tolerate embedded spaces in E format numbers.

Regards,
Martin Brown
 
I meant to give an example.

Suppose Data(k) is holding the value below then your code works fine. If the
2 character as positions 20 & 21 aren't numeric then 'Type Mismatch'

data(k) = "abcdefghilklmnopqrs99vwyz"
string1 = Mid(data(k), 20, 2)
double1 = CDbl(string1)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
The short answer is to make sure that String1 equates to a number without
spaces. You cannot use CDbl with data that VBA sees as text.
 
Back
Top