PDA

View Full Version : VBA Reverse A string



vox190
01-14-2016, 06:39 AM
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

Aflatoon
01-14-2016, 06:55 AM
Why do you need those when VBA has a StrReverse function?

vox190
01-14-2016, 06:57 AM
Its for an assignment

Bob Phillips
01-14-2016, 08:42 AM
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.

SamT
01-14-2016, 12:24 PM
I think I would concatenate the new variable and the previous character of the old string

Paul_Hossler
01-14-2016, 08:11 PM
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

Movian
02-01-2016, 11:14 AM
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?