简体   繁体   中英

Excel VBA find and replace all characters before numbers

In Excel, this is an example of the cell values. "▲99315","▲65". I'm trying to use VBA to remove the arrows which are always up or down, for gain or loss.

I can't seem to make this work using VBA. When I open this workbook it is auto updated. I would like to run a VBA to "Find and Replace" the arrows. When I try to paste the arrow characters in "find" it shows as a "?".

If all of those numbers compulsorily contain the first character as one of up and down arrows, you may give this a try....

The following code assumes that your numbers are in column A and start from Row2. If not, change it as per your requirement.

Sub ReplaceArrows()
    Dim lr As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A2:A" & lr).Value = Evaluate("=IF(A2:A" & lr & "="""","""",RIGHT(A2:A" & lr & ",LEN(A2:A" & lr & ")-1)+0)")
End Sub

To use VBA Find, you need to determine the code for the character you want to replace. Assuming the arrows you are using are the same as you pasted into your question, the following code will work:

with myRange
    .Replace what:=ChrW(&H25B2), replacement:=""  'Up Arrow
    .Replace what:=ChrW(&H25BC), replacement:=""  'Down Arrow
end with

Note the use of ChrW for the double byte character, and the Hex Code representing the up and down arrows.

如果这些是条件格式图标,则需要使用rng.FormatConditions.Delete方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM