Solved

VB script Microsoft Word Tables.  Removing all markup from numeric value in a cell

Posted on 2016-09-05
6
47 Views
Last Modified: 2016-09-05
How do I remove the extra stuff in a Word Table cell (see Screenshot) so that only the numeric value is left.

Something like  Replace(tbl.cell(r,c).Range.Text,"stuff I don't want","")

Screenshot of msgbox showing extra mark up that is causing the following script to err because it can not convert the number value in the cell to a CDbl.  The values in the cells are numbers.  Somehow Word adds bullet points and line breaks

Screenshot:
http://www.screencast.com/t/tOY9cjLi

Script:
tbl.cell(row,2).Range.Text = AccountingFormat(FormatNumber( CDbl(replace(tbl.cell(row-2,c).Range.Text, "-","0")) - CDbl(Replace(tbl.cell(row-1,c).Range.Text, "-","0")) ,0))


Here is a sample of the code that is populating the Cell in the first place:
tbl.cell(newrow.index,2).range.text = AccountingFormat(split(http.responseText,"~")(1))

AccountingFormat function is just replacing 0 with a - symbol.
0
Comment
Question by:maxdbase
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:maxdbase
ID: 41785045
Even though the screen shot shows two numbers, there is only one number and the bullet point in each cell.  I msgbox'ed two separate cells.  The bullet point and line break seem to be what is causing the issue.  I just need the number without the excess mark up stuff.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41785129
It isn't clear what your environment is.

You say VbScript, but AccountingFormat is an Excel function

In Word VBA, this seems to work with your example:
MyNumber = val(replace(mytable.Cell(1,1).Range.Text,",",""))

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41785133
If that doesn't work, can you post an example in a Word document, rather than a picture, please?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:maxdbase
ID: 41785146
I am using a vbs vb script.
I created my own AccountingFormat() function in the vbs file
I shall try the val idea in the vbs vb script and see if that removes
the unwanted bullet point and carriage return that shows up in the msgbox I screenshotted.
0
 
LVL 31

Accepted Solution

by:
Helen Feddema earned 500 total points
ID: 41785206
I have a procedure that I use to strip nonalpnanumeric characters from strings, for various purposes.  I made a version of it that strips nonnumeric characters; you could use this to replace the current contents of a cell with just the numbers:

Public Function StripNonNumericChars(strText As String) As String
'Strips a variety of non-alphanumeric characters from a text string
'Created by Helen Feddema 10-15-97
'Modified by Ruud H.G. van Tol 6-18-99
'Modified by Brad Beacham 6-Feb-2005
'Last modified by Helen Feddema 5-Sep-2016

On Error GoTo ErrorHandler

   Dim strTestString As String
   Dim strChar As String
   Dim i As Integer
   Dim strStripChars As String
   Dim lngChars As Long
   
   strTestString = strText

   For i = 1 To Len(strTestString)
      strChar = Nz(Mid(strTestString, i, 1))
      Debug.Print "Testing " & strChar
      
      If strChar = "" Then
         GoTo EndString
      ElseIf Asc(strChar) < 48 Or Asc(strChar) > 57 Then
         strTestString = Replace(strTestString, strChar, vbNullString)
         Debug.Print "Test string: " & strTestString
         i = i - 1
      End If
   Next i

EndString:
   StripNonNumericChars = strTestString
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in StripNonNumericChars procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Open in new window

0
 

Author Closing Comment

by:maxdbase
ID: 41785281
Great option.  I modified it to fit vb script and fine tuned it a bit due to the possibility of a - which needs to become a 0.  You got me heading a direction that meets the requirements.  And, I like how you started out with the function then kept improving it over time.  Appreciate the help!

Here's what I have so far, I'll likely keep adding to it.  I ultimately want to make some sort of plug in for Microsoft Word Tables similar to jquery that can traverse the table DOM.

For now, this is a great start!

Function StripNonNumericChars(strText)

   Dim strTestString
   
   strTestString = strText

   For i = 1 To Len(strTestString)
   
     
      strChar =Mid(strTestString, i, 1)
      if strChar = "-" then
      strChar = "0"
      end if
      if Asc(strChar) >= 48 and Asc(strChar) <= 57 Then
        StripNonNumericChars = StripNonNumericChars & strChar
      End If
   Next
   

End Function
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question