Code: Select all
Private Sub UserForm_Initialize()
cboSearchby.List = Array("Serial", "Customer ID,"Bill Number")
End Sub
how may this be done in excel userform?
Code: Select all
Private Sub UserForm_Initialize()
cboSearchby.List = Array("Serial", "Customer ID,"Bill Number")
End Sub
Code: Select all
Private Sub TextBox1_Change()
'the change event runs each time the user
'types into a text box
Dim s As String
Dim i As Integer
Dim c As Integer
s = TextBox1.Text
'Note the use of the ListIndex property of the ListBox
'If the ListIndex is -1 means nothing selected
'If 0 means the first item selected
ListBox1.ListIndex = -1
If TextBox1.Text = "" Then 'nothing typed
Exit Sub
End If
Select Case Me.cboSearchBy
Case ""
' Nothing selected
Exit Sub
Case "Serial"
c = 0
Case "Customer ID"
c = 1
Case "Bill Number"
c = 3
End Select
For i = 0 To ListBox1.ListCount - 1
'use the LIKE operator to compare
'convert both to Uppercase as well so case does not matter
If UCase(ListBox1.List(i, c)) Like UCase(s & "*") Then
ListBox1.ListIndex = i
Exit Sub
End If
Next i
End Sub
Code: Select all
Private Sub cboSearchBy_Change()
Me.TextBox1 = ""
End Sub
Code: Select all
=OFFSETData!,1,MATCH("CustomerIDData!,0)-1,COUNTA(Data!)-1,1)
Code: Select all
=OFFSET(Data!,1,MATCH("CustomerID",Data!,0)-1,COUNTA(Data!)-1,1)
Code: Select all
=OFFSET(Data!$A$1,1,MATCH("Customer ID",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Name",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Address",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Age",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Sex",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Date of Birth",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
=OFFSET(Data!$A$1,1,MATCH("Comments",Data!$1:$1,0)-1,COUNTA(Data!$A:$A)-1,1)
Code: Select all
Private Sub UpdateData()
'get fresh values for all textboxes from the spreadsheet
txtCustomerID.Text = Range("CustomerID")(txtRow.Text).Value
txtName.Text = Range("Name")(txtRow.Text).Value
txtAddress.Text = Range("Address")(txtRow.Text).Value
txtDateofBirth.Text = Range("DateofBirth")(txtRow.Text).Value
txtAge.Text = Range("Age")(txtRow.Text).Value
cboSex.List = Range("Sex")(txtRow.Text).Value
cboComments.List = Range("Comments")(txtRow.Text).Value
End Sub
Code: Select all
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm("EditCustomerRecord").Me("txtCustomerID") = Me.ListBox1
UserForm("Name").Me("txtAddress") = Me.ListBox1
End Sub
Code: Select all
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.txtCustomerID = Me.ListBox1.Column(0)
Me.txtName = Me.ListBox1.Column(1)
...
End Sub
Code: Select all
Option Explicit
Private Sub cboSearchBy_Change()
Me.txtEnterParameter = ""
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
End Sub
Private Sub txtEnterParameter_Change()
'the change event runs each time the user
'types into a text box
Dim s As String
Dim i As Integer
Dim c As Integer
s = txtEnterParameter.Text
'Note the use of the ListIndex property of the ListBox
'If the ListIndex is -1 means nothing selected
'If 0 means the first item selected
ListBox1.ListIndex = -1
If txtEnterParameter.Text = "" Then 'nothing typed
Exit Sub
End If
Select Case Me.CboSearchBy
Case ""
' Nothing selected
Exit Sub
Case "Customer ID"
c = 0
Case "Name"
c = 1
Case "Address"
c = 2
End Select
For i = 0 To ListBox1.ListCount - 1
'use the LIKE operator to compare
'convert both to Uppercase as well so case does not matter
If UCase(ListBox1.List(i, c)) Like UCase(s & "*") Then
ListBox1.ListIndex = i
Exit Sub
End If
Next i
End Sub
Private Sub UserForm_Initialize()
Me.txtRow = 1
UpdateData
CboSearchBy.List = Array("Customer ID", "Name", "Address")
cboSex.List = Array("Male", "Female")
cboComments.List = Array("New", "Old")
End Sub
Private Sub txtDateofBirth_AfterUpdate()
Dim d1 As Date
Dim d2 As Date
Dim Age As Integer
If Not IsDate(Me.txtDateofBirth.Value) Then
MsgBox "The Date box must contain a date.", vbExclamation, "Add New Customer"
Me.txtAge = ""
Else
d1 = CDate(Me.txtDateofBirth)
d2 = Date
Age = Year(d2) - Year(d1)
If Month(d2) < Month(d1) Or (Month(d2) = Month(d1) And Day(d2) < Day(d1)) Then
Age = Age - 1
End If
Me.txtAge = Age
End If
'store the value of the textbox in the spreadsheet
Range("DateofBirth")(txtRow.Text).Value = txtDateofBirth.Value
End Sub
Private Sub txtCustomerID_AfterUpdate()
'store the value of the textbox in the spreadsheet
Range("CustomerID")(txtRow.Text).Value = txtCustomerID.Value
End Sub
Private Sub txtName_AfterUpdate()
'store the value of the textbox in the spreadsheet
Range("Name")(txtRow.Text).Value = txtName.Value
End Sub
Private Sub txtAddress_AfterUpdate()
'store the value of the textbox in the spreadsheet
Range("Address")(txtRow.Text).Value = txtAddress.Value
End Sub
Private Sub txtAge_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'store the value of the textbox in the spreadsheet
If Not IsNumeric(txtAge.Text) Then
'only allow numeric values
Cancel = True
Else
Range("Age")(txtRow.Text).Value = txtAge.Value
End If
End Sub
Private Sub cboSex_AfterUpdate()
'store the value of the textbox in the spreadsheet
Range("Sex")(txtRow.Text).Value = cboSex.Value
End Sub
Private Sub cboComments_AfterUpdate()
'store the value of the textbox in the spreadsheet
Range("Comments")(txtRow.Text).Value = cboComments.Value
End Sub
Private Sub txtRow_AfterUpdate()
'go to the right row
If txtRow.Text < 1 Then
'can't go above the top of the range
txtRow.Text = 1
ElseIf txtRow.Text > Range("Data").Rows.Count Then
'can't go more than 1 row below the end of the range
txtRow.Text = Range("Data").Rows.Count
End If
UpdateData
txtCustomerID.SetFocus
End Sub
Private Sub UpdateData()
'get fresh values for all textboxes from the spreadsheet
txtCustomerID.Text = Range("CustomerID")(txtRow.Text).Value
txtName.Text = Range("Name")(txtRow.Text).Value
txtAddress.Text = Range("Address")(txtRow.Text).Value
txtDateofBirth.Text = Range("DateofBirth")(txtRow.Text).Value
txtAge.Text = Range("Age")(txtRow.Text).Value
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.txtCustomerID = Me.ListBox1.Column(0)
Me.txtName = Me.ListBox1.Column(1)
Me.txtAddress = Me.ListBox1.Column(2)
Me.txtDateofBirth = Me.ListBox1.Column(3)
Me.txtAge = Me.ListBox1.Column(4)
Me.cboSex = Me.ListBox1.Column(5)
Me.cboComments = Me.ListBox1.Column(6)
End Sub
Ok ill keep this part of the code as it is.First you wanted the text boxes to be filled, now you don't want them to be filled when the form opens, and you want the form to do something completely different than what it was designed for. Can you explain why you've changed your mind so radically?