replace Chr(45) in VBA

asdafgafawaaa

New Member
Joined
Jun 23, 2017
Messages
19
Hi,

One small section of VBA code doesn't work for the "−", this is not a regular dash.

I use the ASC function to get the ASCII code which is chr(45), then use the replace function to replace with "" so as to remove it. But it doesn't work at all, can anyone help me with this, much appreciate it!

For example for number 9999999−6 at A2, I want to put the new value at B2.

Sub test()

Range("B2")=replace(range("A2"),CHR(45),"")

End Sub

Thank you very much!

Jack
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thank you for the quick reply, the number is 45.

I can replace the is "Mid(Range("A2"), 8, 1)" with "", but when use chr(45), still not working! It's quite painful, haha~~~

Do you have another solution?

thank you again!
 
Upvote 0
when i use below code to key in the value in Excel, it gave me a dash, the shorter one. Is this a bug????


Range("I3") = Chr(45)
 
Upvote 0
"-" = Chr(45)
"–" = Chr(150)
"—" = Chr(151)

Second and third are En-Dash and Em-Dash characters; perhaps you need those?

WBD
 
Upvote 0
One small section of VBA code doesn't work for the "−", this is not a regular dash.

Range("B2")=replace(range("A2"),CHR(45),"")
I get the code for that character to be 8722 which means you will have to use the ChrW function for it, not the Chr one.

Range("B2") = Replace(Range("A2"), ChrW(8722), "")
 
Upvote 0
beaten 2it
 
Last edited:
Upvote 0
When I run the code and paste YOUR text into cell B2 I get the same behaviour as you and the code reports the dash as ASCII 45. BUT when I manually edit the cell and remove and replace the dash with the one on my keyboard the code correctly removes it.

You presumably have some kind of non standard character in there
 
Upvote 0

Forum statistics

Threads
1,216,524
Messages
6,131,176
Members
449,629
Latest member
Mjereza

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