Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert VBA String to Double

I am using the very basic coding of VBA for word to create a template that pulls data from other screens in windows. When it pulls the numbers, they are formatted as strings. I now need to get the strings to be converted to doubles, in order to add/ subtract them. I have been trying everything but cannot seem to figure it out.

Me.salesprice = Trim(scrn.GetString(11, 65, 10))
'This would be formatted as 25,000.00
Me.salestax = Trim(scrn.GetString(12, 66, 10))
Me.pastdue = Trim(scrn.GetString(14, 65, 10))
Me.assessedppt = Trim(scrn.GetString(18, 66, 10))
Me.secdep = Trim(scrn.GetString(17, 65, 10))

assessedppt = Convert.ToDouble(Me.assessedppt)
uappt = Convert.ToDouble(Me.uappt)
salesprice = Convert.ToDouble(Me.salesprice)
salestax = Convert.ToDouble(Me.salestax)
pastdue = Convert.ToDouble(Me.pastdue)
lc = Convert.ToDouble(frmDetails.lc)

totalfinance = salesprice + salestax + pastdue - secdep + assessedppt + uappt + lc
totalsalesprice = salesprice + pastdue
ppt = assessedppt + uappt

When I do this, I get the following error:

Compile error: Variable not defined, and it highlights the first Convert function.

like image 975
c.olson Avatar asked Feb 27 '17 19:02

c.olson


1 Answers

You're using the wrong function to convert. You need to use CDbl, in VBA we have the follow convert functions:

numberDouble = CDbl("10") 'For convert to double
numberInteger = CInt("12") 'For convert to Integer
varString = CStr("11") 'For convert to String
bool = CBool("true") 'For convert to Boolean

So if you change your Convert.toDouble, your code will looks like that:

Me.salesprice = Trim(scrn.GetString(11, 65, 10))
'This would be formatted as 25,000.00
Me.salestax = Trim(scrn.GetString(12, 66, 10))
Me.pastdue = Trim(scrn.GetString(14, 65, 10))
Me.assessedppt = Trim(scrn.GetString(18, 66, 10))
Me.secdep = Trim(scrn.GetString(17, 65, 10))

assessedppt = CDbl(Me.assessedppt.value)
uappt = CDbl(Me.uappt.value)
salesprice = CDbl(Me.salesprice.value)
salestax = CDbl(Me.salestax.value)
pastdue = CDbl(Me.pastdue.value)
lc = CDbl(frmDetails.lc.value)

totalfinance = salesprice + salestax + pastdue - secdep + assessedppt + uappt + lc
totalsalesprice = salesprice + pastdue
ppt = assessedppt + uappt
like image 71
Guilherme Felipe Reis Avatar answered Oct 06 '22 23:10

Guilherme Felipe Reis