Chuyên đề giải đáp những thắc mắc về code VBA

Liên hệ QC

maytinhvp01

Thành viên thường trực
Tham gia
27/7/13
Bài viết
390
Được thích
179
Mình muốn nhờ giải thich câu lệnh " If Ran.Cells(d, c) > max Then max = Ran.Cells(d, c) "
trong ví du:
Public Function LonNhat(Ran As Range)
Dim max As Double, v As Integer, d As Integer, c As Integer
max = Ran.Cells(1, 1)
For d = 1 To Ran.Rows.Count
For c = 1 To Ran.Columns.Count
If Ran.Cells(d, c) > max Then max = Ran.Cells(d, c)
Next c
Next d
v = Tim(max, Ran)
LonNhat = max
End Function
-------------------------------------------------------
[INFO1]Thông báo:
Vì topic này:
http://www.giaiphapexcel.com/forum/...ải-thích-các-code-đề-nghị-các-bạn-gửi-vào-đây
đã quá dài nên BQT đóng lại.
Nay tôi mở topic mới với cùng chủ đề: GIẢI THÍCH NHỮNG THẮC MẮC VỀ CODE
Các bạn nếu có nhu cầu giải thích code, vui lòng post tại đây nhé
NDU96081631

[/INFO1]
 
Chỉnh sửa lần cuối bởi điều hành viên:
Ồ, hóa ra nó không nhận trường hợp file có dấu tiếng Việt, không biết trường hợp xử lý như thế nào, các anh chị hướng dẫn em với nhé.

Chỉ còn cách sửa tên File không có dấu tiếng Việt, không có dấu cách cho chắc ăn.
Hình như VN chưa đặt hàng anh Bill chuyện này hay sao ấy (hic)
 
Upvote 0
Ồ, hóa ra nó không nhận trường hợp file có dấu tiếng Việt, không biết trường hợp xử lý như thế nào, các anh chị hướng dẫn em với nhé.
dhn46 nghĩ nếu anh cho tên file vào 1 ô Excel rồi lấy đó làm tham số mở ô thì chắc vẫn được chứ nhỉ?
 
Upvote 0
Ồ, hóa ra nó không nhận trường hợp file có dấu tiếng Việt, không biết trường hợp xử lý như thế nào, các anh chị hướng dẫn em với nhé.
Nói code không nhận file đặt tên tiếng việt có dấu thì oan cho anh Bill quá.
Bản thân "Shell.Application" là nhất định phải mở được chứ. Đương nhiên là phải có extension đàng hoàng.
 
Upvote 0
Nói code không nhận file đặt tên tiếng việt có dấu thì oan cho anh Bill quá.
Bản thân "Shell.Application" là nhất định phải mở được chứ. Đương nhiên là phải có extension đàng hoàng.

Tôi chưa thử, nhưng tên file lưu trong Folder ghi bằng bảng mã khác (ví dụ: Unicode), tên file trong code "đòi" mở bằng bảng mã khác (Ví dụ TCVN3 - ABC hay Vni for Windows), vẫn có "extension đáng hoàng" nó vẫn nhận được sao ta?
Chắc phải thử lại xem sao.
-----------------
Đã thử rồi. Híc! Càng luxubu hơn là không dấu tiếng Việt, không dấu cách.
 
Lần chỉnh sửa cuối:
Upvote 0
Ồ, hóa ra nó không nhận trường hợp file có dấu tiếng Việt, không biết trường hợp xử lý như thế nào, các anh chị hướng dẫn em với nhé.

Vừa thí nghiệm xong: Tên file có dấu tiếng Việt + tên Folder có dấu tiếng Việt ---> Tất cả đều mở tuốt
 
