Solved

Word 2010 Table  convert 10 to 10.00 via VBA Macro is this possible? 500 pnts

Posted on 2014-07-30
3
333 Views
Last Modified: 2014-08-14
Ok so I have a word table when a user enter 10 I want the to be able to run a macro and have the figure automatically convert to 10 to 10.00 via VBA Macro.

So user enter 10 and the run macro and it converts to 10.00

Easy to get them to just type it but I have my reason for doing this

500pnts - Easy for someone out there I reckon..

Thanks

Ian...
0
Comment
Question by:ise438
[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 Comments
 
LVL 15

Expert Comment

by:DrTribos
ID: 40230817
Easy, but there are a few ways this could be done and picking a way that will help you is difficult because it is not clear what the underlying intention of the question is.  

For example, if the user has the discipline to run the macro, they why not just enter 10.00 instead of 10.  Or is it the case that you have a document full of 10's that you wish to replace with 10.00's

Will the 10 be the only thing in the cell?

A basic approach is shown in the attached, you will need to rename .zip to .docm

Option Explicit

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "Ten" Then
    If ContentControl.Range.Text = "10" Then ContentControl.Range.Text = "10.00"
End If
End Sub	

Open in new window

10.zip
0
 

Author Comment

by:ise438
ID: 40231207
Ok fair point...

Bit of background - I am tweaking and invoice for the company I work, we need to use word instead of excel because only word integrate with our internal system.

The table uses a macro to update the calculation field so they need to run a macro any way.

But there is another macro that control is VAT is applied at that point I want VBA to check the contents of a cell and if it 10 or 15 or 20 change it to 10.00, 15.00 or 20.00. So format the cell so to speak it easy in Excel but not in word.

I know I can reference a cell in a table by using the active table command and the column and row etc

Does that help?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40231376
I guess that Steve is in bed.

 You can format cells without VBA using fields, e.g. { SUM(ABOVE) \# "#,##0.00" }

With VBA, you don't have to select the table to read and/or write data to one of its cells. You can find it in several ways ways, including its Index number. Every table has one - it's the order in the document. Our you can use bookmarks, look for some unique data etc. However, this sample code would work on the first table in the selection.

Option Explicit

Sub FormatCellData()
Dim strText As String
    strText = GetCellText(Selection.Tables(1).Cell(1, 1))
    Select Case strText
        Case "10", "15", "20"
            Selection.Tables(1).Cell(1, 1).Range.Text = strText & ".00"
    End Select
End Sub

Function GetCellText(cl As Cell) As String
    Dim rng As Range
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1 'drop cell delimiter
    GetCellText = rng.Text
End Function

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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