Consulting

Results 1 to 14 of 14

Thread: How to replace a symbol like ≤ by using VBA?

  1. #1
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location

    How to replace a symbol like ≤ by using VBA?

    I tried to replace the symbol ≤ in a string. Without succes so far. Does someone know how to do this?

    Sub test()
    ActiveCell.Offset(0, 1) = Replace(ActiveCell.Value, "=", "@")
    End Sub

    Suppose the ActiveCell contains the string "100 ≤ t ≤ 150"

    Now i want to replace the ≤ characters in this string by the symbol @.
    How do i do that? The code above fails.

    The problem is that you can't type symbols like ≤ in a module.

    Stranno

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    In cell B1 of this example, I put the value of the symbol to replace in cell A1.
    Sub ken()
         Range("A1").Replace What:=Range("B1").Value2, Replacement:="@", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End Sub

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    "≤" = Chr(178)
    "≥" = Chr(179)
    "≠" = Chr(173)

    So you could use code like
    myString = Replace(myString, Chr(178), "@")

  4. #4
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Thanks a lot guys. At the moment i am not equipped with a computer but i assume this will work. I did try the ascii code but i obviously took the wrong number. I think VBA's answer to msgbox asc(≤) was 61 or something like that.

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I got those number from a worksheet using the CODE function.

  6. #6
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Right.

  7. #7
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Unfortunately my reaction was a bit premature. Both solutions don't work on my computer. If i run the following code:
    on error resume next
    For i = 1 to 1000
    cells(i,1) = chr(i)
    next

    i see no ≤, ≥ or ≠ character at all.

    On my computer chr(173), chr(178) and chr(179) represents other characters.

    Also Kenneth's solution did not work.

    keyboard settings?

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Have you tried
    ChrW(8804) "≤"
    ChrW(8805) "≥"
    ChrW(8800) "≠"

  9. #9
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Yes mikerickson i just came to the same solution.

    Sub n()
    ActiveSheet.Cells(1, 1) = ChrW(&H2264)
    MsgBox Replace(ActiveSheet.Cells(1, 1), ChrW(&H2264), "@")
    End Sub

    with hexadecimals

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    My solution does "work" or I would not have posted it.

    In any case, the ChrW() method appears to work fine too.

    Mike's other code probably works for Mac but not Windows.

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Or, it may be specific to the font Calibri, which is my default font.

  12. #12
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    My solution does "work" or I would not have posted it.

    In any case, the ChrW() method appears to work fine too.

    Mike's other code probably works for Mac but not Windows.
    Honestly Kenneth, your example does nothing. Cell A1 remains empty and teh value in Cell B1 doesn't change. Even if i put a 'normal' value in B1 (for example the letter H) nothing happens.

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Why would B1 change? That has the Replace character to find for replacement in my method. Of course one can use ChrW() method in my method as well. Replacing by B1 gives you flexibility to change any character(s).
    Attached Files Attached Files

  14. #14
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Kenneth, I am ashamed. I have totally misunderstood your answer. Thanks a lot for your efforts to convince me.
    stranno

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •