UserForm VBA Excel Phần cuối Code VBA Thêm ảnh
UserForm VBA Excel Phần cuối Code VBA Thêm ảnh. Giaoan.link chia sẻ phần cuối của hướng dẫn này và kèm theo code, file excel mẫu cho các bạn tham khảo. Bên dưới là video hướng dẫn chi tiết và link tải file mẫu.
TOÀN BỘ CODE VBA
Private Sub cmdAdd_Click()
Dim x As Long
Dim y As Worksheet
Set y = Sheet1
x = y.Range("A" & Rows.Count).End(xlUp).Row + 1
With y
.Cells(x, 1).Value = TextBox1.Text
.Cells(x, 2).Value = TextBox2.Text
.Cells(x, 3).Value = ComboBox1.Text
If Opt1.Value = True Then
.Cells(x, 4).Value = "Nam"
End If
If Opt2.Value = True Then
.Cells(x, 4).Value = "Nu"
End If
.Cells(x, 5).Value = TextBox3.Text
.Cells(x, 6).Value = TextBox4.Text
.Cells(x, 7).Value = TextBox5.Text
.Cells(x, 8).Value = TextBox6.Text
.Cells(x, 9).Value = TextBox7.Text
End With
On Error Resume Next
Dim I As String
I = TextBox1.Text
FileCopy FPATH, "C:\dataform\photo\" & I & ".JPG"
Unload Me
UserForm1.Show
End Sub
Private Sub cmdAddPhoto_Click()
Dim x As Integer
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
x = Application.FileDialog(msoFileDialogOpen).Show
If x <> 0 Then
FPATH = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Image1.Picture = LoadPicture(FPATH)
Image1.PictureSizeMode = 1
End If
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdDelete_Click()
Dim x As Long
Dim y As Long
x = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
For y = 6 To x
If Sheet1.Cells(y, 1).Value = TextBox8.Text Then
Rows(y).Delete shift:=xlUp
End If
Next y
End Sub
Private Sub cmdReset_Click()
Unload Me
UserForm1.Show
End Sub
Private Sub cmdSearch_Click()
Dim x As Long
Dim y As Long
x = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
For y = 6 To x
If Sheet1.Cells(y, 1).Value = TextBox8.Text Then
TextBox1 = Sheet1.Cells(y, 1).Value
TextBox2 = Sheet1.Cells(y, 2).Value
ComboBox1 = Sheet1.Cells(y, 3).Value
If Sheet1.Cells(y, 4).Value = "Nam" Then
Opt1.Value = True
End If
If Sheet1.Cells(y, 4).Value = "Nu" Then
Opt2.Value = True
End If
TextBox3 = Sheet1.Cells(y, 5).Value
TextBox4 = Sheet1.Cells(y, 6).Value
TextBox5 = Sheet1.Cells(y, 7).Value
TextBox6 = Sheet1.Cells(y, 8).Value
TextBox7 = Sheet1.Cells(y, 9).Value
On Error Resume Next
Image1.Picture = LoadPicture("C:\dataform\photo\" & TextBox8.Text & Value & ".JPG")
Image1.PictureSizeMode = 1
End If
Next y
End Sub
Private Sub cmdUpdate_Click()
Dim x As Long
Dim y As Long
x = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
For y = 6 To x
If Sheet1.Cells(y, 1).Value = TextBox8.Text Then
Sheet1.Cells(y, 1).Value = TextBox1.Text
Sheet1.Cells(y, 2).Value = TextBox2.Text
Sheet1.Cells(y, 3).Value = ComboBox1.Text
If Opt1.Value = True Then
Sheet1.Cells(y, 4).Value = "Nam"
End If
If Opt2.Value = True Then
Sheet1.Cells(y, 4).Value = "Nu"
End If
Sheet1.Cells(y, 5).Value = TextBox3.Text
Sheet1.Cells(y, 6).Value = TextBox4.Text
Sheet1.Cells(y, 7).Value = TextBox5.Text
Sheet1.Cells(y, 8).Value = TextBox6.Text
Sheet1.Cells(y, 9).Value = TextBox7.Text
On Error Resume Next
Dim I As String
I = TextBox1.Text
FileCopy FPATH, "C:\dataform\photo\" & I & ".JPG"
End If
Next y
End Sub
Private Sub UserForm_initialize()
TextBox1.Text = Application.WorksheetFunction.Max(Sheet1.Range("A:A")) + 1
On Error Resume Next
MkDir "C:\dataform"
MkDir "C:\dataform\photo"
End Sub
Private Sub listbox1_click()
TextBox8.Text = ListBox1.Column(0)
End Sub