+ Reply to Thread
Results 1 to 8 of 8

A better Case (was "Create Loop with Case statement")

Hybrid View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    A better Case (was "Create Loop with Case statement")

    In the code below I am having trouble creating a loop to check the contents of TextBox1 as it can have upwards of 8 characters. This case statement works fine as long as the character count is one.
    How do I count how many characters are in Textbox1 and then loop thru them? Am I on the right thought track?
    'Bunch of other code here (Similar to below except using other fonts)
                sTbItem1 = TextBox1.Value
                Select Case sTbItem1
                    Case "."
                        sTbItem1 = Replace(sTbItem1, ".", "ù")
                    Case "1"
                        sTbItem1 = Replace(sTbItem1, "1", "ï")
                    Case "2"
                        sTbItem1 = Replace(sTbItem1, "2", "ð")
                    Case "3"
                        sTbItem1 = Replace(sTbItem1, "3", "ñ")
                    Case "4"
                        sTbItem1 = Replace(sTbItem1, "4", "ò")
                    Case "5"
                        sTbItem1 = Replace(sTbItem1, "5", "ó")
                    Case "6"
                        sTbItem1 = Replace(sTbItem1, "6", "ô")
                    Case "7"
                        sTbItem1 = Replace(sTbItem1, "7", "õ")
                    Case "8"
                        sTbItem1 = Replace(sTbItem1, "8", "ö")
                    Case "9"
                        sTbItem1 = Replace(sTbItem1, "9", "÷")
                    Case "0"
                        sTbItem1 = Replace(sTbItem1, "0", "î")
                End Select
            End If
        End If
        sTolerance = sTbItem1
        TextBox1.Text = sTbItem1
    End If
    Any hints, tips or examples are appreciated.
    Last edited by Rick_Stanich; 09-16-2009 at 12:55 PM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Create Loop with Case statement

    Not sure if you need such a loop if you're just replacing characters as you can do a partial match?

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Create Loop with Case statement

    I dont follow, Partial match?

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Create Loop with Case statement

    With Find and Replace you don't have to match the entire cell contents. Try it from the worksheet and you'll see.

  5. #5
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Create Loop with Case statement

    Ok, I understand that now.
    I will see how I can apply that in a TextBox.
    Hmm, does it matter if the all of this is in a userform?

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Create Loop with Case statement

    I would have thought it should still work, but what do I know. Report back and put us out of our misery.

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Create Loop with Case statement

    like this perhaps.

    Function ReplaceCharacters(Text As String, OldText As String, NewText As String) As String
    
        Dim lngIndex As Long
        
        If Len(OldText) = Len(NewText) Then
            For lngIndex = 1 To Len(OldText)
                Text = Replace(Text, Mid(OldText, lngIndex, 1), Mid(NewText, lngIndex, 1))
            Next
        End If
        ReplaceCharacters = Text
    
    End Function
    
    Private Sub CommandButton1_Click()
    
        TextBox1.Text = ReplaceCharacters(TextBox1.Text, ".1234567890", "ùïðñòóôõö÷î")
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  8. #8
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Create Loop with Case statement

    Oh sure, now see what you did!?
    Now I have to edit some umpteen pages and condense them to about 1 page.
    Thanks a lot!



    Thanks to all.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1