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:
Bạn chưa mô tả chi tiết mục đích nên mình thiết kế Form thôi, việc còn lại thì bạn tự sửa code nhé.
Hi ad
Mục đích là mình muốn thiết kế 1 add-in dang các ribbon trên thanh menu, khi mình click vào thì nó hình ra như hình dưới
219902
Phục vụ cho công việc lập lại tiêu đề cho từng row dữ liệu. Nghĩa là trong file mình gửi, sheet đầu là sheet dữ liệu, 1 tiêu đề chung cho mọi người. Khi in phiếu lương phát cho mọi người thì mình phải copy tiêu đề đó cho từng người tương ứng.Đoạn code mình gửi là mình copy từ file ra.Mà file này của 1 ng trên mạng, giờ áp vào cty thì mỗi lần làm mình phải vào code sửa lại, nên giờ muốn nhờ ad giúp làm 1 cái add-in. khi làm chỉ click chọn thôi, ko có vào code sửa
Mong là hiểu dc ý mình diễn giải
 
Upvote 0
Hi ad
Mục đích là mình muốn thiết kế 1 add-in dang các ribbon trên thanh menu, khi mình click vào thì nó hình ra như hình dưới
View attachment 219902
Phục vụ cho công việc lập lại tiêu đề cho từng row dữ liệu. Nghĩa là trong file mình gửi, sheet đầu là sheet dữ liệu, 1 tiêu đề chung cho mọi người. Khi in phiếu lương phát cho mọi người thì mình phải copy tiêu đề đó cho từng người tương ứng.Đoạn code mình gửi là mình copy từ file ra.Mà file này của 1 ng trên mạng, giờ áp vào cty thì mỗi lần làm mình phải vào code sửa lại, nên giờ muốn nhờ ad giúp làm 1 cái add-in. khi làm chỉ click chọn thôi, ko có vào code sửa
Mong là hiểu dc ý mình diễn giải
Bạn phải giải thích rõ mới làm được.
Thứ nhất là khung màu đỏ thứ nhất (Titles Range) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Thứ hai là khung màu đỏ thứ hai (Insert Range) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Thứ ba là khung màu đỏ thứ ba (Interval rows) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Giải thích cụ thể và chi tiết, nếu được thì giải thích rõ ràng càng tốt.
 
Upvote 0
Bạn phải giải thích rõ mới làm được.
Thứ nhất là khung màu đỏ thứ nhất (Titles Range) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Thứ hai là khung màu đỏ thứ hai (Insert Range) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Thứ ba là khung màu đỏ thứ ba (Interval rows) là nó lặp lại chổ nào trong file (Cụ thể hơn là nó lấy từ chổ nào của sheet data và chuyển sang sheet result thì nó nằm ở chổ nào?)
Giải thích cụ thể và chi tiết, nếu được thì giải thích rõ ràng càng tốt.
Hi ad, em giải thích tí
1. Hình có 3 khung đỏ với file là ko có gì liên quan, ý e là trong file excel khi bấm nút màu tím thì nó tự chạy dữ liệu (từ sheet DATA), trong file excel có code VBA, nó định sẵn title từ đâu tới đâu, fix sẵn luôn. Giờ em muốn mình tạo 1 nút, khi bấm vào thì nó show cái form giống hình có 3 khung đỏ
a. Khung đỏ đầu: mình muốn tiêu đề nào lập lại thì mình khối chọn, làm vậy nó sẽ động chứ k tĩnh
b. Khung đỏ thứ 2: là dữ liệu nào muốn dc chèn tiêu đề vào, nhưng trong file có 91 người, thì 91 người sẽ dc chèn tiêu đề (xem sheet ketqua)
c. Khung đỏ 3: là muốn bao nhiêu dòng tiêu đề lập lại 1 lần. Như trong file thì mỗi row tiêu đề sẽ lập lại, vd tương lại mình sẽ có nhu cầu cứ sau 5 dòng thì tiêu đề lập lại
vd1: sau 1 row thì lập lại tiêu đề
ABCDEF->là tieu de
111111->du lieu dong 1
ABCDEF->là tieu de
222222->du lieu dong 2
vd2: sau 2 row thì lập lai tiêu đề
ABCDEF->là tieu de
111111->du lieu dong 1
222222->du lieu dong 2
ABCDEF->là tieu de
333333->du lieu dong 3
444444->du lieu dong 4
---
2. Hình có 3 khung đỏ thực chất nó là 1 chức năng trong 1 add-in tên là Kutools
Link tham khảo: https://www.extendoffice.com/documents/excel/4624-excel-header-row-print.html
Nhưng do cái này chỉ cho dùng thử, chứ dùng lâu phải mua key
3. Hiện tại em tạm gọi là có code lập lại tiêu đề rồi nên muốn nâng tiếp 1 bước thành dạng add-in, tạo 1 ribbon trên thanh menu,môi lần dùng chỉ cần click vào, và mình thao tác động, chứ k phải mỗi lần làm phải vào chỉnh sửa code.
Chắc đến đây ad hiểu ý em phải ko
 
Upvote 0
Hi ad, em giải thích tí
1. Hình có 3 khung đỏ với file là ko có gì liên quan, ý e là trong file excel khi bấm nút màu tím thì nó tự chạy dữ liệu (từ sheet DATA), trong file excel có code VBA, nó định sẵn title từ đâu tới đâu, fix sẵn luôn. Giờ em muốn mình tạo 1 nút, khi bấm vào thì nó show cái form giống hình có 3 khung đỏ
a. Khung đỏ đầu: mình muốn tiêu đề nào lập lại thì mình khối chọn, làm vậy nó sẽ động chứ k tĩnh
b. Khung đỏ thứ 2: là dữ liệu nào muốn dc chèn tiêu đề vào, nhưng trong file có 91 người, thì 91 người sẽ dc chèn tiêu đề (xem sheet ketqua)
c. Khung đỏ 3: là muốn bao nhiêu dòng tiêu đề lập lại 1 lần. Như trong file thì mỗi row tiêu đề sẽ lập lại, vd tương lại mình sẽ có nhu cầu cứ sau 5 dòng thì tiêu đề lập lại
vd1: sau 1 row thì lập lại tiêu đề
ABCDEF->là tieu de
111111->du lieu dong 1
ABCDEF->là tieu de
222222->du lieu dong 2
vd2: sau 2 row thì lập lai tiêu đề
ABCDEF->là tieu de
111111->du lieu dong 1
222222->du lieu dong 2
ABCDEF->là tieu de
333333->du lieu dong 3
444444->du lieu dong 4
---
2. Hình có 3 khung đỏ thực chất nó là 1 chức năng trong 1 add-in tên là Kutools
Link tham khảo: https://www.extendoffice.com/documents/excel/4624-excel-header-row-print.html
Nhưng do cái này chỉ cho dùng thử, chứ dùng lâu phải mua key
3. Hiện tại em tạm gọi là có code lập lại tiêu đề rồi nên muốn nâng tiếp 1 bước thành dạng add-in, tạo 1 ribbon trên thanh menu,môi lần dùng chỉ cần click vào, và mình thao tác động, chứ k phải mỗi lần làm phải vào chỉnh sửa code.
Chắc đến đây ad hiểu ý em phải ko
Bạn xem thử đúng yêu cầu của mình chưa nhé, nếu chưa đúng thì tối tính tiếp, có việc bận rồi.
 

File đính kèm

Upvote 0
Bạn xem thử đúng yêu cầu của mình chưa nhé, nếu chưa đúng thì tối tính tiếp, có việc bận rồi.
Dear ad
Đầu tiên mình tks nhiều, cơ bản nó giống ý mình muốn rồi, nhưng kế qua hiện tại còn 1 tí lỗi nhỏ, nhờ ad fix dùm mình luôn nha.
219946
1. Như hình 1 thì sau khi mình chọn các đối tượng tương ứng trong form thì dòng dữ liệu bị lệch so với tiêu đề, vả lại giữa tiêu đề và dòng dữ liệu có khoảng trắng, bỏ luôn được không
2. Trong sheet Data có 91 người, ban đầu mình vd chỉ chọn cho tiêu đề lập lại 3 người đầu tiên thôi, file xuất quả đúng là chỉ có 3 người đó
nhưng 3 người đó bị lập đi lại lại nhiều lần, có cách nào chỉ hiện thị đúng số người mình chọn thôi.
4. Hiện tại mình chọn 3 người thì đồng nghĩa với việc sẽ có 3 cái tiêu đề cho 3 người đó, hiện tại 3 người chỉ có 1 tiêu đề
vd kết qua mong muốn là
AAAA->Tiêu đề
Người 1
AAAA->Tiêu đề
Người 2
AAAA->Tiêu đề
Người 3
----
cái này giống như phiếu lương con, mỗi tháng cắt ra đưa cho mỗi người 1 tờ giấy nhỏ để họ xem
Ad xem lại giúp mình.
 
Upvote 0

File đính kèm

Upvote 0
Em có biến X , em muốn so sánh biến x với vòng lặp for từ a1 đến a10 , nếu chỉ cần có 1 giá trị trong for = x thì ô b1 = "đúng"
- giúp e viết code với
 
Upvote 0
Bạn hãy cho biết thêm: Kiểu dữ liệu của biến X thân thương của bạn;
Tạm là vầy trong khi chờ đợi:
PHP:
 Dim X, J as Long
 For J = 1 To 10
    If Cells(J, "A").Value = X Then
         [B1].Value="OK":      Exit For
    End If
 Next J
 
Upvote 0
Bạn hãy cho biết thêm: Kiểu dữ liệu của biến X thân thương của bạn;
Tạm là vầy trong khi chờ đợi:
PHP:
Dim X, J as Long
For J = 1 To 10
    If Cells(J, "A").Value = X Then
         [B1].Value="OK":      Exit For
    End If
Next J
Bác Sa thức muộn vậy bác.
 
Upvote 0
Có anh cho em hỏi, về code này, bây giờ khi em xóa trắng data cũ và nhập vào data mới, thì dictonary cũng đồng thời bị mất hết keys và items cũ. Có cách nào để vẫn giữ lại item cũ trong Dic không ạ. Đồng thời em muốn xuất data bằng một sub khác, nhưng khi gọi lại các biến của sub DicItem thì lại không đượ, mong nhận được sự giúp đỡ của các anh.
Mã:
Option Explicit
Sub DicItem()
Dim SArr, RArr, TmpArr, Dic1, MaxCols As Long
Dim i As Long, s As Long, EndR As Long, n As Long, Tmp As Long
Dim result()

Set Dic1 = CreateObject("Scripting.Dictionary")

With Dic1
EndR = Sheet1.[A100000].End(xlUp).Row
SArr = Sheet1.Range("A2:DF" & EndR).Value
For i = 1 To EndR - 1
    If Not .Exists(SArr(i, 105)) Then
        s = s + 1
        .Add SArr(i, 105), Array(SArr(i, 110), SArr(i, 18), SArr(i, 19))
        MaxCols = 1
    Else                                             '
        TmpArr = .item(SArr(i, 105))
        Tmp = UBound(.item(SArr(i, 105)))
        ReDim Preserve TmpArr(Tmp + 3)
        TmpArr(Tmp + 1) = SArr(i, 110)
        .item(SArr(i, 105)) = TmpArr
        TmpArr(Tmp + 2) = SArr(i, 18)
        .item(SArr(i, 105)) = TmpArr
        TmpArr(Tmp + 3) = SArr(i, 19)
        .item(SArr(i, 105)) = TmpArr
        If MaxCols < Tmp + 1 Then MaxCols = Tmp + 1
    End If
Next

Sheet8.[A4].Resize(s, 1) = Application.Transpose(.keys)
RArr = Sheet8.[A4].Resize(s, 1).Value

For i = 1 To .Count
Sheet8.Range("B" & i + 3).Resize(1, UBound(.item(RArr(i, 1))) + 1) = .item(RArr(i, 1))
Next

End With

End Sub
 
Upvote 0
Có anh cho em hỏi, về code này, bây giờ khi em xóa trắng data cũ và nhập vào data mới, thì dictonary cũng đồng thời bị mất hết keys và items cũ. Có cách nào để vẫn giữ lại item cũ trong Dic không ạ. Đồng thời em muốn xuất data bằng một sub khác, nhưng khi gọi lại các biến của sub DicItem thì lại không đượ, mong nhận được sự giúp đỡ của các anh.
Mã:
Option Explicit
Sub DicItem()
Dim SArr, RArr, TmpArr, Dic1, MaxCols As Long
Dim i As Long, s As Long, EndR As Long, n As Long, Tmp As Long
Dim result()

Set Dic1 = CreateObject("Scripting.Dictionary")

With Dic1
EndR = Sheet1.[A100000].End(xlUp).Row
SArr = Sheet1.Range("A2:DF" & EndR).Value
For i = 1 To EndR - 1
    If Not .Exists(SArr(i, 105)) Then
        s = s + 1
        .Add SArr(i, 105), Array(SArr(i, 110), SArr(i, 18), SArr(i, 19))
        MaxCols = 1
    Else                                             '
        TmpArr = .item(SArr(i, 105))
        Tmp = UBound(.item(SArr(i, 105)))
        ReDim Preserve TmpArr(Tmp + 3)
        TmpArr(Tmp + 1) = SArr(i, 110)
        .item(SArr(i, 105)) = TmpArr
        TmpArr(Tmp + 2) = SArr(i, 18)
        .item(SArr(i, 105)) = TmpArr
        TmpArr(Tmp + 3) = SArr(i, 19)
        .item(SArr(i, 105)) = TmpArr
        If MaxCols < Tmp + 1 Then MaxCols = Tmp + 1
    End If
Next

Sheet8.[A4].Resize(s, 1) = Application.Transpose(.keys)
RArr = Sheet8.[A4].Resize(s, 1).Value

For i = 1 To .Count
Sheet8.Range("B" & i + 3).Resize(1, UBound(.item(RArr(i, 1))) + 1) = .item(RArr(i, 1))
Next

End With

End Sub
Bạn khai báo biến Public nhé.Khai báo ngoài sub.Bạn thử xem.
 
Upvote 0
Xin chào mọi người
tôi có làm 1 sub để tạo mô hình mẫu
dùng application.inputbox để lựa chọn ô sẽ lưu mô hình mẫu

1. Nhưng chỉ ra kết quả mong muốn trong ActiveSheet là đúng (hình 1)- Sheet2 là sheet hiện hoạt
2. Còn những sheets (sheet KetQua không được kích hoạt) khác thì định đạng không đúng, vì sao vậy? (hình 2)
3. Khác phục như thế nào?
220159
220160
 

File đính kèm

Upvote 0
Xin chào mọi người
tôi có làm 1 sub để tạo mô hình mẫu
dùng application.inputbox để lựa chọn ô sẽ lưu mô hình mẫu

1. Nhưng chỉ ra kết quả mong muốn trong ActiveSheet là đúng (hình 1)- Sheet2 là sheet hiện hoạt
2. Còn những sheets (sheet KetQua không được kích hoạt) khác thì định đạng không đúng, vì sao vậy? (hình 2)
3. Khác phục như thế nào?
View attachment 220159
View attachment 220160
Bạn bị sai ở tất cả các lệnh cells(..., ...), do lệnh cells nó hiểu là sheet hiện tại. Để khắc phục bạn thử sửa lại code thế này.
Mã:
Sub MoHinhMau()
Dim DesTemp As Range, Des As Range

On Error Resume Next
    Set DesTemp = Application.InputBox("Ch" & ChrW(7885) & "n ô t" & ChrW(7841) & _
        "o mô hình m" & ChrW(7851) & "u", "Mô hình m" & ChrW(7851) & _
        "u", Default:=ActiveWindow.ActiveCell.Address, Type:=8)

If Err.Number <> 0 Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

If IsArray(DesTemp) = True Then
   Set Des = DesTemp.Cells(1, 1)
Else
    Set Des = DesTemp
End If

