thanks Dave for your reply.I am still facing problem in it.i added the following code to my userform.I want to populate the combobox value when the userform initializes but when i am doing that it is not populating "Class 0 to Class 4" values.
Also i put the function get row and cbClassLevelChange in the module.What changes should i do?
Dim id As Integer, i As Integer, j As Integer, flag As Boolean
Dim ws As Worksheet
Dim RecordRow As Long
Dim Action As String
Private Sub btnOk_Click()
Dim msg As String
'add new or update existing record
ws.Cells(RecordRow, 1).value = Me.cbClassLevel.value
ws.Cells(RecordRow, 2).value = Me.TextBox1.value
Select Case Action
Case "New"
'update combobox list
Me.cbClassLevel.List = ws.Range("A2:A" & RecordRow).value
msg = "New Record Entered"
Case "Update"
msg = "Record Updated"
End Select
'report result
' MsgBox Me.cbClassLevel.Text & Chr(10) & msg, 48, msg
Me.cbClassLevel.Text = ""
Me.TextBox1.Text = ""
End Sub
Private Sub CommandButton1_Click()
cbClassLevel.Text = ""
TextBox1.Text = ""
End Sub
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
OnlyNumbers
End Sub
Private Sub OnlyNumbers()
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.value) And .value <> vbNullString Then
MsgBox "Sorry, only numbers allowed"
.value = vbNullString
End If
End With
End If
End Sub
Private Sub UserForm_Initialize()
'intialize ws object variable
Set ws = Worksheets("Forecast")
'populate combobox list
With Me.cbClassLevel
' .AddItem "Class 0"
'.AddItem "Class 1"
'.AddItem "Class 2"
'.AddItem "Class 3"
'.AddItem "Class 4"
.RowSource = ""
.List = ws.Range("A2:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row).value
'show first record
.ListIndex = 0
End With
End Sub
Hi welcome to the board.
See if this code does what you want.
Place all code in your forms code page:
Code:
Dim ws As Worksheet
Dim RecordRow As Long
Dim Action As String
Private Sub btnOk_Click()
Dim msg As String
'add new or update existing record
ws.Cells(RecordRow, 1).Value = Me.cbClassLevel.Value
ws.Cells(RecordRow, 2).Value = Me.TextBox1.Value
Select Case Action
Case "New"
'update combobox list
Me.cbClassLevel.List = ws.Range("A2:A" & RecordRow).Value
msg = "New Record Entered"
Case "Update"
msg = "Record Updated"
End Select
'report result
MsgBox Me.cbClassLevel.Text & Chr(10) & msg, 48, msg
Me.cbClassLevel.Text = ""
Me.TextBox1.Text = ""
End Sub
Private Sub cbClassLevel_Change()
If Me.cbClassLevel = -1 Then Exit Sub
'find record row in range - if recod does not exist
'next blank row in range is returned
RecordRow = GetRow(Target:=ws.Columns(1), Search:=Me.cbClassLevel.Text)
'return value to textbox
Me.TextBox1.Value = ws.Cells(RecordRow, 2).Value
End Sub
Private Sub UserForm_Initialize()
'intialize ws object variable
Set ws = Worksheets("Forecast")
'populate combobox list
With Me.cbClassLevel
.RowSource = ""
.List = ws.Range("A2:A" & ws.Cells(ws.Rows.Count, 1).End(xlUp).Row).Value
'show first record
.ListIndex = 0
End With
End Sub
Function GetRow(ByVal Target As Range, ByVal Search As String) As Long
Dim FoundCell As Range
'find the next blank row in range
GetRow = Target.Parent.Cells(Target.Parent.Rows.Count, Target.Column).End(xlUp).Row + 1
Action = "New"
'check if record exists
Set FoundCell = Target.Find(Search, lookat:=xlWhole, LookIn:=xlValues)
'if exists - return record row
If Not FoundCell Is Nothing Then GetRow = FoundCell.Row: Action = "Update"
End Function
Note the variables that sit outside any procedure. these must be placed at very top of your forms code page.
Code is not fully tested but when form opened, it should load all values in your target range (Column 1) in your combobox & populate the textbox with correct record. This should happen either if you select a record from list or type it directly into the combobox. All existing records should Update when you press the command button.
Where a record does not exist, it will be added to the next available row in range and your combobox list updated.
You should adjust code as required to meet your project need.
Hope Helpful
Dave