MACRO LIBRARY

Lesson 1.9 start code

Free public access. No account or subscription required.

Files

Lesson 1.9 start code

Code

User Form: frmNames

Sub main()
    frmNames.Show
End Sub

Module: debugging1

Dim strName As String
Dim strFirstName As String
Dim strLastName As String
Dim intSpaceLocation As Integer
Dim intLength As Integer
Dim i As Integer
    
Private Sub cmdFormat_Click()
    i = 0
    While i < lstName.ListCount
        strName = lstName.List(i)
        Debug.Print strName
        If InStr(1, strName, ",") = 0 Then
            Call FirstNameFirst
        Else
            Call LastNameFirst
        End If
        i = i + 1
    Wend
End Sub
    
Sub FirstNameFirst()
    intSpaceLocation = InStr(1, strName, " ")
    intLength = Len(strName)
    strFirstName = Left(strName, intSpaceLocation - 1)   'should be - 1
    strLastName = Mid(strName, intSpaceLocation + 1, intLength)
    strName = strLastName & ", " & strFirstName
    lstName.List(i) = strName
End Sub
    
Sub LastNameFirst()
    intSpaceLocation = InStr(1, strName, " ")
    intLength = Len(strName)
    strLastName = Left(strName, intSpaceLocation - 2)  'should be - 2
    strFirstName = Mid(strName, intSpaceLocation + 1, intLength)
    strName = strFirstName & " " & strLastName
    lstName.List(i) = strName
End Sub
    
Private Sub cmdAdd_Click()
    If txtName.Text <> "" Then lstName.AddItem txtName.Text
End Sub
    
Private Sub UserForm_Initialize()
    lstName.AddItem "Gargarin, Yuri"
    lstName.AddItem "Armstrong, Neil"
    lstName.AddItem "Aldrin, Buzz"
    lstName.AddItem "John Glenn"
End Sub