Programming for Everybody: February 2025

VBA code for search ComboBox in userform Microsoft #Excel With Source Code



 Private Sub ComboBox1_Change()

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

Dim i As Integer

For i = 1 To ws.UsedRange.Rows.Count

If Me.ComboBox1.Column(0) = ws.Cells(i, 2).Value Then

Me.id_txt.Text = ws.Cells(i, 1).Value

Me.math_txt.Text = ws.Cells(i, 3).Value

Me.geo_txt.Text = ws.Cells(i, 4).Value

Me.chem_txt.Text = ws.Cells(i, 5).Value

Me.his_txt.Text = ws.Cells(i, 6).Value

Me.com_txt.Text = ws.Cells(i, 7).Value

End If

Next i

End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()

Dim lastrow, i As Integer

lastrow = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row

For i = 2 To lastrow

Dim vs As String

vs = Sheet1.Cells(i, "B")

Me.ComboBox1.AddItem vs

Next i

End Sub

Master Excel VBA: Insert, Update, Delete, and Search with Pictures + Free Source Code! 🚀

 Dim imagefile As String

Private Sub CommandButton1_Click()

imagefile = Application.GetOpenFilename(Title:="Select Image", _

FileFilter:="ImageFiles(*.png;*.Jpeg;*.JpG), *.png;*.Jpeg;*.JpG")

If imagefile = "False" Then

MsgBox "Please Select one Image"

Else

Me.Image1.Picture = LoadPicture(imagefile)

Me.Image1.PictureSizeMode = fmPictureSizeModeStretch


End If

End Sub


Private Sub CommandButton2_Click()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("Sheet1")

Dim last_row As Integer

last_row = sheet_v.Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & last_row + 1).Value = Me.code_student.Value

sheet_v.Range("b" & last_row + 1).Value = Me.name_txt.Value

sheet_v.Range("C" & last_row + 1).Value = Me.age_txt.Value

sheet_v.Range("D" & last_row + 1).Value = imagefile

MsgBox "The student inserted Successfully"

End Sub


Private Sub CommandButton3_Click()

Me.code_student.Value = ""

Me.name_txt.Value = ""

Me.age_txt.Value = ""

imagefile = ""

Me.Image1.Picture = LoadPicture(imagefile)

End Sub


Private Sub CommandButton4_Click()

If Me.search_txt.Text = "" Then

MsgBox "Please Enter code student"

Else

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("Sheet1")

Dim last_row, i As Integer

last_row = sheet_v.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To last_row

If Me.search_txt.Text = sheet_v.Cells(i, 1) Then

Me.code_student.Text = sheet_v.Cells(i, 1)

Me.name_txt.Value = sheet_v.Cells(i, 2)

Me.age_txt.Value = sheet_v.Cells(i, 3)

imagefile = sheet_v.Cells(i, 4)

Me.Image1.Picture = LoadPicture(imagefile)

Me.Image1.PictureSizeMode = fmPictureSizeModeStretch

Exit Sub

Else

Me.code_student.Value = ""

Me.name_txt.Value = ""

Me.age_txt.Value = ""

imagefile = ""

Me.Image1.Picture = LoadPicture(imagefile)

End If

Next i

End If

End Sub


Private Sub CommandButton5_Click()


Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("Sheet1")

Dim last_row, i As Integer

last_row = sheet_v.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To last_row

If Me.search_txt.Text = sheet_v.Cells(i, 1) Then

 sheet_v.Cells(i, 1) = Me.code_student.Text

 sheet_v.Cells(i, 2) = Me.name_txt.Value

sheet_v.Cells(i, 3) = Me.age_txt.Value

 sheet_v.Cells(i, 4) = imagefile

Me.Image1.Picture = LoadPicture(imagefile)

Me.Image1.PictureSizeMode = fmPictureSizeModeStretch


End If

Next i

End Sub


Private Sub CommandButton6_Click()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("Sheet1")

Dim last_row, i As Integer

last_row = sheet_v.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To last_row

If Me.search_txt.Text = sheet_v.Cells(i, 1) Then

Rows(i).Delete

End If

Next i

Me.code_student.Value = ""

Me.name_txt.Value = ""

Me.age_txt.Value = ""

imagefile = ""

Me.Image1.Picture = LoadPicture(imagefile)

End Sub


Private Sub UserForm_Click()

End Sub




Microsoft Excel Search| search in all columns Excel Using ComboBox in VBA #Excel userforms

 Dim columnindex_v As Integer

Private Sub populate_combo1()

Dim i, lastcolumn As Integer

lastcolumn = Sheet5.Cells(1, Columns.Count).End(xlToLeft).Column

For i = 1 To lastcolumn

Me.ComboBox1.AddItem Sheet5.Cells(1, i).Value

Next i

End Sub


Private Sub ComboBox1_Change()

getcolumnindex

getcolumnname

populate_combo2

End Sub

Private Sub getcolumnindex()

columnindex_v = WorksheetFunction.Match(Me.ComboBox1.Text, Sheet5.Range("1:1"), 0)

Me.columnindex_txt.Caption = columnindex_v

End Sub

Private Sub getcolumnname()

Me.columnname_txt.Caption = Split(Sheet5.Columns(columnindex_v).EntireColumn.Address(0, 0), ":")(0)

End Sub

Private Sub search_listbox1()

With Me.ListBox1

.Clear

.AddItem "RN"

.List(.ListCount - 1, 1) = "ID"

.List(.ListCount - 1, 2) = "Firstname"

.List(.ListCount - 1, 3) = "Lastname"

.List(.ListCount - 1, 4) = "Sport"

.List(.ListCount - 1, 5) = "Points"

.ColumnCount = 6

Dim lastrow, i As Integer

lastrow = Sheet5.Cells(Rows.Count, Me.columnname_txt.Caption).End(xlUp).Row

For i = 2 To lastrow

If Me.ComboBox2.Text = Sheet5.Cells(i, columnindex_v) Then

.AddItem

.List(.ListCount - 1, 0) = .ListCount - 1

.List(.ListCount - 1, 1) = Sheet5.Cells.Range("A" & i)

.List(.ListCount - 1, 2) = Sheet5.Cells.Range("B" & i)

.List(.ListCount - 1, 3) = Sheet5.Cells.Range("C" & i)

.List(.ListCount - 1, 4) = Sheet5.Cells.Range("D" & i)

.List(.ListCount - 1, 5) = Sheet5.Cells.Range("E" & i)

End If

Next i

End With

End Sub

Private Sub ComboBox2_Change()

search_listbox1

End Sub


Private Sub UserForm_Initialize()

populate_combo1

End Sub

Private Sub populate_combo2()

Me.ComboBox2.Clear

Dim lastrow, i As Integer

lastrow = Sheet5.Cells(Rows.Count, Me.columnname_txt.Caption).End(xlUp).Row

For i = 2 To lastrow

Dim value_str As String

value_str = Sheet5.Cells(i, Me.columnname_txt.Caption)

If WorksheetFunction.CountIf(Sheet5.Range(Me.columnname_txt.Caption & 2, Me.columnname_txt.Caption & i), value_str) = 1 Then

Me.ComboBox2.AddItem value_str

End If

Next i

End Sub