Keeping trailing zero's when transferring from excel to word VBA

Madter

New Member
Joined
Oct 16, 2017
Messages
24
Hi.
I have this VBA code, where i pull data from word and then i transfer other data back to word.

I have this code:
Code:
Call findAndReplace(Replace(Round(vbaSheet.Cells(i, "U").Value, 2), ",", "."), "Freight:", 3)

And this code for example takes this from excel "155,40" and returns it to word as this "155.4".

This problem only occurs when there's a zero at the end of the decimals. But i want the number to be transferred as "155.40".
What can i add to this?

Thank you so much in advance
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe trial...
Code:
Call findAndReplace(Replace(CSTR(Round(vbaSheet.Cells(i, "U").Value, 2)), ",", "."), "Freight:", 3)
HTH. Dave
 
Upvote 0
Hi, this is just a guess, but is your decimal separator a comma? If so maybe you could try:

Code:
Call findAndReplace(Replace(Format(vbaSheet.Cells(i, "U").Value, "0,00"), ",", "."), "Freight:", 3)
 
Last edited:
Upvote 0
Personally, I'd skip the to-ing & fro-ing with Excel and use something like:
Call FindAndReplace("Freight:")
with:
Code:
Private Sub FindAndReplace(strFnd As String)
With wdDoc.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strFnd & "[$€£ 0-9,.]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    Do While .Characters.Last Like "[,. ]"
      .End = .End - 1
    Loop
    .Start = .Start + Len(strFnd)
    .MoveStartWhile " ", 1 'wdForward
    If .Characters.First Like "[$€£]" Then .Start = .Start + 1
    .MoveStartWhile " ", 1 'wdForward
    If (Right(.Text, 4) Like "#.##") = False Then
      .Text = Format(Replace(Replace(Replace(.Text, ".", ""), " ", ""), ",", "."), "#,##0.00")
    End If
    .Collapse 0 'wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub
With this approach, any value following 'Freight:' in the Word document will be converted to the #,##0.00 format, including amounts preceded by currency symbols.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,208
Members
448,874
Latest member
Lancelots

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top