Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-05
6
Medium Priority
?
55 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

650 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