Private Sub CommandButton1_Click()
Dim Sh As Worksheet
Dim fR As Long, sR As Long
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
Cells(5, "M").Resize(, 5).EntireColumn.Hidden = False
Cells(5, "M").Resize(, 5).EntireRow.Hidden = False
[G1] = Timer
sR = [M65535].End(xlUp).Row - 4
Cells(6, "M").Resize(sR, 5).ClearContents
For Each Sh In Worksheets
If Left(Sh.Name, 1) = "N" And Sh.[a17] <> blank Then
fR = [M65535].End(xlUp).Row + 1
sR = Sh.[C65535].End(xlUp).Row - 16
Cells(fR, "M").Resize(sR) = "Nhap"
Cells(fR, "N").Resize(sR, 4) = Sh.[a17].Resize(sR, 4).Value
Else
If Left(Sh.Name, 1) = "X" And Sh.[a17] <> blank Then
fR = [M65535].End(xlUp).Row + 1
sR = Sh.[D65535].End(xlUp).Row - 16
Cells(fR, "M").Resize(sR) = "Xuat"
Cells(fR, "N").Resize(sR, 3) = Sh.[B17].Resize(sR, 3).Value
Tmp = Cells(fR, "N").Resize(sR, 1).Offset(, 3)
For iR = 1 To sR
Tmp(iR, 1) = (-1) * Sh.[B17].Offset(iR - 1, 3).Value
Next
Cells(fR, "N").Resize(sR, 1).Offset(, 3) = Tmp
End If
End If
Next
sR = [M65535].End(xlUp).Row - 3
Cells(5, "M").Resize(sR, 5).Sort Key1:=[P6], Order1:=1, Header:=1
fR = [P65535].End(xlUp).Row + 1: sR = [M65535].End(xlUp).Row - fR + 1
Cells(fR, "M").Resize(sR, 5).Clear
Cells(5, "M").Resize(fR, 5).Sort Key1:=[N6], Order1:=1, Header:=1
If ActiveSheet.PivotTables.Count > 0 Then
Refresh_Pivot
Else: Pivot: End If
[G2] = Timer: [G3] = [G2] - [G1]
Cells(5, "M").Resize(, 5).EntireColumn.Hidden = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Private Sub Pivot()
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
[G1] = Timer
Range([a3], [a3].End(xlDown)).Resize(, 6).Clear
'-----------------------------------------------------------------------------
[m5].Resize([m5].End(xlDown).Row - 4, 5).Name = "Tmp": [a3].Name = "Start"
ActiveWorkbook.PivotCaches.Add(1, "Tmp").CreatePivotTable "Start", "GPE_boyxin"
With ActiveSheet.PivotTables("GPE_boyxin")
.PivotFields([m5].Value).Orientation = xlColumnField
.PivotFields([n5].Value).Orientation = xlRowField
.PivotFields([n5].Value).Subtotals = Array(False, False, False, _
False, False, False, False, False, False, False, False, False)
.PivotFields([o5].Value).Orientation = xlRowField
.PivotFields([o5].Value).Subtotals = Array(False, False, False, _
False, False, False, False, False, False, False, False, False)
.PivotFields([p5].Value).Orientation = xlRowField
.AddDataField .PivotFields([q5].Value), "Sum " & [q5].Value, xlSum
End With
'-----------------------------------------------------------------------------
Columns("A:A").NumberFormat = "0000000000000"
Columns("D:F").NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)"
With Range([a3], [a3].End(xlDown)).Resize(, 6)
.Font.Name = "VNI-Helve"
.Font.Size = 10
.EntireColumn.AutoFit
End With
[G2] = Timer: [G3] = [G2] - [G1]
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Private Sub Refresh_Pivot()
ActiveSheet.PivotTables("GPE_boyxin").PivotCache.Refresh
Columns("A:A").NumberFormat = "0000000000000"
Columns("D:F").NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)"
With Range([a3], [a3].End(xlDown)).Resize(, 6)
.Font.Name = "VNI-Helve"
.Font.Size = 10
.EntireColumn.AutoFit
End With
End Sub