Consulting

Results 1 to 5 of 5

Thread: Find and replace function (not Sub) with VBA

  1. #1

    Find and replace function (not Sub) with VBA

    Dear All,

    I am a newbie at VBA and I would like to create a function (not a Sub) to be used at any range and for any given value in the workbook. The Function should be used for replacing characters in the values. So far I have tried 2 codings:

    1st:
    Function Chatrim(incorref As Double) As Double
        Chatrim = replace(incorref, "2022/", "") incorref is  the chosen value and the expected result would be like : incorref = 2022/445 expected result with the function is  445 
    End Function
    2nd:

    Function Replacechar(incorvalue As Double) As Double
    Range(incorvalue).replace What:="2022/", Replacement:="" 
    'incorvalue is  the chosen value and the expected result would be like : incorvalue = 2022/445 expected result with the function is  445 
    End Function
    For both above functions I get the result: #VALUE!

    Thank you for your help!
    Last edited by Aussiebear; 03-27-2023 at 12:11 PM. Reason: Added code tags to submitted code

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Welcome to the VBAX forum Horesz89.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Review https://www.automateexcel.com/vba/fi...xt%20String%20


    BTW, you posted in forum for discussion about forum. Should post in appropriate topic forum.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Your functions are expecting to be passed a value of type Double yet you're processing it as a string.
    Change the first line to:
    Function Chatrim(incorref) As Double
    and it will work well if...
    you supply a string that after processing (replacement) can be coerced to a value of type Double, eg. 2022/456. The 2022/ is removed leaving 456 which can be coerced to a double, so the result will be a number 456.
    However if you pass the likes of 2022/ert, ert cannot be coerced to a number so you'll again get the #VALUE! error. If you want to return ert in such a case you need to change the first line to:
    Function Chatrim(incorref)
    Last edited by Aussiebear; 04-16-2023 at 04:23 PM. Reason: Added code tags
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Not sure what you're looking to do, but try this

    Capture.JPG


    Option Explicit
    
    
    Function Chatrim(incorref As Variant) As Variant
        Chatrim = Replace(incorref, "2022/", vbNullString)
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •