30.4k views
3 votes
Write a sub that does the following:

a. it asks the user to input a word.
b. it enters each character of the word in order in consecutive cells in row 2, starting with cell C2.

User Afraz
by
4.6k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

Using an Excel VBA string function to write the sub

Initially:

To loop from the last character back unto the first, we have to Offset in order to increase the column with each iteration of the for loop, as well as Mid$ just to return the characters one at a time.

Public Sub Question() Dim Word As String Dim Cnt As Integer Word = InputBox("Please Enter a word") For Cnt = Len(Word) To 1 Step -1 Range("C2").Offset(, Len(Word) - Cnt).Value = Mid$(Word, Cnt, 1) Next End Sub

Similarly, suppose we want to use StrReverse: Then; we can write it as;

Public Sub Question2() Dim Word As String Dim Cnt As Integer Word = InputBox("Please Enter a word") Word = StrReverse(Word) For Cnt = 1 To Len(Word) Range("C2").Offset(, Cnt - 1).Value = Mid$(Word, Cnt, 1) Next End Sub

User Sudcha
by
4.0k points