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.
 
@GerrardSVK
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in post #10 for you this time. 😊
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Your method of referring to your sheet with the values you need to change is messy. You should always strive to be explicit with your object references. Please try the following - just change the workbook/worksheet names to suit.
VBA Code:
Sub Fix_Format()
    Dim wb As Workbook, ws As Worksheet
    Set wb = Workbooks("Workbook_Name")         '<-- *** Change to actual workbook name ***
    Set ws = wb.Worksheets("Sheet1")            '<-- *** Change to actual worksheet name ***
        With ws.Range("G9:G136")
            .Value2 = Evaluate("Substitute(Substitute(" & .Address(, , , 1) & ",""°"",""""),""."","","")")
        End With
End Sub
 
Upvote 0
Your method of referring to your sheet with the values you need to change is messy. You should always strive to be explicit with your object references. Please try the following - just change the workbook/worksheet names to suit.
VBA Code:
Sub Fix_Format()
    Dim wb As Workbook, ws As Worksheet
    Set wb = Workbooks("Workbook_Name")         '<-- *** Change to actual workbook name ***
    Set ws = wb.Worksheets("Sheet1")            '<-- *** Change to actual worksheet name ***
        With ws.Range("G9:G136")
            .Value2 = Evaluate("Substitute(Substitute(" & .Address(, , , 1) & ",""°"",""""),""."","","")")
        End With
End Sub
Hi but I cant use exact name of workbook because it will change with using this macro so this is why I use wrokbook marking by number. My point is you open exel file named A take numbers in format using dot instead of commas convert them to numbers using comma and erease degree mark then I need to copy that data to another workbook then I will close exel file A and then open exel file B(this file has same content like A but different values) and I need to use same macro so It has to by name free makro.
 
Upvote 0
I think I understand you. OK, try this:
VBA Code:
Sub Fix_Format()
    Dim wb As Workbook, ws As Worksheet
    Set wb = Workbooks(2)
    Set ws = wb.ActiveSheet
        With ws.Range("G9:G136")
            .Value2 = Evaluate("Substitute(Substitute(" & .Address(, , , 1) & ",""°"",""""),""."","","")")
        End With
End Sub
 
Upvote 0
I think I understand you. OK, try this:
VBA Code:
Sub Fix_Format()
    Dim wb As Workbook, ws As Worksheet
    Set wb = Workbooks(2)
    Set ws = wb.ActiveSheet
        With ws.Range("G9:G136")
            .Value2 = Evaluate("Substitute(Substitute(" & .Address(, , , 1) & ",""°"",""""),""."","","")")
        End With
End Sub
Hi it works but not corectly It changes not decimal but thousands for me situation is following. I have decimal number 1.234° and the reasult have to be 1,234 from you code it changes to tousands 1 234.
 
Upvote 0
Can you share your file via Google Drive, Dropbox or similar file sharing platform? The format of your values is obviously different from what I've been testing with. When I run the code, it changes from this:
Book1
G
91.245°
101.245°
111.245°
121.245°
131.245°
141.245°
151.245°
161.245°
171.245°
181.245°
191.245°
201.245°
211.245°
221.245°
231.245°
241.245°
251.245°
261.245°
271.245°
281.245°
291.245°
301.245°
311.245°
321.245°
331.245°
341.245°
351.245°
361.245°
371.245°
381.245°
391.245°
401.245°
411.245°
421.245°
431.245°
441.245°
451.245°
461.245°
471.245°
481.245°
491.245°
501.245°
511.245°
521.245°
531.245°
541.245°
551.245°
561.245°
571.245°
581.245°
591.245°
601.245°
611.245°
621.245°
631.245°
641.245°
651.245°
661.245°
671.245°
681.245°
691.245°
701.245°
711.245°
721.245°
731.245°
741.245°
751.245°
761.245°
771.245°
781.245°
791.245°
801.245°
811.245°
821.245°
831.245°
841.245°
851.245°
861.245°
871.245°
881.245°
891.245°
901.245°
911.245°
921.245°
931.245°
941.245°
951.245°
961.245°
971.245°
981.245°
991.245°
1001.245°
1011.245°
1021.245°
1031.245°
1041.245°
1051.245°
1061.245°
1071.245°
1081.245°
1091.245°
1101.245°
1111.245°
1121.245°
1131.245°
1141.245°
1151.245°
1161.245°
1171.245°
1181.245°
1191.245°
1201.245°
1211.245°
1221.245°
1231.245°
1241.245°
1251.245°
1261.245°
1271.245°
1281.245°
1291.245°
1301.245°
1311.245°
1321.245°
1331.245°
1341.245°
1351.245°
1361.245°
Sheet1


