How do you convert a string to a double?

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.
 
M

Mike H

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.
 
M

Martin Brown

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
 
M

Mike H

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.
 
J

JLGWhiz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top