0

Here is the prompted Question:

Write a sub that does the following: 1) it tasks the user to input a word; 2) it enters each character of the word in backwards order in consecutive cells in row 5, starting with cell "B5". hint: use a for loop.

I cant really figure out how to start, here is what I have but i know it is completely wrong:

Public Sub Question2()

Dim Word As String
Dim Counter As Integer

Word = InputBox("Please Enter a word")

For Counter = 1 To Len(Word)
    Range("B5").EntireRow.Value = StrReverse(Word)
Next

End Sub
0

2 Answers 2

1

Perhaps something like the following, using Step -1 to loop from the last character to the first, Offset to increment the column with each iteration of the loop, and Mid$ to return the characters one at a time:

Public Sub Question2()

    Dim Word As String
    Dim Counter As Integer

    Word = InputBox("Please Enter a word")

    For Counter = Len(Word) To 1 Step -1
        Range("B5").Offset(, Len(Word) - Counter).Value = Mid$(Word, Counter, 1)
    Next

End Sub

Of if you want to use StrReverse:

Public Sub Question2()

    Dim Word As String
    Dim Counter As Integer

    Word = InputBox("Please Enter a word")
    Word = StrReverse(Word)

    For Counter = 1 To Len(Word)
        Range("B5").Offset(, Counter - 1).Value = Mid$(Word, Counter, 1)
    Next

End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

If you can be sure it is ANSI characters only (Most likely, fingers crossed) you can use StrConv to convert the string to UNICODE and then split into an array using the UNICODE terminator (Chr(0)) and then write the array directly to the cells...

Public Sub Question2()

   Dim vArray As Variant
   vArray = Split(StrConv(StrReverse(InputBox("Please Enter a word")), 64), Chr(0))
   Range("B5").Resize(1, UBound(vArray)) = vArray

End Sub

1 Comment

Oops - didn't notice the 'Use a For Loop' comment... Oh well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.