Lỗi 13 là lỗi do một biến không khớp với kiểu dữ liệu của biến đó, nhưng mình chạy thử vài lần rồi, không thấy báo lỗi gì cả. Kết quả như trong hình bài #2 đó bạn.
Private Sub Worksheet_Activate()
On Error Resume Next
'dat ten cho mang
Lr = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
ThisWorkbook.Names.Add Name:="data_SME", RefersTo:=Sheet1.Range("A2:AQ" & Lr)
PivotTables(1).PivotCache.Refresh
Dim pivotTable As pivotTable
Dim Range As Range, newRange As Range
Set Range = Sheet1.Range("A2:AQ10")
'Set pivotTable = Sheets("pivot").PivotTables("PivotTable1")
Set pivotTable = Sheet2.PivotTables("PivotTable1")
' Change the name of the sheets according to your sheet name for both newRange and pivot table
pivotTable.ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range)
Set newRange = Sheet1.Range("data_SME")
' Change the name of the sheets according to your sheet name for both newRange and pivot table
pivotTable.ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=newRange)
End Sub
Anh coi dùm em code này với.
Nếu xoá dòng (xoá bớt dữ liệu) thì code chạy đúng, còn thêm dòng (thêm dữ liệu) thì code giữ nguyên không chọn thêm dòng mới.
(Anh có thể test thử).
Em cảm ơn anh. nha.