Replace function for partial characters

GerrardSVK

New Member
Joined
Sep 18, 2023
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone could someone help me I just need to write simple macro that will change number from this format: 1.234 into format using comma instead of dot : 1,234
Also I need to change sign of degrees into nothing. So if there will be some number like this 1.234° final result will be 1,234.

I tried this but dont work:
Sub Replace()

Workbooks(2).ActiveSheet.Range("G9:G136").Replace What:=".", Replacement:=","
Workbooks(2).ActiveSheet.Range("G9:G136").Replace What:="°", Replacement:=""

End Sub

I think there have to be additional xlPart defined or something.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This is the best way I can do:
VBA Code:
Sub test()
  Call ReplaceDecimal(Range("G9:G136"))
  Call ReplaceDegree(Range("G9:G136"))
End Sub
Sub ReplaceDecimal(rng As Object)
  For Each r In rng
    r.Value = "'" & Replace(r.Text, ",", ".")
  Next
End Sub
Sub ReplaceDegree(rng As Range)
  For Each r In rng
    r.Value = "'" & Replace(r.Text, "°", "")
  Next
End Sub
 
Upvote 0
This is the best way I can do:
VBA Code:
Sub test()
  Call ReplaceDecimal(Range("G9:G136"))
  Call ReplaceDegree(Range("G9:G136"))
End Sub
Sub ReplaceDecimal(rng As Object)
  For Each r In rng
    r.Value = "'" & Replace(r.Text, ",", ".")
  Next
End Sub
Sub ReplaceDegree(rng As Range)
  For Each r In rng
    r.Value = "'" & Replace(r.Text, "°", "")
  Next
End Sub
r is not defined what is it?
 
Upvote 0
Ok try lşke this
VBA Code:
Sub test()
  Call ReplaceDecimal(Range("G9:G136"))
  Call ReplaceDegree(Range("G9:G136"))
End Sub
Sub ReplaceDecimal(rng As Object)
  Dim r as Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, ",", ".")
  Next
End Sub
Sub ReplaceDegree(rng As Range)
  Dim r as Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, "°", "")
  Next
End Sub
 
Upvote 0
Ok try lşke this
VBA Code:
Sub test()
  Call ReplaceDecimal(Range("G9:G136"))
  Call ReplaceDegree(Range("G9:G136"))
End Sub
Sub ReplaceDecimal(rng As Object)
  Dim r as Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, ",", ".")
  Next
End Sub
Sub ReplaceDegree(rng As Range)
  Dim r as Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, "°", "")
  Next
End Sub
I tried this but I need to define other workbook is this correct? I tried this but didnt work for me.

VBA Code:
Sub test()
  Call ReplaceDecimal(Workbooks(2).ActiveSheet.Range("G9:G136"))
  Call ReplaceDegree(Workbooks(2).ActiveSheet.Range("G9:G136"))
End Sub
Sub ReplaceDecimal(rng As Object)
  Dim r As Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, ",", ".")
  Next
End Sub
Sub ReplaceDegree(rng As Range)
  Dim r As Range
  For Each r In rng
    r.Value = "'" & Replace(r.Text, "°", "")
  Next
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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