To this:
Book1
G
91,245
101,245
111,245
121,245
131,245
141,245
151,245
161,245
171,245
181,245
191,245
201,245
211,245
221,245
231,245
241,245
251,245
261,245
271,245
281,245
291,245
301,245
311,245
321,245
331,245
341,245
351,245
361,245
371,245
381,245
391,245
401,245
411,245
421,245
431,245
441,245
451,245
461,245
471,245
481,245
491,245
501,245
511,245
521,245
531,245
541,245
551,245
561,245
571,245
581,245
591,245
601,245
611,245
621,245
631,245
641,245
651,245
661,245
671,245
681,245
691,245
701,245
711,245
721,245
731,245
741,245
751,245
761,245
771,245
781,245
791,245
801,245
811,245
821,245
831,245
841,245
851,245
861,245
871,245
881,245
891,245
901,245
911,245
921,245
931,245
941,245
951,245
961,245
971,245
981,245
991,245
1001,245
1011,245
1021,245
1031,245
1041,245
1051,245
1061,245
1071,245
1081,245
1091,245
1101,245
1111,245
1121,245
1131,245
1141,245
1151,245
1161,245
1171,245
1181,245
1191,245
1201,245
1211,245
1221,245
1231,245
1241,245
1251,245
1261,245
1271,245
1281,245
1291,245
1301,245
1311,245
1321,245
1331,245
1341,245
1351,245
1361,245
Sheet1
 
Upvote 0
Can you share your file via Google Drive, Dropbox or similar file sharing platform? The format of your values is obviously different from what I've been testing with. When I run the code, it changes from this:
Book1
G
91.245°
101.245°
111.245°
121.245°
131.245°
141.245°
151.245°
161.245°
171.245°
181.245°
191.245°
201.245°
211.245°
221.245°
231.245°
241.245°
251.245°
261.245°
271.245°
281.245°
291.245°
301.245°
311.245°
321.245°
331.245°
341.245°
351.245°
361.245°
371.245°
381.245°
391.245°
401.245°
411.245°
421.245°
431.245°
441.245°
451.245°
461.245°
471.245°
481.245°
491.245°
501.245°
511.245°
521.245°
531.245°
541.245°
551.245°
561.245°
571.245°
581.245°
591.245°
601.245°
611.245°
621.245°
631.245°
641.245°
651.245°
661.245°
671.245°
681.245°
691.245°
701.245°
711.245°
721.245°
731.245°
741.245°
751.245°
761.245°
771.245°
781.245°
791.245°
801.245°
811.245°
821.245°
831.245°
841.245°
851.245°
861.245°
871.245°
881.245°
891.245°
901.245°
911.245°
921.245°
931.245°
941.245°
951.245°
961.245°
971.245°
981.245°
991.245°
1001.245°
1011.245°
1021.245°
1031.245°
1041.245°
1051.245°
1061.245°
1071.245°
1081.245°
1091.245°
1101.245°
1111.245°
1121.245°
1131.245°
1141.245°
1151.245°
1161.245°
1171.245°
1181.245°
1191.245°
1201.245°
1211.245°
1221.245°
1231.245°
1241.245°
1251.245°
1261.245°
1271.245°
1281.245°
1291.245°
1301.245°
1311.245°
1321.245°
1331.245°
1341.245°
1351.245°
1361.245°
Sheet1


To this:
Book1
G
91,245
101,245
111,245
121,245
131,245
141,245
151,245
161,245
171,245
181,245
191,245
201,245
211,245
221,245
231,245
241,245
251,245
261,245
271,245
281,245
291,245
301,245
311,245
321,245
331,245
341,245
351,245
361,245
371,245
381,245
391,245
401,245
411,245
421,245
431,245
441,245
451,245
461,245
471,245
481,245
491,245
501,245
511,245
521,245
531,245
541,245
551,245
561,245
571,245
581,245
591,245
601,245
611,245
621,245
631,245
641,245
651,245
661,245
671,245
681,245
691,245
701,245
711,245
721,245
731,245
741,245
751,245
761,245
771,245
781,245
791,245
801,245
811,245
821,245
831,245
841,245
851,245
861,245
871,245
881,245
891,245
901,245
911,245
921,245
931,245
941,245
951,245
961,245
971,245
981,245
991,245
1001,245
1011,245
1021,245
1031,245
1041,245
1051,245
1061,245
1071,245
1081,245
1091,245
1101,245
1111,245
1121,245
1131,245
1141,245
1151,245
1161,245
1171,245
1181,245
1191,245
1201,245
1211,245
1221,245
1231,245
1241,245
1251,245
1261,245
1271,245
1281,245
1291,245
1301,245
1311,245
1321,245
1331,245
1341,245
1351,245
1361,245
Sheet1
Try this link there are 2 files Konverter is that where VBA code is writen and 1.kus cavita3 is that I will change format in it.

 
Upvote 0
OK. I'm not sure that I can be of any further assistance. The file you shared is a comma delimited csv with all the values in column A - I'm not sure what process you undertake to get those values into column G so as to match your original post requirements. In any event, I ran the code on column A to see whether it would remove the degree symbol & convert the periods to commas - and it seemed to. Therefore, I cannot duplicate the issue you're having with the code. Here's a link to the file you shared after I ran the code on column A.
1.kus_kavita.csv
 
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,636
Members
449,109
Latest member
Sebas8956

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