Please I am having problem with userform to activate a sheet then searching with two criteria that is by year and month to populate other fields and then update.Kindly assist me.Thank you
Private Sub ComboBox2_Change()
Dim x As Integer
‘ Loop through months (1 to 12) and add them to ComboBox2
With Me.ComboBox2
For x = 1 To 12
.AddItem MonthName(x)
Next x
End With
End Sub
Private Sub CommandButton1_Click()
Dim targetsheet As String
targetsheet = ComboBox1.Value
‘ Check if targetsheet is empty, exit the sub if true
If targetsheet = “” Then
Exit Sub
End If
With Worksheets(targetsheet)
Dim lastRow As Long
' Find the last row in column 1
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
' Fill in values from textboxes and combobox into the next row
.Cells(lastRow + 1, 1).Value = TextBox3.Value
.Cells(lastRow + 1, 2).Value = ComboBox2.Value
.Cells(lastRow + 1, 3).Value = TextBox1.Value
.Cells(lastRow + 1, 4).Value = TextBox2.Value
.Cells(lastRow + 1, 5).Value = TextBox4.Value
.Cells(lastRow + 1, 6).Value = TextBox5.Value
.Cells(lastRow + 1, 7).Value = TextBox6.Value
.Cells(lastRow + 1, 10).Value = TextBox7.Value
.Cells(lastRow + 1, 12).Value = TextBox8.Value
.Cells(lastRow + 1, 13).Value = TextBox9.Value
.Cells(lastRow + 1, 14).Value = TextBox10.Value
MsgBox "Data was added successfully"
' Clear the values of input fields after data is added
TextBox3.Value = ""
ComboBox2.Value = ""
TextBox1.Value = ""
TextBox2.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
End With
End Sub
Private Sub CommandButton2_Click()
Dim ws As Worksheet
Dim TextYear As Long
Dim TextMonth As Integer
Dim resultRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets("SheetName")
TextYear = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For TextMonth = 1 To 12
resultRow = 0
For i = 4 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If ws.Cells(i, 1).Value = TextYear And ws.Cells(i, 2).Value = TextMonth Then
' Fill in textboxes with values from the found row
TextYear = ws.Cells(i, 1).Value
TextMonth = ws.Cells(i, 2).Value
TextBox1.Value = ws.Cells(i, 3).Value
TextBox2.Value = ws.Cells(i, 4).Value
TextBox4.Value = ws.Cells(i, 5).Value
TextBox5.Value = ws.Cells(i, 6).Value
TextBox6.Value = ws.Cells(i, 7).Value
TextBox7.Value = ws.Cells(i, 8).Value
TextBox8.Value = ws.Cells(i, 9).Value
TextBox9.Value = ws.Cells(i, 10).Value
TextBox10.Value = ws.Cells(i, 11).Value
resultRow = 1
Exit For
End If
Next i
Next TextMonth
End Sub
Private Sub CommandButton3_Click()
‘ Unload the UserForm1
Unload UserForm1
End Sub
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim TextYear As Long
Dim TextMonth As Integer
Dim resultRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets("SheetName")
TextYear = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For TextMonth = 1 To 12
resultRow = 0
For i = 4 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
If ws.Cells(i, 1).Value = TextYear And ws.Cells(i, 2).Value = TextMonth Then
' Fill in textboxes with values from the found row
ws.Cells(i, 1).Value = TextYear
ws.Cells(i, 2).Value = TextMonth
ws.Cells(i, 3).Value = TextBox1.Value
ws.Cells(i, 4).Value = TextBox2.Value
ws.Cells(i, 5).Value = TextBox4.Value
ws.Cells(i, 6).Value = TextBox5.Value
ws.Cells(i, 7).Value = TextBox6.Value
ws.Cells(i, 8).Value = TextBox7.Value
ws.Cells(i, 9).Value = TextBox8.Value
ws.Cells(i, 10).Value = TextBox9.Value
ws.Cells(i, 11).Value = TextBox10.Value
resultRow = 1
Exit For
End If
Next i
Next TextMonth
MsgBox "Data was Updated successfully"
' Clear the values of input fields after data is added
TextBox3.Value = ""
ComboBox2.Value = ""
TextBox1.Value = ""
TextBox2.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox8.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
End Sub
Private Sub UserForm_Initialize()
Dim sh As Worksheet
ComboBox1.Clear
‘ Fill ComboBox1 with worksheet names from the workbook
For Each sh In ThisWorkbook.Worksheets
ComboBox1.AddItem sh.Name
Next sh
End Sub
Userform and the error