With Des.Resize(11, 7)
    .Clear
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
End With
    With Des.Resize(9, 5)

        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
    
    With Des.Offset(10, 1).Resize(, 4)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
    With Des.Offset(2, 6).Resize(7)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
    
    With Des.Offset(3, 2).Resize(6, 3)
        .Borders(xlEdgeLeft).Weight = xlMedium
        .Borders(xlEdgeRight).Weight = xlMedium
        .Borders(xlEdgeTop).Weight = xlMedium
        .Borders(xlEdgeBottom).Weight = xlMedium
        .Borders(xlInsideVertical).Weight = xlMedium
        .Borders(xlInsideHorizontal).Weight = xlMedium
        .Interior.Color = 65280
    End With
    
    With Des.Resize(2, 1)
        .MergeCells = True
        .FormulaR1C1 = "Day"
        .Font.Bold = True
    End With
    With Des.Resize(11, 7)
        .Cells(1, 2).FormulaR1C1 = "L" & ChrW(432) & ChrW(7907) & "ng (Kg)"
        .Cells(1, 2).Font.Bold = True
        .Cells(1, 3).FormulaR1C1 = "10000"
        .Cells(1, 4).FormulaR1C1 = "4900"
        .Cells(1, 5).FormulaR1C1 = "4900"
        .Cells(2, 2).FormulaR1C1 = "Kho"
        .Cells(2, 2).Font.Bold = True
        .Cells(2, 3).FormulaR1C1 = "1250"
        .Cells(2, 4).FormulaR1C1 = "652"
        .Cells(2, 5).FormulaR1C1 = "590"
        .Cells(3, 1).FormulaR1C1 = "Yêu C" & ChrW(7847) & "u" & Chr(10) & "(Kg)"
        .Cells(3, 1).Font.Bold = True
        .Cells(3, 2).FormulaR1C1 = "So luong" & Chr(10) & "-----------" & "Tip"
    
        .Cells(3, 2).Font.Bold = True
        .Cells(3, 3).FormulaR1C1 = "2"
        .Cells(3, 4).FormulaR1C1 = "6"
        .Cells(3, 5).FormulaR1C1 = "8"
        .Cells(3, 7).FormulaR1C1 = "K" & ChrW(7871) & "t qu" & ChrW(7843)
        .Cells(3, 7).Font.Bold = True
        .Cells(4, 1).FormulaR1C1 = "4000"
        .Cells(4, 2).FormulaR1C1 = "91"
        .Cells(4, 3).FormulaR1C1 = "0"
        .Cells(4, 4).FormulaR1C1 = "1"
        .Cells(4, 5).FormulaR1C1 = "0"
        .Cells(4, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-3]C[-4]:R[-3]C[-2]*R[-1]C[-4]:R[-1]C[-2]*RC[-4]:RC[-2]/R[-2]C[-4]:R[-2]C[-2])"
        .Cells(5, 1).FormulaR1C1 = "2000"
        .Cells(5, 2).FormulaR1C1 = "127"
        .Cells(5, 3).FormulaR1C1 = "1"
        .Cells(5, 4).FormulaR1C1 = "0"
        .Cells(5, 5).FormulaR1C1 = "0"
        .Cells(5, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-4]C[-4]:R[-4]C[-2]*R[-2]C[-4]:R[-2]C[-2]*RC[-4]:RC[-2]/R[-3]C[-4]:R[-3]C[-2])"
        .Cells(6, 1).FormulaR1C1 = "25000"
        .Cells(6, 2).FormulaR1C1 = "153"
        .Cells(6, 3).FormulaR1C1 = "2"
        .Cells(6, 4).FormulaR1C1 = "0"
        .Cells(6, 5).FormulaR1C1 = "2"
        .Cells(6, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-5]C[-4]:R[-5]C[-2]*R[-3]C[-4]:R[-3]C[-2]*RC[-4]:RC[-2]/R[-4]C[-4]:R[-4]C[-2])"
        .Cells(7, 1).FormulaR1C1 = "5200"
        .Cells(7, 2).FormulaR1C1 = "173"
        .Cells(7, 3).FormulaR1C1 = "2"
        .Cells(7, 4).FormulaR1C1 = "0"
        .Cells(7, 5).FormulaR1C1 = "0"
        .Cells(7, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-6]C[-4]:R[-6]C[-2]*R[-4]C[-4]:R[-4]C[-2]*RC[-4]:RC[-2]/R[-5]C[-4]:R[-5]C[-2])"
        .Cells(8, 1).FormulaR1C1 = "7200"
        .Cells(8, 2).FormulaR1C1 = "233"
        .Cells(8, 3).FormulaR1C1 = "2"
        .Cells(8, 4).FormulaR1C1 = "0"
        .Cells(8, 5).FormulaR1C1 = "0"
        .Cells(8, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-7]C[-4]:R[-7]C[-2]*R[-5]C[-4]:R[-5]C[-2]*RC[-4]:RC[-2]/R[-6]C[-4]:R[-6]C[-2])"
        .Cells(9, 1).FormulaR1C1 = "42000"
        .Cells(9, 2).FormulaR1C1 = "277"
        .Cells(9, 3).FormulaR1C1 = "0"
        .Cells(9, 4).FormulaR1C1 = "2"
        .Cells(9, 5).FormulaR1C1 = "1"
        .Cells(9, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-8]C[-4]:R[-8]C[-2]*R[-6]C[-4]:R[-6]C[-2]*RC[-4]:RC[-2]/R[-7]C[-4]:R[-7]C[-2])"
        .Cells(11, 2).FormulaR1C1 = "Thua"
        .Cells(11, 2).Font.Bold = True
        .Cells(11, 3).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-1]:R[-2]C[-1]*R[-7]C:R[-2]C)"
        .Cells(11, 4).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-2]:R[-2]C[-2]*R[-7]C:R[-2]C)"
        .Cells(11, 5).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-3]:R[-2]C[-3]*R[-7]C:R[-2]C)"
        .Columns.AutoFit
        .Rows.AutoFit
        .Columns(6).EntireColumn.ColumnWidth = 3
    End With
    Des.Offset(, 2).Resize(, 3).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2, 6).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = False

End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Bạn bị sai ở tất cả các lệnh cells(..., ...), do lệnh cells nó hiểu là sheet hiện tại. Để khắc phục bạn thử sửa lại code thế này.
Mã:
Sub MoHinhMau()
...............................................
    With Des.Resize(9, 5)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
...............................................
End Sub
Anh có tổng kết các cách tô viềng ở bài này VBA, tô viềng trong Excel
 
Upvote 0
Vài ngày trước mình có tải một file VBA chèn ảnh vào exel như thế này. Khi ở trạng thái wooksheet file vẫn hiện hình ảnh bình thường, tuy nhiên khi chuyển sang chế độ in thì file không hiển thị hình ảnh để in. Anh/Chị nào giúp em sửa lại code để in được hình với. Dja Em cảm ơn nhiều ạ!
 

File đính kèm

Upvote 0
Bạn bị sai ở tất cả các lệnh cells(..., ...), do lệnh cells nó hiểu là sheet hiện tại. Để khắc phục bạn thử sửa lại code thế này.
Mã:
Sub MoHinhMau()
Dim DesTemp As Range, Des As Range

On Error Resume Next
    Set DesTemp = Application.InputBox("Ch" & ChrW(7885) & "n ô t" & ChrW(7841) & _
        "o mô hình m" & ChrW(7851) & "u", "Mô hình m" & ChrW(7851) & _
        "u", Default:=ActiveWindow.ActiveCell.Address, Type:=8)

