Programming for Everybody: excel vba
Showing posts with label excel vba. Show all posts
Showing posts with label excel vba. Show all posts

search button in userform Excel and display all data in one label With Source Code



 Source Code:

Private Sub CommandButton1_Click()

If TextBox1.Text = "" Then

MsgBox "Please Enter ID"

Exit Sub

End If

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet6")

Dim i As Integer

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

If TextBox1.Text = ws.Cells(i, 1).Value Then

Label2.Caption = "First name: " & ws.Cells(i, 2).Value & vbCrLf & "" _

& "Last name: " & ws.Cells(i, 3).Value & vbCrLf & "" _

& "Marks: " & ws.Cells(i, 4).Value

Exit Sub

Else

Label2.Caption = "No data found"

End If

Next i

End Sub



🔍 Searching and selecting records efficiently inside an Excel VBA UserForm can significantly improve user experience.

🎯 In this tutorial, I demonstrate how to implement a dynamic search feature using a TextBox and display filtered results in a ListBox. Selecting a row automatically populates the UserForm fields with the corresponding data, making data editing and navigation faster and more reliable.

📺 Full tutorial on YouTube:

#Microsoft #ExcelVBA #VBA #UserForm #Automation #TechCommunity

 

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




Excel VBA tutorial for beginners- How to create login form in Microsoft Excel VBA with source code



In this video, we'll learn how to create a login form in Microsoft Excel using VBA. We'll show you how to create a user login form with a show and hide password textbox. This tutorial is useful if you need to create a login form for your website or application. If you're a Microsoft Excel user, then this video is for you! In this video, we'll show you how to create a user login form using VBA. We'll show you how to create a user login form with a show and hide password textbox. This tutorial is useful if you need to create a login form for your website or application. So make sure to watch the video and learn how to create a login form with 
Subscribe to my channel to find everyday new information in programming and Computerr Science Subscribe to @programmingforeverybody
https://www.youtube.com/@programmingforeverybody/?sub_confirmation=1
Source Code:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
passtxt.PasswordChar = ""
Else
passtxt.PasswordChar = "*"
End If
End Sub

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
Dim i As Integer
For i = 1 To ws.UsedRange.Rows.Count
If usertxt.Value = ws.Cells(i, 1).Value And passtxt.Value = ws.Cells(i, 2).Value Then
UserForm1.lbl_user.Caption = Me.usertxt.Value
Unload Me
UserForm1.Show
Exit Sub
End If
Next
MsgBox ("Error username or password")
End Sub

With source code Excel macro VBA tutorial| insert update delete in sheet excel using entry form

 



Private Sub CommandButton1_Click()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

sheet_v.Range("B" & lrow_v + 1).Value = TextBox2.Value

sheet_v.Range("C" & lrow_v + 1).Value = TextBox3.Value

sheet_v.Range("D" & lrow_v + 1).Value = TextBox4.Value

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

'MsgBox ("Data inserted successfully")

Call load_data

End Sub

Sub load_data()

Dim sheet_v As Worksheet

Set sheet_v = ThisWorkbook.Sheets("sheet1")

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

sheet_v.Range("A" & lrow_v + 1).Value = TextBox1.Value

With ListBox1

.ColumnCount = 4

.ColumnHeads = True

.ColumnWidths = "70,90,90,90"

.RowSource = "sheet1!A2:D" & lrow_v

End With

End Sub


Private Sub CommandButton2_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Sheets("sheet1").Cells(i, 1).Value = TextBox1.Text

Sheets("sheet1").Cells(i, 2).Value = TextBox2.Text

Sheets("sheet1").Cells(i, 3).Value = TextBox3.Text

Sheets("sheet1").Cells(i, 4).Value = TextBox4.Text

End If

Next i

End Sub


Private Sub CommandButton3_Click()

Dim lrow_v As Long

lrow_v = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

Dim i As Long

For i = 2 To lrow_v

If Sheets("sheet1").Cells(i, 1).Value = id.Text Then

Rows(i).Delete

End If

Next i

TextBox1.Value = ""

TextBox2.Value = ""

TextBox3.Value = ""

TextBox4.Value = ""

id.Value = ""

End Sub


Private Sub ListBox1_Click()


End Sub


Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

TextBox1.Value = ListBox1.Column(0)

TextBox2.Value = ListBox1.Column(1)

TextBox3.Value = ListBox1.Column(2)

TextBox4.Value = ListBox1.Column(3)

id.Value = ListBox1.Column(0)

End Sub


Private Sub UserForm_Click()


End Sub


Private Sub UserForm_Initialize()

Call load_data

End Sub