Mình đã sửa theo như này, nhưng vẫn bị lỗi dòng màu vàng như cũ. ca này khó quá. Tại sao Excel 2013 trở đi ko lỗi, mà Excel 2010 trở về trước lỗi nhỉ? Mình nghĩ lỗi này xuất phát từ sự khác nhau ở các phiên bản excel, hoặc mình quên kích hoạt cái gì đó.Sửa:
chuyển thành:Mã:Sheet4.Range("N" & fRow & ":N" & lrBH).Formula = "=NUMBERVALUE(A" & fRow & ")" Set rngBH = Sheet4.Range("N" & fRow & ":N" & lrBH)
Mã:Sheet4.Range("N" & fRow & ":N" & lrBH).Formula = "=NUMBERVALUE(A" & fRow & ")" Thisworkbook.sheets(4).Activate Set rngBH = Thisworkbook.sheets(4).Range("N" & fRow & ":N" & lrBH)
Function Match_vba(Byval s As String, byval fRowtemp as byte,byval lrBHtem as long) As Long
Dim arr_temp As Variant
Dim itemp As Long
ThisWorkbook.Sheets(4).Activate 'lam viec voi sheet 4
arr_temp = ThisWorkbook.Sheets(4).Range("N" & fRowtemp & ":N" & lrBHtem).Value
ThisWorkbook.Sheets(2).Activate 'Quay tro ve sheet In Hoa Don
For itemp = LBound(arr, 1) To UBound(arr, 1) Step 1
If Val(CStr(arr(itemp, 1))) = Val(s) Then
Match_vba = itemp
Exit Function
End If
Next itemp
End Function
vtHD = Application.Match(CLng(LstHD.Text), rngBH, 0)
vtHD = Match_vba(LstHD.Text,fRow,lrBH)
Sau khi sửa như @vbano1 nói thì nó báo lỗi Type miss match ở chỗ hàmvậy đưa ra cách khắc phục trước, tìm nguyên nhân sau.
Anh cho hàm này vào module bất kỳ.
Sửa:Mã:Function Match_vba(Byval s As String, byval fRowtemp as byte,byval lrBHtem as long) As Long Dim arr_temp As Variant Dim itemp As Long ThisWorkbook.Sheets(4).Activate 'lam viec voi sheet 4 arr_temp = ThisWorkbook.Sheets(4).Range("N" & fRowtemp & ":N" & lrBHtem).Value ThisWorkbook.Sheets(2).Activate 'Quay tro ve sheet In Hoa Don For itemp = LBound(arr, 1) To UBound(arr, 1) Step 1 If Val(CStr(arr(itemp, 1))) = Val(s) Then Match_vba = itemp Exit Function End If Next itemp End Function
thành:Mã:vtHD = Application.Match(CLng(LstHD.Text), rngBH, 0)
@thanhphuongvip vừa cập nhật lại code, anh báo lại kết quả cho tôi biết nhé.Mã:vtHD = Match_vba(LstHD.Text,fRow,lrBH)
Function Match_vba(Byval s As String, byval fRowtemp as byte,byval lrBHtem as long) As Long
Dim arr_temp As Variant
Dim itemp As Long
ThisWorkbook.Sheets(4).Activate 'lam viec voi sheet 4
arr_temp = ThisWorkbook.Sheets(4).Range("N" & fRowtemp & ":N" & lrBHtem).Value
ThisWorkbook.Sheets(2).Activate 'Quay tro ve sheet In Hoa Don
For itemp = LBound(arr_temp, 1) To UBound(arr_temp, 1) Step 1
If Val(CStr(arr_temp(itemp, 1))) = Val(s) Then
Match_vba = itemp
Exit Function
End If
Next itemp
End Function
Public arr
'Public dic As New Scripting.Dictionary
Public dic As Object
Private Sub LstHD_Click()
Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer
b = LstHD.ListIndex
dk = LstHD.List(b, 0)
ReDim kq(1 To UBound(arr), 1 To 9)
If dic.Exists(dk) Then
s = dic.Item(dk)
For Each i In Split(s, "#")
a = a + 1
kq(a, 1) = a
For k = 2 To 9
kq(a, k) = arr(i, k + 1)
Next k
Next i
End If
With Sheets("HoaDon")
lr = .Range("A" & Rows.Count).End(xlUp).Row
If lr > 11 Then .Range("a12:I" & lr).ClearContents
If a Then .Range("A12:I12").Resize(a).Value = kq
End With
End Sub
Private Sub TxtHD_Change()
Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer
dk = TxtHD.Value
ReDim kq(1 To UBound(arr), 1 To 9)
If dic.Exists(dk) Then
s = dic.Item(dk)
For Each i In Split(s, "#")
a = a + 1
kq(a, 1) = a
For k = 2 To 9
kq(a, k) = arr(i, k + 1)
Next k
Next i
End If
With Sheets("HoaDon")
lr = .Range("A" & Rows.Count).End(xlUp).Row
If lr > 11 Then .Range("a12:I" & lr).ClearContents
If a Then .Range("A12:I12").Resize(a).Value = kq
End With
End Sub
Private Sub UserForm_Initialize()
Set dic = CreateObject("scripting.dictionary")
Dim i As Long, kq, lr As Long, dk As String, a As Integer
With Sheets("ChiTietBanHang")
lr = .Range("A" & Rows.Count).End(xlUp).Row
arr = .Range("A10:J" & lr).Value
End With
ReDim kq(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
dk = arr(i, 1)
If Not dic.Exists(dk) Then
a = a + 1
kq(a, 1) = dk
dic.Add dk, i
Else
dic.Item(dk) = dic.Item(dk) & "#" & i
End If
Next i
LstHD.List = kq
End Sub
Code này chạy quá chuẩn và quá nhanh luôn.@thanhphuongvip Bạn sử dụng code này xem được không nhé.
Mã:Public arr 'Public dic As New Scripting.Dictionary Public dic As Object Private Sub LstHD_Click() Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer b = LstHD.ListIndex dk = LstHD.List(b, 0) ReDim kq(1 To UBound(arr), 1 To 9) If dic.Exists(dk) Then s = dic.Item(dk) For Each i In Split(s, "#") a = a + 1 kq(a, 1) = a For k = 2 To 9 kq(a, k) = arr(i, k + 1) Next k Next i End If With Sheets("HoaDon") lr = .Range("A" & Rows.Count).End(xlUp).Row If lr > 11 Then .Range("a12:I" & lr).ClearContents If a Then .Range("A12:I12").Resize(a).Value = kq End With End Sub Private Sub TxtHD_Change() Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer dk = TxtHD.Value ReDim kq(1 To UBound(arr), 1 To 9) If dic.Exists(dk) Then s = dic.Item(dk) For Each i In Split(s, "#") a = a + 1 kq(a, 1) = a For k = 2 To 9 kq(a, k) = arr(i, k + 1) Next k Next i End If With Sheets("HoaDon") lr = .Range("A" & Rows.Count).End(xlUp).Row If lr > 11 Then .Range("a12:I" & lr).ClearContents If a Then .Range("A12:I12").Resize(a).Value = kq End With End Sub Private Sub UserForm_Initialize() Set dic = CreateObject("scripting.dictionary") Dim i As Long, kq, lr As Long, dk As String, a As Integer With Sheets("ChiTietBanHang") lr = .Range("A" & Rows.Count).End(xlUp).Row arr = .Range("A10:J" & lr).Value End With ReDim kq(1 To UBound(arr), 1 To 1) For i = 1 To UBound(arr) dk = arr(i, 1) If Not dic.Exists(dk) Then a = a + 1 kq(a, 1) = dk dic.Add dk, i Else dic.Item(dk) = dic.Item(dk) & "#" & i End If Next i LstHD.List = kq End Sub
Nếu như mình tự mò thì mình sẽ dùng hàm Vlookup để tra, chỉ biết tới đó thôi. Nếu @Snow24 viết cho mình để lấy 1 thông tin phần đầu thì những cái sau mình cũng biết lấy luôn, mà hình như Snow quên lấy cái thông tin phần đầu hóa đơn nên mình ko biết thêm nó vô chỗ nào, hihiĐề nghị đọc code thì phải hiểu và thử tự sửa đê. Coi như bài tập luyện tập.
@Snow24 thử không hỗ trợ nữa xem @thanhphuongvip có tự sửa được không nhé. Không lẽ đọc code không hiểu gì thì còn ý nghĩa gì nữa
ReDim kq(1 To UBound(arr), 1 To 13)
Public arr
'Public dic As New Scripting.Dictionary
Public dic As Object
Private Sub LstHD_Click()
Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer
Dim diengiai As String
Dim makhachhang As String
Dim khachhang As String 'giaiphapvba
Dim ngayxuat As String 'giaiphapvba
b = LstHD.ListIndex
dk = LstHD.List(b, 0)
ReDim kq(1 To UBound(arr), 1 To 13)
If dic.Exists(dk) Then
s = dic.Item(dk)
For Each i In Split(s, "#")
a = a + 1
kq(a, 1) = a
For k = 2 To 12
kq(a, k) = arr(i, k + 1)
Next k
diengiai = CStr(arr(i, 13))
makhachhang = CStr(arr(i, 12))
khachhang = CStr(arr(i, 11))
ngayxuat = CStr(arr(i, 2))
Next i
End If
With Sheets("HoaDon")
lr = .Range("A" & Rows.Count).End(xlUp).Row
If lr > 11 Then .Range("a12:I" & lr).ClearContents
If a Then .Range("A12:I12").Resize(a).Value = kq
End With
Sheets("HoaDon").Cells(6, 3).Value = diengiai
Sheets("HoaDon").Cells(5, 8).Value = makhachhang
Sheets("HoaDon").Cells(5, 3).Value = khachhang
Sheets("HoaDon").Cells(6, 8).Value = ngayxuat
End Sub
Private Sub TxtHD_Change()
Dim kq, i, s As String, a As Long, dk As String, k As Long, lr As Long, b As Integer
Dim diengiai As String
Dim makhachhang As String
Dim khachhang As String 'giaiphapvba
Dim ngayxuat As String 'giaiphapvba
dk = TxtHD.Value
ReDim kq(1 To UBound(arr), 1 To 13)
If dic.Exists(dk) Then
s = dic.Item(dk)
For Each i In Split(s, "#")
a = a + 1
kq(a, 1) = a
For k = 2 To 12
kq(a, k) = arr(i, k + 1)
Next k
tenkhachhang = CStr(arr(i, 11))
makhachhang = CStr(arr(i, 12))
khachhang = CStr(arr(i, 11))
ngayxuat = CStr(arr(i, 2))
Next i
End If
With Sheets("HoaDon")
lr = .Range("A" & Rows.Count).End(xlUp).Row
If lr > 11 Then .Range("a12:I" & lr).ClearContents
If a Then .Range("A12:I12").Resize(a).Value = kq
End With
Sheets("HoaDon").Cells(6, 3).Value = diengiai
Sheets("HoaDon").Cells(5, 8).Value = makhachhang
Sheets("HoaDon").Cells(5, 3).Value = khachhang
Sheets("HoaDon").Cells(6, 8).Value = ngayxuat
End Sub
Private Sub UserForm_Initialize()
Set dic = CreateObject("scripting.dictionary")
Dim i As Long, kq, lr As Long, dk As String, a As Integer
With Sheets("ChiTietBanHang")
lr = .Range("A" & Rows.Count).End(xlUp).Row
arr = .Range("A10:M" & lr).Value
End With
ReDim kq(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
dk = arr(i, 1)
If Not dic.Exists(dk) Then
a = a + 1
kq(a, 1) = dk
dic.Add dk, i
Else
dic.Item(dk) = dic.Item(dk) & "#" & i
End If
Next i
LstHD.List = kq
End Sub