vba - Invoicing in Word document -


i'm trying write vba script multiply figures exist in table in ms word.

so far i've managed below value of b appears £11,000 in document , when message out returns 0.

i'd value of b 11000 can multiply, ideas or written code great.

private sub calculate_click()      dim t1 table     dim double     dim m, b currency      ' first table     set t1 = activedocument.tables(1)      = val(t1.cell(5, 3).range.text)     = a/100      b = val(t1.cell(7, 4).range.text)     'b = ccur(t1.cell(7, 4).range.text)   **i tried didn't work     msgbox b      m = * b     t1.cell(5, 4).range.text = format(m, "currency")   end sub 

your code perfect - until tried put icing on cake. :-) dim double, b double because don't want multiply apples oranges. m = * b gives correct result, converted currency divides 100. perhaps done in line, t1.cell(5, 4).range.text = format(m, "currency"). don't know. suggest declare m double , format result format(m, "#,##0.00")

here complete suggested code.

private sub calculate_click()      dim double, b double, m double     dim ccy string      activedocument.tables(1)             ' first table         = cellvalue(.cell(5, 3))         b = cellvalue(.cell(7, 4), ccy)         m = * b / 100         if ccy = "" ccy = chrw(163)         if len(ccy) > 1 ccy = ccy & " "         .cell(5, 4).range.text = format(m, ccy & "#,##0.00") '        .cell(5, 4).range.text = format(m, "currency")     end end sub  private function cellvalue(target cell, _                            optional ccy string) double      dim numbers string     dim txt string, ch string     dim long, n long      txt = replace(trim(target.range.text), chr(13), "")     n = len(txt)     while < n         = + 1         ch = mid(txt, i, 1)         if isnumeric(ch) exit     loop     if ccy = trim(left(txt, - 1))      txt = mid(txt, i)     n = len(txt)     = 1 n         ch = mid(txt, i, 1)         if isnumeric(ch) or (ch = ".") numbers = numbers & ch     next     cellvalue = val(numbers) end function 

chrw(163) £ sign. have added number format output. difference between using line or 1 "currency" format "currency" format defined in regional settings" while other line defines see it.

the function cellvalue pick currency used in price cell , apply total cell. if currency multi-character abbreviation (like gbp) space added. if no currency given £ used, without space.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -