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:
Sub Boimau()
Sheets("UU").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YE").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YG").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YH").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YJ").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YN").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YQ").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YP").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YR").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YS").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("YT").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("QQ").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("NN").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("PP").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("VV").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("SS").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("TT").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("RR").Select
Range("N11:O38").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
Sheets("QQ").Select
End Sub
Nhờ các thầy cô rút ngẵn đoạn code giúp em được không ạ
 
Upvote 0
Nhờ các thầy cô rút ngẵn đoạn code giúp em được không ạ
Chạy thử xem sao
Mã:
Sub Boimau_()
Dim Ten
Dim Ws As Worksheet
Dim IDic As Object
Dim t
Set IDic = CreateObject("Scripting.Dictionary")
Ten = Array("UU", "YE", "YG", "YH", "YJ", "YN", "YQ", "YP", "YR", "YS", "YT", "QQ", "NN", "PP", "VV", "SS", "TT", "RR", "QQ")
For Each t In Ten
    IDic.Item(t) = ""
Next t
For Each Ws In Worksheets
    If IDic.exists(Ws.Name) Then
        Ws.Range("N11:O38").Select
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
        End With
    End If
Next Ws
End Sub
 
Upvote 0
Chạy thử xem sao
Mã:
Sub Boimau_()
Dim Ten
Dim Ws As Worksheet
Dim IDic As Object
Dim t
Set IDic = CreateObject("Scripting.Dictionary")
Ten = Array("UU", "YE", "YG", "YH", "YJ", "YN", "YQ", "YP", "YR", "YS", "YT", "QQ", "NN", "PP", "VV", "SS", "TT", "RR", "QQ")
For Each t In Ten
    IDic.Item(t) = ""
Next t
For Each Ws In Worksheets
    If IDic.exists(Ws.Name) Then
        Ws.Range("N11:O38").Select
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
        End With
    End If
Next Ws
End Sub
Nó báo lỗi thầy ơi
 

File đính kèm

  • anh1.png
    anh1.png
    178.7 KB · Đọc: 4
Upvote 0
PHP:
Sub GPE(Rng As Range)
 With Rng.Interior
    .Pattern = xlSolid:                                 .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight2:     .TintAndShade = 0.799981688894314
    .PatternTintAndShade = 0
 End With
End Sub
Mã:
Sub Boimau()
 Sheets("UU").Select
 Range("N11:O38").Select:           GPE Selection
 Sheets("YE").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YG").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YH").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YJ").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YN").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YQ").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YP").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YR").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YS").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("YT").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("QQ").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("NN").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("PP").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("VV").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("SS").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("TT").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("RR").Select
 Range("N11:O38").Select:            GPE Selection
 Sheets("QQ").Select
 End Sub
 
Upvote 0
PHP:
Sub GPE(Rng As Range)
With Rng.Interior
    .Pattern = xlSolid:                                 .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight2:     .TintAndShade = 0.799981688894314
    .PatternTintAndShade = 0
End With
End Sub
Mã:
Sub Boimau()
Sheets("UU").Select
Range("N11:O38").Select:           GPE Selection
Sheets("YE").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YG").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YH").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YJ").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YN").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YQ").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YP").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YR").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YS").Select
Range("N11:O38").Select:            GPE Selection
Sheets("YT").Select
Range("N11:O38").Select:            GPE Selection
Sheets("QQ").Select
Range("N11:O38").Select:            GPE Selection
Sheets("NN").Select
Range("N11:O38").Select:            GPE Selection
Sheets("PP").Select
Range("N11:O38").Select:            GPE Selection
Sheets("VV").Select
Range("N11:O38").Select:            GPE Selection
Sheets("SS").Select
Range("N11:O38").Select:            GPE Selection
Sheets("TT").Select
Range("N11:O38").Select:            GPE Selection
Sheets("RR").Select
Range("N11:O38").Select:            GPE Selection
Sheets("QQ").Select
End Sub
Nó chạy lâu quá thầy ơi. Hay máy em cùi mía nhỉ
 
Upvote 0
Upvote 0
Nhờ các thầy cô rút ngẵn đoạn code giúp em được không ạ
Mã:
Sub Boimau()
  Dim Ws As Worksheet, TenSheet As String
  TenSheet = "#UU#YE#YG#YH#YJ#YN#YQ#YP#YR#YS#YT#QQ#NN#PP#VV#SS#TT#RR#"
  For Each Ws In Worksheets
    If InStr(1, TenSheet, "#" & Ws.Name & "#") > 0 Then
      With Ws.Range("N11:O38").Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
      End With
    End If
  Next Ws
End Sub
 
Upvote 0
Sub XuatPDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
FileName = "ThisWorkbook & Name.pdf"
Quality = xlQualityStandard
IncludeDocProperties = True
IgnorePrintAreas = False
End Sub
Nhờ các thầy cô sửa giúp em đoạn code trên với ạ. Em muốn xuất tất cả sheet trong cùng 1 file. nhưng cái code trên nó lại chỉ xuất cái sheet hiện hành.
Nếu em muốn xuất PDF mà nó lưu vào khu vực mình muốn thì thêm code như nào ạ
 
Upvote 0
Nhờ các thầy cô sửa giúp em đoạn code trên với ạ. Em muốn xuất tất cả sheet trong cùng 1 file. nhưng cái code trên nó lại chỉ xuất cái sheet hiện hành.
Nếu em muốn xuất PDF mà nó lưu vào khu vực mình muốn thì thêm code như nào ạ
PHP:
Sub Main()
    Const thumuc_gioidanh As String = "D:\Gioi danh"
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Visible = xlSheetVisible Then
            Luu_PDF ws, thumuc_gioidanh
        End If
    Next ws
End Sub

Sub Luu_PDF(ByVal ws As Worksheet, ByVal Noi_luu As String)
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Noi_luu & "\" & ws.Name & ".pdf"
End Sub
 
Upvote 0
Nhờ mọi người giải thích dùm em ý nghĩa đoạn code bên dưới , qua trọng các dòng em đánh số .
code lồng nhiều hàm if quá ko hiểu nổi !!!
Mã:
Sub fifoNL()
  Dim nhapArr(), xuatArr(), Res()
  Dim I As Long, n As Long, sRow As Long
  Dim sNhap As Double, sXuat As Double, dXuat As Date
  Dim Ma As String, tmp As String
  With Sheets("DATANL")
    I = .Range("A" & Rows.Count).End(xlUp).Row
    If I < 3 Then MsgBox ("Khong co du lieu"): Exit Sub
    nhapArr = .Range("A3:D" & I).Value
    I = .Range("F" & Rows.Count).End(xlUp).Row
    If I < 3 Then MsgBox ("Khong co du lieu"): Exit Sub
    xuatArr = .Range("F3:H" & I).Value
    sRow = UBound(xuatArr)
1    ReDim Res(1 To sRow, 1 To 1)
  End With
  For I = 1 To sRow
    dXuat = xuatArr(I, 1): Ma = xuatArr(I, 2): sXuat = xuatArr(I, 3)
2     tmp = ""
    If Len(Ma) > 0 And sXuat > 0 Then
      For n = 1 To UBound(nhapArr)
        If nhapArr(n, 1) > dXuat Then Exit For
        If nhapArr(n, 2) = Ma Then
          sNhap = nhapArr(n, 3)
          If sNhap > 0 Then
            If sNhap >= sXuat Then
3              Res(I, 1) = tmp & nhapArr(n, 4)
4              If Len(tmp) > 0 Then Res(I, 1) = Res(I, 1) & "(" & sXuat & ")"
              nhapArr(n, 3) = sNhap - sXuat
              sXuat = 0
              Exit For
            Else
5              tmp = tmp & nhapArr(n, 4) & "(" & sNhap & "); "
              nhapArr(n, 3) = 0
              sXuat = sXuat - sNhap
            End If
          End If
        End If
      Next n
6      If sXuat > 0 Then Res(I, 1) = tmp & "Thieu(" & sXuat & ")"
    End If
  Next I
7  Sheets("DATANL").Range("I3").Resize(sRow) = Res
End Sub
 
