"What’s in a String"- VBA break down Loop through character contents of a string
Coding to Loop through character contents of a string
Recap :
In VBA coding and probably a lot of things to do with computers what is “actually” there is a long string of “characters” . These “characters” can be what we may recognise as every day characters, like H e l l o , as well as other things which technically still go be the name of characters. Some times these other characters may be referred to as hidden characters. In this usage of the word, hidden is not really an official term, but more of an everyday term used to mean some characters in the string that in many systems which we use to “view” strings, those characters are not obvious to see to us Humans
Check what ya got in ya string
I have found it can be interesting, informing and occasionally essential, to know what I have in a string. This can be done very easily in VBA with a simple loop. In the simplest form you need to use just two simple VBA functions , one, Len , to initially get the character length so that you know how many times to loop. In the Loop you use a second function, Mid , to get at each character as you loop.
In most practical situations you will have to get the string that you want to actually look at by some means that may not be straight forward. Exactly how you do that may vary from time to time, so it is usually convenient to write a routine which will work on some string which you present it. That routine will be a Sub routine which is written to take in a string, or string variable with a string in it.
In post #1 ( http://www.excelfox.com/forum/showth...ts-of-a-string ) , the merits of different basic procedure formats were discussed.
In this post we will start from the coding below , with the aim to develop the second procedure to give us a clear indication of exactly what is in the string under investigation, MyString
The coding , on running procedure, Sub MyTestString() currently simply gives a message.Code:Sub MyTestString() ' In the practice we would likely have our string obtained from some method and would have it held in some string variable Dim varForMyString As String Let varForMyString = "Hello" Call WtchaGot(strIn:=varForMyString) End Sub Sub WtchaGot(ByVal strIn As String) MsgBox prompt:="You did pass" & vbCr & vbLf & " the following string: " & vbCr & vbLf & vbTab & """" & strIn & """", Buttons:=vbInformation, Title:="Info about the string you gave me" End Sub
WotChaGotSimpleMsgBox.JPG : https://imgur.com/eYXDqSB
String output of characters and character listing .
Output: Long VBA code string representation
The full un simplified long string , as discussed in the example at the start of this Thread, in the syntax that would be required to add the string via coding is both a nice visual representation when used for all characters in a string, as well as being convenient to then use in coding for further investigations. So one of the two main outputs of the full routine, Sub WtchaGot(ByVal strIn As String) , will be a form which will show characters
_ that can be “seen” in their typical form,
and
_ “hidden” characters will be shown in either a form which can be used in VBA coding, which if does not have a specific VBA syntax constant will resort to the official listed ASCII ( http://www.asciitable.com/ ) number form: For example, the carriage return has the official number of 13, which can be used in VBA coding as Chr(13) , but as this character also has a VBA constant form , vbCr , this will be used in our string output.
As example, say at the start of the following text , some “hidden” character was present
Hi
__”u”.
This would be an example of the testing procedure used to test our main routine:
Our long VBA syntaxly acceptable string, which our routine should give us would be of this formCode:Sub TestWtchaGot() ' In the practice we would likely have our string obtained from some method and would have it held in some string variable Dim strTest As String Let strTest = Chr(1) & "Hi" & vbCrLf & vbTab & """u.""" Call WtchaGot(strIn:=strTest) ' Call WtchaGot(Chr(1) & "Hi" & vbCrLf & vbTab & """u.""") End Sub
__ Chr(1) & "H" & "i" & vbCr & vbLf & vbTab & """" & "u" & "." & """"
This will be output in a message box and also in the Immediate window, ( from the immediate window we could obtain a copy in which to paste into the VBA code pane window as part of a routine )
Output: Character listing
A second output will be given which will be a simple 2 column list. One column will be the “see able” version of the character, if excel manages to do that, and the other column will be its ASCII character.
This is intended to act as a notepad type thing , and if columns are already filled, then the latest will be added to the right of any existing ones. A date is given as well as the string length, and the first part of the string for ease of identification. It would be intended that the user manually deletes columns from time to time is they are no linger needed. This would be the results after two consecutive runs for the above example
Row\Col A B C D E 107 Feb 2019 Hi
"u."07 Feb 2019 Hi
"u." 21 1 3H 72 H 72 4i 105 i 105 513 13 610 10 79 9 8" 34 " 34 9u 117 u 117 10. 46 . 46 11" 34 " 34 12 13
A convenient customization could be to add a list of all ASCII characters in the first few columns. The routine would autoamticaaly ignores thes as it will paste its results in the next free column
Code Example
Here is a code example: http://www.excelfox.com/forum/showth...0938#post10938
http://www.excelfox.com/forum/showth...0939#post10939
. A brief description:
Rem 1 Worksheets info
‘1a) The routine is written to work in Excel mainly because of the convenience of the output possibilities. A worksheet is made "WotchaGotInString" if it does not already exist. Its existence is checked by If it is Not possible to refer to an arbitrary range in the worksheet "WotchaGotInString"
An array is made
‘1b) An array is made for the 2 column list and the first “row filled with some information : Date , Length of string and fist part of string
Rem 2)
This is the main Loop is done for each character in the string, ( the length has already been determined as it was needed for “row” dimension the 2 “column” array ). At the start of each loop, the character at that position is determined by
__ Mid(strIn, Cnt, 1) ' the character in strIn at position from the left of length 1
‘2a) Long VBA code string representation
This section tries to build a string in a format both pictorially convenient and syntaxly acceptable to VBA. The string will be built up in a string variable , WotchaGot
__ Chr(1) & "H" & "i" & vbCr & vbLf & vbTab & """" & "u" & "." & """"
All the single characters are shown separate by " & "
There are then three main processes in this section , ‘2a) , to determine the type of character.
‘2a)(i). This checks to see if the current character is one of the simple ones, like "A - Z" or like "0 - 9" or like "a - z"
__1If this is the case Then then we simply add that to the string variable housing the characters so far, WotchaGot. 2a) is then Ended
__Else two other main sections , ‘2a)(ii) and ‘2a)(iii) will be gone through sequentially if the character was not a simple one. All the remaining section uses a Select Case to look for some Case or other , after which if it is found , an appropriate addition is made to WotchaGot.
' 2a)(ii)_1 are for cases where I may wish to see a “see able” character in its normal see able form, like these ! ” § $ % & / ; : . etc….
' 2a)(ii)_2 is for hidden characters which VBA has a constant for , like these
vbCr , vbLf , vTab , etc..
At a final ' 2a)(iii) we have a Case Else which hopefully will catch anything we have not listed. This will then be added to WotchaGot in the form of its ASCII number , which we show as like Chr(3)
At this stage we are finished with section 2a) for any particular loop
‘2b) Fill the two “columns” in output Array for this loop.
The first “column” is filled with the loop number ( which corresponds to the character number counting from the left in the original string) and the actual character as a see able or hidden character. The second “column” is given its ASCII number which we can determine in VBA via the Asc-Function thus:
__ Asc(Caracter)
where in our case Caracter is the string variable of length 1 which we use in each loop to hold the next single character given by
__ Caracter = Mid(strIn, Cnt, 1)
At this point the coding has reached the last point of the Loop and the Loop restarts
Note: we have some lines commented out of this form
' ___ Case " "
' ____ Let WotchaGot = WotchaGot & """" & " " & """" & " & "
This is for convenience for later addition of any other specific characters that should not be left for section ' 2a)(iii) Case Else to catch
Just after the end of the Loop section is a code line to take off a last 3 characters ___ & __ ( 2 spaces each side of a & )
Rem 3Output
'3a) The long string, WotchaGot , is displayed both in a message box and in the Immediate window. Note that for long strings, that viewable length of a string is limited both in the message box and to lesser extent in the Immediate window.
'3b) Our output array, arrWotchaGot() , is a 2 dimension array of two columns, so we paste this out starting top left of the next free column in row 1 over a range resized to the size of the array.
_.___________________________________
Refs
https://www.automateexcel.com/vba/loop-through-string/
Codes from Lisa Green : http://www.eileenslounge.com/viewtop...243670#p243668




Reply With Quote
Bookmarks