Results 1 to 10 of 27

Thread: "What’s in a String"- VBA break down Loop through character contents of a string

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,315
    Rep Power
    10

    "What’s in a String"- VBA break down Loop through character contents of a string

    "What's in a String"- VBA break down Loop through character contents of a string

    __Hello (or _ vbTab & "He" & "l" & "l" & "o" & vbCr & vbLf _ )
    The initial three posts, post #1 , post #2 and post # 3 , will take some time to read through as in these posts the technical background is explained in as much detail as possible. If you already are at a good competent level of VBA , and/or want to get simply to a way to "see" what is in a string of text" , you may wish to start at post #4

    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.

    So as example we will make a routine with first (signature) line of, say
    Sub LoopThroughString(ByVal MyString As String)
    So we have a routine like
    Code:
    Sub LoopThroughString(ByVal MyString As String)
     
     
     
    End Sub
    The first ( signature ) line means that that routine will work from within another routine as a sort of a method, which when you Call it in to use, will need to be given some string value at MyString. You are allowed to pass it a variable containing a string variable as well, if you prefer: The signature line specifies that it will take the Value of that. Within the sub routine, you refer to the passed value via MyString

    For the purposes of this demo we will first need to have a simple routine that Calls that main routine, Sub LoopThroughString( ByVal MyString [color=Blue]As String[/color] )
    Lets call that Calling sub routine, Sub MyTestString()
    It is that simple routine that we will run in our demos. You have to do it like this, because you cannot easily run a code such as Sub LoopThroughString( ByVal MyString [color=Blue]As String[/color] ) directly. VBA syntax simply does not allow you to do that easily. The simplest way to get it to run is to Call it from a simple routine which must at the Call line pass the string that I want to look at.
    Either of the 8 Calling lines in the next routine are syntaxly satisfactory . So running the routine Sub MyTestString() will result in the routine Sub LoopThroughString( ByVal MyString [color=Blue]As String[/color] ) running 8 times: You will get the pop up message box 8 times :
    StringInfoMsgBox.JPG : https://imgur.com/cWG7z5s
    WotChaGotSimpleMsgBox.jpg
    Code:
    Sub MyTestString()
     Call LoopThroughString(MyString:="Hello")
     LoopThroughString MyString:="Hello"
     Call LoopThroughString("Hello")
     LoopThroughString "Hello"
    ' 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 LoopThroughString(MyString:=varForMyString)
     LoopThroughString MyString:=varForMyString
     Call LoopThroughString(varForMyString)
     LoopThroughString varForMyString
    End Sub
    Sub LoopThroughString(ByVal MyString As String)
     MsgBox prompt:="You did pass" & vbCr & vbLf & "  the following string: " & vbCr & vbLf & vbTab & """" & MyString & """", Buttons:=vbInformation, Title:="Info about the string you gave me"
    End Sub
    I personally prefer the syntax form which helps remind me what is going on, and so I would reduce the demo coding to Call the main routine, Sub LoopThroughString( ByVal MyString [color=Blue]As String[/color] ) , just once and supply it the string under investigation within a variable:
    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 LoopThroughString(MyString:=varForMyString)
    End Sub
     
    Sub LoopThroughString(ByVal MyString As String)
     MsgBox prompt:="You did pass" & vbCr & vbLf & "  the following string: " & vbCr & vbLf & vbTab & """" & MyString & """", Buttons:=vbInformation, Title:="Info about the string you gave me"
    End Sub
    In that coding the various & vbCr & vbLf & vbTab & """" stuff is just to pretty up the format a bit and to make us aware of some of the most common hidden characters.
    This shortened version might be more familiar to a complete beginner:
    Code:
    Sub MyTestString()
    Dim varForMyString As String
     Let varForMyString = "Hello"
     LoopThroughString varForMyString
    End Sub
     
    Sub LoopThroughString(ByVal MyString As String)
     MsgBox MyString
    End Sub
    vbTab vbCr vbLf """"
    I have not mentioned it yet, it may have been obvious, but just in case not.. The first three things there are the most common used "hidden characters" and so are really worth with getting familiar with if you are interested in looking at contents of a string. Also the way we handle quotes in a string is very awkward leading often to problems, so it is really worth getting a feel for that at an early stage.
    vbCr vbLf
    These come about the early days of computing. Back then strings and other things in strings passing around computers and the early days of the internet tended to find there way fed into a mechanical printer of mechanical typewriter which had paper fed into it.
    Cr means something along the lines of carriage return which in turn means go back to the start. Usually this start means the left side of a piece of paper . You would need to do that if you are typing out along a piece of paper as eventually you would get to the other side of the paper. Almost always when you did a Cr you would need to move the piece of paper by a bit more than the height of a line so that the next printing did not go on top of thee last line printed. Typically the word "LlineFeed" was used for this process of shifting the paper, hence the Lf abbreviation
    So those "hidden characters" would have been recognised by an old printer as telling it to move to a new line and go back to the start side of the paper before printing further. As coding and screens and word processing developed, those two hidden characters were the natural things to keep using to indicate a new line on what ever media we "look at" computer stuff. There are two characters there. Often in coding you can use something like vbCrLf instead. But that is still "seen" as 2 characters by most computer things: it will almost always be measured to have a Length of 2. Some computer systems will recognise it as vbCrLf. Others will "see" it as vbCr & vbLf
    vbTab
    This can be a bit inconsistent. Or rather, the results it gives can be very dependant on various computer settings, so that can make it tricky to use effectively. The simplest explanation is a space. More specifically it can be use to define a specific place where something may begin. In some situations an argument version is available vbTab( ) to define specifically "where something may be". Exactly how it works can be a bit variable.

    Important uses of vbTab vbCr vbLf
    In computing generally the use of vbCr & vbLf will signalise to a display window or text file or similar to separate a string into lines. For modern uses there often is not the requirement to have the two and it would appear that in most cases either of these 3 will result in a new line being displayed.
    vbCr & vbLf
    vbCr
    vbLf

    In some situations Excel will use vbCr & vbLf to separate rows. It appears that within a cell it usually just uses vbLf
    In some situations Excel will use the vbTab to separate the text in a line of cells: In other words it can be thought of as replacing the vertical "wall" between cells
    These uses of vbTab vbCr vbLf allow for some interesting alternative ways to manipulate ranges.
    ref to be added later shfkjashfkjhhkhkjfhkjashfkjhkjhkjsahhfkjashfkjh
    Last edited by DocAElstein; 02-27-2019 at 06:40 PM.

Similar Threads

  1. VBA Versions of my "Get Field" and "Get Reverse Field" formulas
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 4
    Last Post: 06-02-2017, 06:15 PM
  2. Get "Reversed" Field from Delimited Text String
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 3
    Last Post: 02-22-2015, 09:01 AM
  3. Replies: 1
    Last Post: 02-10-2015, 09:41 AM
  4. Replies: 4
    Last Post: 09-09-2013, 05:13 PM
  5. Ordinal Suffix (i.e., "st", "nd", "rd" and "th")
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 0
    Last Post: 03-20-2012, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •