thầy giáo xem giúp em về bài tập thực hành gộp file bảng lương này với ạ, tại sao khi e chạy code thì nó toàn báo lỗi này
code đây ạ:
Sub tonghop_dulieu()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Show
Dim i As Long
For i = 1 To .SelectedItems.Count
Dim wb_kq As Workbook
Dim wb_dl As Workbook
Set wb_kq = ThisWorkbook
Set wb_dl = Workbooks.Open(.SelectedItems(i))
Dim lr_kq As Long
lr_kq = wb_kq.Sheet6.Range("A" & Rows.Count).End(xlUp).Row
Dim lr_dl As Long
lr_dl = wb_dl.Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Dim fr_dl As Long
fr_dl = 8
Dim kc As Long
kc = lr_dl - fr_dl + 1
wb_kq.Sheet6.Range("A" & lr_kq + 1 & ":BM" & lr_kq + kc).Value = _
wb_dl.Sheet1.Range("A" & fr_dl & ":BM" & lr_dl).Value
wb_dl.Close SaveChanges:=False
Next i
End With
End Sub
Bạn để ý code ở đoạn này:
lr_kq = wb_kq.Sheet6.Range("A" & Rows.Count).End(xlUp).Row
Với cách viết có bao gồm cả workbook thì đối tượng sheet bắt buộc phải viết dưới dạng Worksheets("tên sheet") chứ không viết dưới dạng sheet code được.
Các đoạn code khác bạn cũng chú ý yếu tố này nhé.
à vâng, với việc có thể mình sẽ không nhớ chính xác sheet name nên ban đầu e định viết theo cách trên, nhưng sau e đã chuyển sang viết theo cách gọi số thứ tự của sheet để dễ dàng hơn.
Đúng rồi, bạn có thể viết theo tên hoặc theo số thứ tự. Thường khi lập trình lấy dữ liệu từ wb khác thì hay dùng cách xác định sheet theo thứ tự.
Quan trọng là ko viết theo sheet code, bởi sheet code chỉ áp dụng với 1 wb duy nhất thì đúng, còn nhiều wb thì sẽ lỗi (cơ chế của VBA thôi)
Hi thầy em muốn attach file vào mail outlook . Nhưng trong trường hợp chỉ có 1 file tồn tại thì code vẫn hiểu và attach vào file .Thầy giải giúp em nha thầy . Code bên dưới phải có đủ data mới run được .
Sub Noi_Dung()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim sName1 As String
Dim sName2 As String
sName1 = Sheets("Form bao cao chuyen tai").Range("C4").Value
sName2 = Sheets("Form bao cao chuyen tai").Range("C5").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
If Sheet9.Range("B2").Value <> "" Then
Else
MsgBox " Khong co data "
Exit Sub
End If
Dim MailBody As String
Dim DongCuoi As Long
DongCuoi = Sheet9.Range("B" & Rows.Count).End(xlUp).Row
Set rng = Nothing
On Error Resume Next
Set rng = Sheet9.Range("A1:L" & DongCuoi).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
MailBody = "Dear team!" & vbNewLine & _
"(Pls xem file dinh kem )"
With OutMail
.To = "hau.nt@pg.com;bac.dt@pg.com;hai.nl@pg.com;dang.nt@pg.com;nhat.th@pg.com; giaohangpg@alcvietnam.com>;hai.nt@pg.com;nhanh.lv@pg.com;logistics@macsvietnam.com.vn; thien.nd.2@pg.com;yen.bq@pg.com;hai.nq@pg.com; anh.lx@pg.com;hai.nt@pg.com;tuyet.ht@pg.com;nguyen.pt.2@pg.com;hoang.nv@pg.com"
.CC = "dang.nt@pg.com"
.BCC = ""
.Subject = "Tally sheet done " & Sheet4.Range("B1").Text
On Error Resume Next
.Attachments.Add ThisWorkbook.Path & "\" & sName1 & ".xlsm"
.Attachments.Add ThisWorkbook.Path & "\" & sName2 & ".xlsm"
.HTMLBody = MailBody & RangetoHTML(rng) & "Thank you "
.Display
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub