Consulting

Results 1 to 7 of 7

Thread: VBA Reverse A string

  1. #1

    VBA Reverse A string

    Hi guys,

    Im new to this forum as well as the VBA language, I was wondering by any chance if someone can help me with a problem.
    I need to figure out a way to reverse a string using the For and Next X format and/or If, Else and Endif.
    Any help would work, especially if you can point me in the right direction!

    Thanks in advance

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,722
    Location
    Why do you need those when VBA has a StrReverse function?
    Be as you wish to seem

  3. #3
    Its for an assignment

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's quite simple, get the length of the string variable, then loop from the last character to the first and put into a new variable. Assuming i is the loop control index, the position in the new variable would be i-Len(original_string)+1.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I think I would concatenate the new variable and the previous character of the old string
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Pseudo-code steps

    1. Create a string variable (Dim ...) to hold the reversed string for output

    2. Use a For / Next / Step loop to step DOWN by -1 from the LEN of the input string to 1 (the first char)

    3. For each iteration, add the i-th char from the MID-dle (that is a hint) of the input string to the end of the output string in 1)

    Online help is pretty good
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    Just as another thought process for this

    You could also look at the RIGHT character of the string then have a temp string that you trim the last character off each iteration and then look at the new RIGHT hand character each time adding to the string. not sure if there is a real benefit but just another approach to the same task

    Vox190 how did the assignment go?
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

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
  •