[VBA] Decimal place of number in string

Dajnoxes

New Member
Joined
Aug 7, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have string:

Line from Point N50424 to N52525 has fixed length. Line diameter 4.5 Max Line diameter 6.166666666625

Except this string I have a lot of different strings in my sheet but I have issue only with this one because after every export csv file
Max Line diameter is changing,

Max Line diameter 6.166666666625
sometimes it.s 6.16, next time it wil be 6.1666666666666662525

Can I somehow make it in VBA that will change this string to have 2 place decimal

for example:
when Max Line Diameter 6.1111111111 it will change to 6.11
when Max Line Diameter 3.5934432424 it will change to 3.59

I hope you understand what I mean :D

Best Regards,
Dajnoxes
 
See what this one does:
VBA Code:
Sub FormatTest3()
    Dim MyStr As String
    Dim X As Variant
    Dim S As String
    Dim SA As Variant, D As Double
  
    X = Range("A1").Value
    Debug.Print "Cell Data is type " & TypeName(X)
  
    MyStr = Trim(CStr(X))
    Debug.Print "MyStr = '" & MyStr & "'"
    Debug.Print "Last Char = " & Asc(Right(MyStr, 1)) & " ('" & Right(MyStr, 1) & "')"
  
    SA = Split(MyStr, " ")
    X = SA(UBound(SA))
    Debug.Print "X = " & X & " (Type " & TypeName(X) & ")"
  
    D = CDbl(X)
    Debug.Print "D = " & D & " (Type " & TypeName(D) & ")"
    Debug.Print "Rounded D = " & Round(D, 2)
  
    MyStr = Replace(MyStr, SA(UBound(SA)), Round(D, 2))
    Debug.Print "MyStr (after)  = '" & MyStr & "'"
  
    Range("A2").Value = MyStr
End Sub

(note: everything I've posted so far works without error for me. On 2 different PCs)
I'm still getintg the same error :(

Error line: D = CDbl(X)

Cell Data is type String
MyStr = 'Line from Point N50424 to N52525 has fixed length. Line diameter 4.5 Max Line diameter 6.166666666625'
Last Char = 53 ('5')
X = 6.166666666625 (Type String)
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Replace it with
VBA Code:
    D = Val(X)

And report results.
It works,
Cell Data is type String
MyStr = 'Line from Point N50424 to N52525 has fixed length. Line diameter 4.5 Max Line diameter 6.166666666625'
Last Char = 53 ('5')
X = 6.166666666625 (Type String)
D = 6,166666666625 (Type Double)
Rounded D = 6,17
MyStr (after) = 'Line from Point N50424 to N52525 has fixed length. Line diameter 4.5 Max Line diameter 6,17'

Line from Point N50424 to N52525 has fixed length. Line diameter 4.5 Max Line diameter 6,17

Thanks for your help :)
 
Upvote 0
Rounded D = 6,17

So your PC is using the comma as the decimal separator instead of the period? That explains a lot. Why did your original post show only periods being used?
 
Upvote 0
So your PC is using the comma as the decimal separator instead of the period? That explains a lot. Why did your original post show only periods being used?
I changed manually separator in Excel to period but originally I have comma
When I write 1.15 it sees as decimal separator.
Thankfully, it works now :)
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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