• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Populating Text Fields in Userform Based on First Row of Data within Worksheet Selected in Combobox

VBXL

Member
Hello there, I am trying to get my userform to function as described above.

PhwEWSy.png

ZhnyjMN.png


Can someone please help? This is what I have coded:

Code:
Dim ws As Worksheet

Private Sub cbContactType_Change()
    Me.cmdbNew.Enabled = CBool(Me.cbContactType.ListIndex <> -1)
    If Me.cbContactType.Enabled Then Set ws = Worksheets(cbContactType.Text)
    Me.txt7.Visible = Not IsError(Application.Match(cbContactType.Text, Array("Housing Associations", "Landlords"), False))
    Me.mstrAccounts.Visible = Me.txt7.Visible
    Me.MLA.Visible = Me.txt7.Visible
 
  Dim Tgt As Range
  With ws
  Set fCell = .Range("A:A").Find(cbContactType.Value, , xlValues, xlWhole)
  If fCell Is Nothing Then Exit Sub
    X = fCell.Row
    Me.txt1.Value = .Cells(X, 1).Value
    Me.txt2.Value = .Cells(X, 2).Value
    Me.txt3.Value = .Cells(X, 3).Value
    Me.txt5.Value = .Cells(X, 5).Value
    Me.txt6.Value = .Cells(X, 6).Value
    Me.txt4.Value = .Cells(X, 4).Value
    Me.txt7.Value = .Cells(X, 7).Value

End With
 
End Sub

Private Sub iptSearch_Click()
      Contacts.Hide
      Unload Contacts
End Sub

Private Sub mstrYes_Click()
For Each objCrl In Me.Controls
If mstrYes.Value Then txt7.Visible = True
Next
End Sub

Private Sub mstrNo_Click()
For Each objCrl In Me.Controls
If mstrNo.Value Then txt7.Visible = False
Next
mstrYes.Visible = True
mstrNo.Visible = True
End Sub

'Private Sub cmdbChange_SpinUp()
'    If Me.cbContactType.ListRows.Count < 1 Then Exit Sub
'    If CurrentRow > 1 Then
'        CurrentRow = CurrentRow - 1
'        UpdatecmdbChange
'    End If
'End Sub

'Private Sub cmdbChange_SpinDown()
'    If CurrentRow = Me.cbContactType.ListRows.Count Then Exit Sub
'    If CurrentRow < Me.cbContactType.ListRows.Count Then
'        CurrentRow = CurrentRow + 1
'        UpdatecmdbChange
'    End If
'End Sub

'Private Sub UpdatePositionCaption()
'    dtaRow.Caption = CurrentRow & " of " & Me.cbContactType.ListRows.Count
'End Sub

Private Sub UserForm_Initialize()
    Me.cbContactType.List = Array("Council Contacts", "Local Contacts", "Housing Associations", "Landlords", "Letting and Selling Agents", "Developers", "Employers")
    Me.cmdbNew.Enabled = False
    Me.txt7.Visible = False
    Me.mstrAccounts.Visible = False
    Me.MLA.Visible = False
    Dim objCtrl As Control
        mstrYes.Value = False
        mstrNo.Value = False
        For Each objCtrl In Me.Controls
        If Left(objCtrl.Name, 4) = “Text” Then txt7.Visible = False
        Next
            If Me.txt7.Value = "" Then
            Me.txt7.Value = "Example1: " & vbCrLf & "Example2: " & vbCrLf & "Example3: "
            End If
End Sub

Private Sub cmdbNew_Click()
Dim cNum As Integer, X As Integer
    Dim nextrow As Long
    nextrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    If Len(ws.Cells(1, 2).Value) > 0 Then nextrow = nextrow + 1
    cNum = 7
        Dim AlignLeft As Boolean
    For X = 1 To cNum
    AlingLeft = CBool(X = 1 Or X = 7)
    With ws.Cells(nextrow, X + 1)
        .Value = Me.Controls("txt" & X).Value
        .EntireColumn.AutoFit
        .HorizontalAlignment = IIf(X = 1 Or X = 7, xlLeft, xlCenter)
        .VerticalAlignment = xlCenter
        With .Font
            .Name = "Arial"
            .FontStyle = "Regular"
            .Size = 10
        End With
    End With
    Me.Controls("txt" & X).Text = ""
    Next
    MsgBox "Contact added to " & ws.Name, 64, "Contact Added"
    Application.ScreenUpdating = False
    Unload Me
    Contacts.Show
    Application.ScreenUpdating = True
End Sub

Private Sub cmdbClose_Click()
Unload Me
End Sub

The file can be found here: https://www.dropbox.com/s/gooebb7hcm07cqm/BKContacts.xlsm?dl=0

Thanks in advance.


▬▬▬▬▬▬▬▬▬ Mod edit : thread moved to appropriate forum !
 
Back
Top