If Err.Number <> 0 Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

If IsArray(DesTemp) = True Then
   Set Des = DesTemp.Cells(1, 1)
Else
    Set Des = DesTemp
End If

With Des.Resize(11, 7)
    .Clear
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
End With
    With Des.Resize(9, 5)

        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
  
    With Des.Offset(10, 1).Resize(, 4)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
    With Des.Offset(2, 6).Resize(7)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
  
    With Des.Offset(3, 2).Resize(6, 3)
        .Borders(xlEdgeLeft).Weight = xlMedium
        .Borders(xlEdgeRight).Weight = xlMedium
        .Borders(xlEdgeTop).Weight = xlMedium
        .Borders(xlEdgeBottom).Weight = xlMedium
        .Borders(xlInsideVertical).Weight = xlMedium
        .Borders(xlInsideHorizontal).Weight = xlMedium
        .Interior.Color = 65280
    End With
  
    With Des.Resize(2, 1)
        .MergeCells = True
        .FormulaR1C1 = "Day"
        .Font.Bold = True
    End With
    With Des.Resize(11, 7)
        .Cells(1, 2).FormulaR1C1 = "L" & ChrW(432) & ChrW(7907) & "ng (Kg)"
        .Cells(1, 2).Font.Bold = True
        .Cells(1, 3).FormulaR1C1 = "10000"
        .Cells(1, 4).FormulaR1C1 = "4900"
        .Cells(1, 5).FormulaR1C1 = "4900"
        .Cells(2, 2).FormulaR1C1 = "Kho"
        .Cells(2, 2).Font.Bold = True
        .Cells(2, 3).FormulaR1C1 = "1250"
        .Cells(2, 4).FormulaR1C1 = "652"
        .Cells(2, 5).FormulaR1C1 = "590"
        .Cells(3, 1).FormulaR1C1 = "Yêu C" & ChrW(7847) & "u" & Chr(10) & "(Kg)"
        .Cells(3, 1).Font.Bold = True
        .Cells(3, 2).FormulaR1C1 = "So luong" & Chr(10) & "-----------" & "Tip"
  
        .Cells(3, 2).Font.Bold = True
        .Cells(3, 3).FormulaR1C1 = "2"
        .Cells(3, 4).FormulaR1C1 = "6"
        .Cells(3, 5).FormulaR1C1 = "8"
        .Cells(3, 7).FormulaR1C1 = "K" & ChrW(7871) & "t qu" & ChrW(7843)
        .Cells(3, 7).Font.Bold = True
        .Cells(4, 1).FormulaR1C1 = "4000"
        .Cells(4, 2).FormulaR1C1 = "91"
        .Cells(4, 3).FormulaR1C1 = "0"
        .Cells(4, 4).FormulaR1C1 = "1"
        .Cells(4, 5).FormulaR1C1 = "0"
        .Cells(4, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-3]C[-4]:R[-3]C[-2]*R[-1]C[-4]:R[-1]C[-2]*RC[-4]:RC[-2]/R[-2]C[-4]:R[-2]C[-2])"
        .Cells(5, 1).FormulaR1C1 = "2000"
        .Cells(5, 2).FormulaR1C1 = "127"
        .Cells(5, 3).FormulaR1C1 = "1"
        .Cells(5, 4).FormulaR1C1 = "0"
        .Cells(5, 5).FormulaR1C1 = "0"
        .Cells(5, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-4]C[-4]:R[-4]C[-2]*R[-2]C[-4]:R[-2]C[-2]*RC[-4]:RC[-2]/R[-3]C[-4]:R[-3]C[-2])"
        .Cells(6, 1).FormulaR1C1 = "25000"
        .Cells(6, 2).FormulaR1C1 = "153"
        .Cells(6, 3).FormulaR1C1 = "2"
        .Cells(6, 4).FormulaR1C1 = "0"
        .Cells(6, 5).FormulaR1C1 = "2"
        .Cells(6, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-5]C[-4]:R[-5]C[-2]*R[-3]C[-4]:R[-3]C[-2]*RC[-4]:RC[-2]/R[-4]C[-4]:R[-4]C[-2])"
        .Cells(7, 1).FormulaR1C1 = "5200"
        .Cells(7, 2).FormulaR1C1 = "173"
        .Cells(7, 3).FormulaR1C1 = "2"
        .Cells(7, 4).FormulaR1C1 = "0"
        .Cells(7, 5).FormulaR1C1 = "0"
        .Cells(7, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-6]C[-4]:R[-6]C[-2]*R[-4]C[-4]:R[-4]C[-2]*RC[-4]:RC[-2]/R[-5]C[-4]:R[-5]C[-2])"
        .Cells(8, 1).FormulaR1C1 = "7200"
        .Cells(8, 2).FormulaR1C1 = "233"
        .Cells(8, 3).FormulaR1C1 = "2"
        .Cells(8, 4).FormulaR1C1 = "0"
        .Cells(8, 5).FormulaR1C1 = "0"
        .Cells(8, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-7]C[-4]:R[-7]C[-2]*R[-5]C[-4]:R[-5]C[-2]*RC[-4]:RC[-2]/R[-6]C[-4]:R[-6]C[-2])"
        .Cells(9, 1).FormulaR1C1 = "42000"
        .Cells(9, 2).FormulaR1C1 = "277"
        .Cells(9, 3).FormulaR1C1 = "0"
        .Cells(9, 4).FormulaR1C1 = "2"
        .Cells(9, 5).FormulaR1C1 = "1"
        .Cells(9, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-8]C[-4]:R[-8]C[-2]*R[-6]C[-4]:R[-6]C[-2]*RC[-4]:RC[-2]/R[-7]C[-4]:R[-7]C[-2])"
        .Cells(11, 2).FormulaR1C1 = "Thua"
        .Cells(11, 2).Font.Bold = True
        .Cells(11, 3).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-1]:R[-2]C[-1]*R[-7]C:R[-2]C)"
        .Cells(11, 4).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-2]:R[-2]C[-2]*R[-7]C:R[-2]C)"
        .Cells(11, 5).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-3]:R[-2]C[-3]*R[-7]C:R[-2]C)"
        .Columns.AutoFit
        .Rows.AutoFit
        .Columns(6).EntireColumn.ColumnWidth = 3
    End With
    Des.Offset(, 2).Resize(, 3).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2, 6).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = False