Upvote 0
Em nhờ các anh giải thích 2 code sau
Mã:
[COLOR=#007700][FONT=monospace]Private [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Sub CommandButton1_Click[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 50000[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 5[/FONT][/COLOR][COLOR=#007700][FONT=monospace]), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Worksheet[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each Ws In ThisWorkbook[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Worksheets
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Name [/FONT][/COLOR][COLOR=#007700][FONT=monospace]<> [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"KH" [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then
        sArr [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J4[/FONT][/COLOR][COLOR=#007700][FONT=monospace]], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J4[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlToRight[/FONT][/COLOR][COLOR=#007700][FONT=monospace])).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]5[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
        [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K [/FONT][/COLOR][COLOR=#007700][FONT=monospace]+ [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1
            [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 5
                dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next I
        Next J
    End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next Ws
Sheets[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"KH"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]:[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]E50000[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ClearContents
Sheets[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"KH"[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]K[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]5[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr
End Sub  [/FONT][/COLOR]



Mã:
Public
Mã:
[COLOR=#0000BB][FONT=monospace]Sub GPE[/FONT][/COLOR][COLOR=#007700][FONT=monospace]()
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dim sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace](), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]DK [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dic [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Object[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]CoL [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Worksheet
Set Dic [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]CreateObject[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"Scripting.Dictionary"[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]], [[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]A14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlDown[/FONT][/COLOR][COLOR=#007700][FONT=monospace])).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
DK [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]G12[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
ReDim dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To 1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Not Dic[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]exists[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then Dic[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Add Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I
Next I
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each Ws In Worksheets
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Name [/FONT][/COLOR][COLOR=#007700][FONT=monospace]<> [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]"KH" [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then
        CoL [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J4[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlToRight[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Column [/FONT][/COLOR][COLOR=#007700][FONT=monospace]- [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1
        sArr [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]B4[/FONT][/COLOR][COLOR=#007700][FONT=monospace]], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Ws[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]B65536[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]xlUp[/FONT][/COLOR][COLOR=#007700][FONT=monospace])).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace](, [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]CoL[/FONT][/COLOR][COLOR=#007700][FONT=monospace]).[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value
        [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]7 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
            If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]DK Then
                [/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]9 To UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]2[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
                    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
                    If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dic[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]exists[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Then dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Dic[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Item[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Tem[/FONT][/COLOR][COLOR=#007700][FONT=monospace]), [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]sArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]I[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]J[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
                [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next J
            End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
        [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next I
    End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next Ws
[/FONT][/COLOR][COLOR=#007700][FONT=monospace][[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]G14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]:[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]G50000[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]ClearContents
[/FONT][/COLOR][COLOR=#007700][FONT=monospace][[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]G14[/FONT][/COLOR][COLOR=#007700][FONT=monospace]].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Resize[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]UBound[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]1[/FONT][/COLOR][COLOR=#007700][FONT=monospace])) = [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]dArr
Set Dic [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Nothing
End Sub [/FONT][/COLOR]


http://www.giaiphapexcel.com/forum/...-hàm-INDEX-MATCH-lấy-dữ-liệu-tất-cả-các-sheet
 
Upvote 0
LOIgi.jpg
Các bác cho hỏi khi mở cửa sổ VBA hiện lỗi này là thế nào ạ. Phương án giải quyết thế nào ạ.
 
Upvote 0
Nhờ các tiền bối giải thích cho em code của hàm này với ạ. Em xin chân thành cảm ơn!

Function VT_KieuThep(ByVal FindKT As String) As Long
Const Start_Index_Data = 5
Dim Rng As Range
If Trim(FindKT) <> "" Then
With Sheet1.Range("C" & Start_Index_Data & ":C" & Sheet1.UsedRange.Rows.Count)
Set Rng = .Find(what:=FindKT, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
VT_KieuThep = Rng.Row
Else
VT_KieuThep = 0
End If
End With
End If
End Function
 
Upvote 0
Nhờ các tiền bối giải thích cho em code của hàm này với ạ. Em xin chân thành cảm ơn!

Function VT_KieuThep(ByVal FindKT As String) As Long
Const Start_Index_Data = 5
Dim Rng As Range
If Trim(FindKT) <> "" Then
With Sheet1.Range("C" & Start_Index_Data & ":C" & Sheet1.UsedRange.Rows.Count)
Set Rng = .Find(what:=FindKT, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
VT_KieuThep = Rng.Row
Else
VT_KieuThep = 0
End If
End With
End If
End Function
Nghĩ mãi mà không hiểu ai lại viết chi cái hàm này, trong khi Match có sẵn của excel thì không xài.
 
Upvote 0
Đây là một hàm dựa vào một chuỗi để tìm ra số hàng mà chuỗi đó tồn tại trong một vùng ở cột C của sheet1.

Nếu là tôi, tôi sẽ viết hàm này rộng hơn.

--------------------------------------------------------------

Tôi viết cho bạn đây:

Mã:
Function VT_KieuThep(ByVal FindKT, ByVal FindRange As Range) As Long
    On Error Resume Next
    VT_KieuThep = FindRange.Find(what:=FindKT, LookIn:=xlValues, Lookat:=xlWhole).Row
End Function

Công thức:

=VT_KieuThep(F1,$C$1:$C$34)

hoặc:

=VT_KieuThep("Nghĩa",$C$1:$C$34)
Vấn đề là khi đưa hàm của anh HTN vào thủ tục sau thì nó bị lỗi ạ, chắc là do người viết code quá cầu kỳ đọc khó hiều quá. Nhờ các anh xử lý giúp ạ.

Private Sub Worksheet_Change(ByVal Target As Range)
Const start_index = 7
Dim Row_Index As Long
Dim Row_Data As Long
Dim Row_Height As Long
Dim j As Long
If InStr(Target.Address, "$C$") > 0 Then 'Vi tri cua Cell tai cot C
If Target.Count <> 1 Then Exit Sub 'Neu chon lon hon thi bo qua
Row_Data = VT_KieuThep(Range("C" & Target.Row))
If Range("C" & Target.Row) <> "" And Row_Data > 0 Then
Row_Index = Target.Row
Row_Height = Sheet1.Range("D" & Row_Data).RowHeight
Sheet2.Range("D" & Row_Index).RowHeight = Row_Height
Sheet1.Activate
Sheet1.Range("D" & Row_Data & ":R" & Row_Data).Select
Application.CutCopyMode = False
Selection.Copy
Sheet2.Select
Sheet2.Range("D" & Row_Index).Select
ActiveSheet.Paste
Sheet2.Range("C" & Row_Index + 1).Select
Else
End If
End If
End Sub
 
Upvote 0
Vấn đề là khi đưa hàm của anh HTN vào thủ tục sau thì nó bị lỗi ạ, chắc là do người viết code quá cầu kỳ đọc khó hiều quá. Nhờ các anh xử lý giúp ạ.

Row_Data = VT_KieuThep(Range("C" & Target.Row))

cầu kỳ hay không là ý cá nhân. Chỉ là hàm của người ta cần 2 tham số, bạn chỉ cung cấp 1 thì nó lỗi là lẽ đương nhiên. Cái đơn giản này mà bạn không biết thì kêu giải thích code làm chi cho mất công.
 
Upvote 0
Web KT
Back
Top Bottom