Upvote 0
Nhờ mọi người giải thích dùm em ý nghĩa đoạn code bên dưới , qua trọng các dòng em đánh số .
code lồng nhiều hàm if quá ko hiểu nổi !!!
Mã:
Sub fifoNL()
  Dim nhapArr(), xuatArr(), Res()
  Dim I As Long, n As Long, sRow As Long
  Dim sNhap As Double, sXuat As Double, dXuat As Date
  Dim Ma As String, tmp As String
  With Sheets("DATANL")
    I = .Range("A" & Rows.Count).End(xlUp).Row
    If I < 3 Then MsgBox ("Khong co du lieu"): Exit Sub
    nhapArr = .Range("A3:D" & I).Value
    I = .Range("F" & Rows.Count).End(xlUp).Row
    If I < 3 Then MsgBox ("Khong co du lieu"): Exit Sub
    xuatArr = .Range("F3:H" & I).Value
    sRow = UBound(xuatArr)
1    ReDim Res(1 To sRow, 1 To 1)
  End With
  For I = 1 To sRow
    dXuat = xuatArr(I, 1): Ma = xuatArr(I, 2): sXuat = xuatArr(I, 3)
2     tmp = ""
    If Len(Ma) > 0 And sXuat > 0 Then
      For n = 1 To UBound(nhapArr)
        If nhapArr(n, 1) > dXuat Then Exit For
        If nhapArr(n, 2) = Ma Then
          sNhap = nhapArr(n, 3)
          If sNhap > 0 Then
            If sNhap >= sXuat Then
3              Res(I, 1) = tmp & nhapArr(n, 4)
4              If Len(tmp) > 0 Then Res(I, 1) = Res(I, 1) & "(" & sXuat & ")"
              nhapArr(n, 3) = sNhap - sXuat
              sXuat = 0
              Exit For
            Else
5              tmp = tmp & nhapArr(n, 4) & "(" & sNhap & "); "
              nhapArr(n, 3) = 0
              sXuat = sXuat - sNhap
            End If
          End If
        End If
      Next n
6      If sXuat > 0 Then Res(I, 1) = tmp & "Thieu(" & sXuat & ")"
    End If
  Next I
7  Sheets("DATANL").Range("I3").Resize(sRow) = Res
End Sub
Nếu muốn rõ ràng thì bạn nên gửi kèm theo file chứa code lên cho nhanh
 
Upvote 0
Đọc code này thấy sặc mùi sì tin GPE.
Lấy ở thớt nào thì chịu khó cho luôn đường dẫn, bà con khỏi mất công tìm hiểu.
(ví dụ có những chỗ code dở hay sai, không thấy ngữ cảnh làm sao biết)
 
Upvote 0
Em mới học VBA mà tìm và thử mãi vẫn chưa làm được chọn ô cuối cùng của mảng vừa chọn. Nhờ các bác giúp đỡ với ạ.
Trân trọng cảm ơn!

Mã:
Sub Chon_o_cuoi()
'
    Sheets("Sheet1").range("mang1").Select
'    Cần code tiếp theo để đưa chuột về ô cuối cùng của mảng "mang1"

End Sub
 
Upvote 0
Em mới học VBA mà tìm và thử mãi vẫn chưa làm được chọn ô cuối cùng của mảng vừa chọn. Nhờ các bác giúp đỡ với ạ.
Trân trọng cảm ơn!
PHP:
Sub Chon_O_Cuoi()
    Sheets("Sheet1").Range("mang1").Select
    With Selection
        If .Columns.Count = 1 Then
            .Offset()(.Rows.Count).Select
        Else
            .Offset()(.Cells.Count).Select
        End If
        MsgBox Selection.Address
    End With
End Sub
 
Upvote 0
Em mới học VBA mà tìm và thử mãi vẫn chưa làm được chọn ô cuối cùng của mảng vừa chọn. Nhờ các bác giúp đỡ với ạ.
Trân trọng cảm ơn!
Mã:
Sub Chon_o_cuoi()
  Sheets("Sheet1").Range("mang1").Select
  Selection(Selection.Rows.Count, Selection.Columns.Count).Select
End Sub
 
Upvote 0
Mã:
Sub Chon_o_cuoi()
' . . . . '
End Sub
PHP:
Sub Chon_O_Cuoi()
 '  Range1 = Union(Range("A2:B3"), [C1: D9])        '
    Sheets("Sheet1").Range("Range1").Select
    Selection(Selection.Rows.Count, Selection.Columns.Count).Select
    MsgBox Selection.Address
End Sub
 
Upvote 0
Web KT
Back
Top Bottom