End Sub
Cám ơn Bạn đã giúp đỡ nhé.
Code chạy Ok rồi.
Bạn bị sai ở tất cả các lệnh cells(..., ...), do lệnh cells nó hiểu là sheet hiện tại. Để khắc phục bạn thử sửa lại code thế này.
Mã:
Sub MoHinhMau()
Dim DesTemp As Range, Des As Range

On Error Resume Next
    Set DesTemp = Application.InputBox("Ch" & ChrW(7885) & "n ô t" & ChrW(7841) & _
        "o mô hình m" & ChrW(7851) & "u", "Mô hình m" & ChrW(7851) & _
        "u", Default:=ActiveWindow.ActiveCell.Address, Type:=8)

If Err.Number <> 0 Then Exit Sub

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

If IsArray(DesTemp) = True Then
   Set Des = DesTemp.Cells(1, 1)
Else
    Set Des = DesTemp
End If

With Des.Resize(11, 7)
    .Clear
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
End With
    With Des.Resize(9, 5)

        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
   
    With Des.Offset(10, 1).Resize(, 4)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
    With Des.Offset(2, 6).Resize(7)
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Interior.Color = 65535
    End With
   
    With Des.Offset(3, 2).Resize(6, 3)
        .Borders(xlEdgeLeft).Weight = xlMedium
        .Borders(xlEdgeRight).Weight = xlMedium
        .Borders(xlEdgeTop).Weight = xlMedium
        .Borders(xlEdgeBottom).Weight = xlMedium
        .Borders(xlInsideVertical).Weight = xlMedium
        .Borders(xlInsideHorizontal).Weight = xlMedium
        .Interior.Color = 65280
    End With
   
    With Des.Resize(2, 1)
        .MergeCells = True
        .FormulaR1C1 = "Day"
        .Font.Bold = True
    End With
    With Des.Resize(11, 7)
        .Cells(1, 2).FormulaR1C1 = "L" & ChrW(432) & ChrW(7907) & "ng (Kg)"
        .Cells(1, 2).Font.Bold = True
        .Cells(1, 3).FormulaR1C1 = "10000"
        .Cells(1, 4).FormulaR1C1 = "4900"
        .Cells(1, 5).FormulaR1C1 = "4900"
        .Cells(2, 2).FormulaR1C1 = "Kho"
        .Cells(2, 2).Font.Bold = True
        .Cells(2, 3).FormulaR1C1 = "1250"
        .Cells(2, 4).FormulaR1C1 = "652"
        .Cells(2, 5).FormulaR1C1 = "590"
        .Cells(3, 1).FormulaR1C1 = "Yêu C" & ChrW(7847) & "u" & Chr(10) & "(Kg)"
        .Cells(3, 1).Font.Bold = True
        .Cells(3, 2).FormulaR1C1 = "So luong" & Chr(10) & "-----------" & "Tip"
   
        .Cells(3, 2).Font.Bold = True
        .Cells(3, 3).FormulaR1C1 = "2"
        .Cells(3, 4).FormulaR1C1 = "6"
        .Cells(3, 5).FormulaR1C1 = "8"
        .Cells(3, 7).FormulaR1C1 = "K" & ChrW(7871) & "t qu" & ChrW(7843)
        .Cells(3, 7).Font.Bold = True
        .Cells(4, 1).FormulaR1C1 = "4000"
        .Cells(4, 2).FormulaR1C1 = "91"
        .Cells(4, 3).FormulaR1C1 = "0"
        .Cells(4, 4).FormulaR1C1 = "1"
        .Cells(4, 5).FormulaR1C1 = "0"
        .Cells(4, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-3]C[-4]:R[-3]C[-2]*R[-1]C[-4]:R[-1]C[-2]*RC[-4]:RC[-2]/R[-2]C[-4]:R[-2]C[-2])"
        .Cells(5, 1).FormulaR1C1 = "2000"
        .Cells(5, 2).FormulaR1C1 = "127"
        .Cells(5, 3).FormulaR1C1 = "1"
        .Cells(5, 4).FormulaR1C1 = "0"
        .Cells(5, 5).FormulaR1C1 = "0"
        .Cells(5, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-4]C[-4]:R[-4]C[-2]*R[-2]C[-4]:R[-2]C[-2]*RC[-4]:RC[-2]/R[-3]C[-4]:R[-3]C[-2])"
        .Cells(6, 1).FormulaR1C1 = "25000"
        .Cells(6, 2).FormulaR1C1 = "153"
        .Cells(6, 3).FormulaR1C1 = "2"
        .Cells(6, 4).FormulaR1C1 = "0"
        .Cells(6, 5).FormulaR1C1 = "2"
        .Cells(6, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-5]C[-4]:R[-5]C[-2]*R[-3]C[-4]:R[-3]C[-2]*RC[-4]:RC[-2]/R[-4]C[-4]:R[-4]C[-2])"
        .Cells(7, 1).FormulaR1C1 = "5200"
        .Cells(7, 2).FormulaR1C1 = "173"
        .Cells(7, 3).FormulaR1C1 = "2"
        .Cells(7, 4).FormulaR1C1 = "0"
        .Cells(7, 5).FormulaR1C1 = "0"
        .Cells(7, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-6]C[-4]:R[-6]C[-2]*R[-4]C[-4]:R[-4]C[-2]*RC[-4]:RC[-2]/R[-5]C[-4]:R[-5]C[-2])"
        .Cells(8, 1).FormulaR1C1 = "7200"
        .Cells(8, 2).FormulaR1C1 = "233"
        .Cells(8, 3).FormulaR1C1 = "2"
        .Cells(8, 4).FormulaR1C1 = "0"
        .Cells(8, 5).FormulaR1C1 = "0"
        .Cells(8, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-7]C[-4]:R[-7]C[-2]*R[-5]C[-4]:R[-5]C[-2]*RC[-4]:RC[-2]/R[-6]C[-4]:R[-6]C[-2])"
        .Cells(9, 1).FormulaR1C1 = "42000"
        .Cells(9, 2).FormulaR1C1 = "277"
        .Cells(9, 3).FormulaR1C1 = "0"
        .Cells(9, 4).FormulaR1C1 = "2"
        .Cells(9, 5).FormulaR1C1 = "1"
        .Cells(9, 7).FormulaR1C1 = "=RC[-5]*SUMPRODUCT(R[-8]C[-4]:R[-8]C[-2]*R[-6]C[-4]:R[-6]C[-2]*RC[-4]:RC[-2]/R[-7]C[-4]:R[-7]C[-2])"
        .Cells(11, 2).FormulaR1C1 = "Thua"
        .Cells(11, 2).Font.Bold = True
        .Cells(11, 3).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-1]:R[-2]C[-1]*R[-7]C:R[-2]C)"
        .Cells(11, 4).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-2]:R[-2]C[-2]*R[-7]C:R[-2]C)"
        .Cells(11, 5).FormulaR1C1 = "=R[-9]C-SUMPRODUCT(R[-7]C[-3]:R[-2]C[-3]*R[-7]C:R[-2]C)"
        .Columns.AutoFit
        .Rows.AutoFit
        .Columns(6).EntireColumn.ColumnWidth = 3
    End With
    Des.Offset(, 2).Resize(, 3).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Des.Offset(2, 6).Resize(6).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = False

End Sub
Cảm ơn bạn đã giúp đỡ
Code chạy Ok rồi.
 
Upvote 0
Bạn biết mục đích code ấy nó làm cái gì hôn? (không đi theo cái file áp dụng thì mục đích rỗng tuếch)
Người viết code lúc ấy chỉ có 1 mục đích chính trong đầu: từ dữ kiện đầu vào như thế, đạt yêu cầu đầu ra như này, với tốc độ nhanh nhất. Hết.
Nó không phải là loại code viết để người khác tìm hiểu và học hỏi.
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom