hello bạn
không biết bạn đã giải quyết vấn đề này xong chưa, tôi thi mới tập tành vba thôi,
gửi bạn tham khảo.
để dể tìm số liệu bạn nên tạo tiêu đề ở dòng đầu tiên của sheet data và sử dụng "SoGiay" làm key trong quá trình tìm kiếm, nếu bạn đổi key thì hãy đổi lại lệnh trong code
bạn hãy tạo thêm 2 CommandButton cho "Preview" và "Save" rồi chép 2 đạon code này vào cho mỗi button tương ứng nha
Private Sub CommandButton1_Click() ' đoạn code cho button Preview
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("DATA")
ws.Cells(1, 20) = Me.XSoGiay
If Range("T1").Value = "" Then
MsgBox " NHAP SO GIAY"
Cells(1, 20).Select
Exit Sub
End If
Cells.Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=Range("T1")
iRow = ws.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row - 1
If ws.Cells(iRow, 1).Value = "SoGiay" Then 'tui sử dụng "SoGiay" làm tiêu đề, nếu bạn sửa thì sửa lại chử này
MsgBox " Ban Nhap Sai So Giay"
Exit Sub
End If
Me.XSoGiay.Value = ws.Cells(iRow, 1).Value
Me.XSoQuyen.Value = ws.Cells(iRow, 2).Value
Me.XHovaTen.Value = ws.Cells(iRow, 3).Value
Me.XGioiTinh.Value = ws.Cells(iRow, 4).Value
Me.XNgaysinh.Value = ws.Cells(iRow, 5).Value
Me.XNoisinhcon.Value = ws.Cells(iRow, 6).Value
Me.XDantoccon.Value = ws.Cells(iRow, 7).Value
Me.XQuoctichcon.Value = ws.Cells(iRow, 8).Value
Me.XHovatencha.Value = ws.Cells(iRow, 9).Value
Me.XDantoccha.Value = ws.Cells(iRow, 10).Value
Me.XQuoctichcha.Value = ws.Cells(iRow, 11).Value
Me.XNamsinhcha.Value = ws.Cells(iRow, 12).Value
Me.XNoiocha.Value = ws.Cells(iRow, 13).Value
Me.XHovaTenme.Value = ws.Cells(iRow, 14).Value
Me.XDantocme.Value = ws.Cells(iRow, 15).Value
Me.XQuoctichme.Value = ws.Cells(iRow, 16).Value
Me.XNamsinhme.Value = ws.Cells(iRow, 17).Value
Me.XNoiome.Value = ws.Cells(iRow, 18).Value
ActiveSheet.AutoFilterMode = False
Range("T1").ClearContents
End Sub
Private Sub CommandButton2_Click() ' đoạn code cho save
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("DATA")
ws.Cells(1, 20) = Me.XSoGiay
Cells.Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=Range("T1")
iRow = ws.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row - 1
ws.Cells(iRow, 1).Value = Me.XSoGiay.Value
ws.Cells(iRow, 2).Value = Me.XSoQuyen.Value
ws.Cells(iRow, 3).Value = Me.XHovaTen.Value
ws.Cells(iRow, 4).Value = Me.XGioiTinh.Value
ws.Cells(iRow, 5).Value = Me.XNgaysinh.Value
ws.Cells(iRow, 6).Value = Me.XNoisinhcon.Value
ws.Cells(iRow, 7).Value = Me.XDantoccon.Value
ws.Cells(iRow, 8).Value = Me.XQuoctichcon.Value
ws.Cells(iRow, 9).Value = Me.XHovatencha.Value
ws.Cells(iRow, 10).Value = Me.XDantoccha.Value
ws.Cells(iRow, 11).Value = Me.XQuoctichcha.Value
ws.Cells(iRow, 12).Value = Me.XNamsinhcha.Value
ws.Cells(iRow, 13).Value = Me.XNoiocha.Value
ws.Cells(iRow, 14).Value = Me.XHovaTenme.Value
ws.Cells(iRow, 15).Value = Me.XDantocme.Value
ws.Cells(iRow, 16).Value = Me.XQuoctichme.Value
ws.Cells(iRow, 17).Value = Me.XNamsinhme.Value
ws.Cells(iRow, 18).Value = Me.XNoiome.Value
ActiveSheet.AutoFilterMode = False
Range("T1").ClearContents
ThisWorkbook.Save
End Sub
hy vọng 9dáp ứng được yêu cầu của bạn