You are on page 1of 16

Private Sub BuatLaporan_Click()

'Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim lngRow As Long, lngRow2 As Long, lngRow3 As Long, counter As Long,


lngRows As Long
Dim l As Long, m As Long
Dim unik As Double
lngRow = Worksheets("Real").Cells(65556, 1).End(xlUp).Row
lngRow2 = Worksheets("Analisa").Cells(65556, 1).End(xlUp).Row
lngRow3 = Worksheets("OA").Cells(65556, 1).End(xlUp).Row

For i = 2 To lngRow
Worksheets("Real").Cells(i, 63).value = Worksheets("Real").Cells(i,
17).value & Worksheets("Real").Cells(i, 62).value
Next i
For i = 2 To lngRow2
Worksheets("Analisa").Cells(i, 32).value = Worksheets("Analisa").Cells(i,
5).value & Worksheets("Analisa").Cells(i, 9).value
Next i
For i = 2 To lngRow3
Worksheets("OA").Cells(i, 59).value = Worksheets("OA").Cells(i, 4).value &
Worksheets("OA").Cells(i, 5).value
Next i

Sheets.Add.Name = "Raw"
'==========PASANG DATA========
Worksheets("Raw").Cells(1, 1).value = "Plant"

Worksheets("Raw").Cells(1, 2).value = "Provinsi"


Worksheets("Raw").Cells(1, 3).value = "Unit"
Worksheets("Raw").Cells(1, 4).value = "NoSO&LineSO"
Worksheets("Raw").Cells(1, 5).value = "NoDO"
Worksheets("Raw").Cells(1, 6).value = "Tonnage"
Worksheets("Raw").Cells(1, 7).value = "ZPR0/unit"
Worksheets("Raw").Cells(1, 8).value = "ShpCstNet"
Worksheets("Raw").Cells(1, 9).value = "Revenue"
For i = 2 To lngRow
l=0
l = Application.WorksheetFunction.Match(Worksheets("Real").Cells(i,
63).value, Worksheets("Analisa").Columns(32), 0)
m = Application.WorksheetFunction.Match(Worksheets("Real").Cells(i,
18).value, Worksheets("OA").Columns(2), 0)
On Error GoTo Handler
Worksheets("Raw").Cells(i, 1).value = Worksheets("Real").Cells(i,
2).value
Worksheets("Raw").Cells(i, 2).value = Worksheets("Real").Cells(i,
14).value
Worksheets("Raw").Cells(i, 3).value = Worksheets("Real").Cells(i,
21).value
Worksheets("Raw").Cells(i, 4).value = Worksheets("Real").Cells(i,
63).value
Worksheets("Raw").Cells(i, 5).value = Worksheets("Real").Cells(i,
18).value
Worksheets("Raw").Cells(i, 6).value = Worksheets("Real").Cells(i,
22).value
Worksheets("Raw").Cells(i, 7).value = Worksheets("Analisa").Cells(l,
24).value / 1000
Worksheets("Raw").Cells(i, 8).value = Worksheets("OA").Cells(i,

53).value
Worksheets("Raw").Cells(i, 9).value = (Worksheets("Raw").Cells(i,
7).value * Worksheets("Raw").Cells(i, 6).value) - Worksheets("Raw").Cells(i,
8).value
Handler:
'Worksheets("Main").Rows(i).Delete
Resume Handler2
Handler2:
Next i

'===========HAPUS YG KOSONG===========
' Input box to determine the total number of rows in the worksheet.
counter = Worksheets("Raw").Cells(65556, 1).End(xlUp).Row
ActiveCell.Select
' Loops through the desired number of rows.
For i = 1 To counter
' Checks to see if the active cell is blank.
If ActiveCell = vbNullString Then
Selection.EntireRow.Delete

' Decrements count each time a row is deleted. This ensures


' that the macro will not run past the last row.
counter = counter - 1
Else
' Selects the next cell.
ActiveCell.Offset(1, 0).Select

End If
Next i

End Sub
Private Sub ComboBox1_Change()
Dim libro As Worksheet
Dim combo As ComboBox
Dim lngRows As Long
Dim rngMerge As Range, cell As Range
Dim lngRows2 As Long
Dim oRange As Excel.Range
Dim oTarget As Excel.Range
Dim oRow As Excel.Range
Dim oRowAmend As Excel.Range
Dim oDic As Scripting.Dictionary
Dim sIndex As String
Dim vKey As Variant
Dim vItem As Variant
Dim data As Variant
Dim a As Long
Dim countDict As Variant
Dim countDict2 As Variant
Dim countDict3 As Variant
Dim countDict4 As Variant
Dim countDictPivot As Variant

Dim countDictUnit As Variant


Dim category As Variant
Dim value As Variant
Dim value2 As Variant
Dim value3 As Variant
Dim value4 As Variant
Dim d As Variant

Set libro = ActiveWorkbook.Sheets("Main")


'Set combo = libro.Shapes("ComboBox1")

With ComboBox1 ' assuming the name of your control is "ComboBox1"

'Application.EnableEvents = True
choice = Me.ComboBox1.value
Select Case choice

'================================CHOICE
PLANT===================================
Case Is = "Plant"
Application.DisplayAlerts = False
'If .Text = "Plant" Then
Sheets.Add.Name = "Laporan Plant"
lngRows = Worksheets("Raw").Cells(65556, 1).End(xlUp).Row
For i = 1 To lngRows

Worksheets("Laporan Plant").Cells(i, 1).value =


Worksheets("Raw").Cells(i, 2).value
Worksheets("Laporan Plant").Cells(i, 2).value =
Worksheets("Raw").Cells(i, 3).value
Worksheets("Laporan Plant").Cells(i, 3).value =
Worksheets("Raw").Cells(i, 6).value
Worksheets("Laporan Plant").Cells(i, 4).value =
Worksheets("Raw").Cells(i, 7).value
Worksheets("Laporan Plant").Cells(i, 5).value =
Worksheets("Raw").Cells(i, 8).value
Worksheets("Laporan Plant").Cells(i, 6).value =
Worksheets("Raw").Cells(i, 9).value
'Worksheets("Laporan Plant").Cells(i, 7).Value =
Worksheets("Mudah").Cells(i, 7).Value
'Worksheets("Laporan Plant").Cells(i, 8).Value =
Worksheets("Mudah").Cells(i, 8).Value
Next i

'==============URUTKAN YG SETIPE=============
'Worksheets("Main").Cells(3).CurrentRegion.Sort
key1:=Worksheets("Raw").Cells(3), Header:=xlYes 'change this to xlYes if
your table has header cells
Worksheets("Laporan Plant").Cells(2).CurrentRegion.Sort
key1:=Worksheets("Laporan Plant").Cells(2), Header:=xlYes 'change this to
xlYes if your table has header cells
Worksheets("Laporan Plant").Cells(1).CurrentRegion.Sort
key1:=Worksheets("Laporan Plant").Cells(1), Header:=xlYes 'change this to
xlYes if your table has header cells

'==============TAMBAH VALUE YG SETIPE=============


With Worksheets("Laporan Plant")

Set countDict = CreateObject("Scripting.Dictionary")


Set countDict2 = CreateObject("Scripting.Dictionary")
Set countDict3 = CreateObject("Scripting.Dictionary")
Set countDict4 = CreateObject("Scripting.Dictionary")
Set countDictPivot = CreateObject("Scripting.Dictionary")
Set countDictUnit = CreateObject("Scripting.Dictionary")

data = ActiveSheet.UsedRange 'Assumes data is in columns A/B

'Populate the dictionary: key = category / Item = count


For a = LBound(data, 1) To UBound(data, 1)
category = Trim(data(a, 1)) & Trim(data(a, 2))
value = data(a, 3)
value2 = data(a, 4)
value3 = data(a, 5)
value4 = data(a, 6)
If countDict.Exists(category) Then
countDict(category) = countDict(category) + value 'if we have already
seen that category, add to the total
countDict2(category) = countDict2(category) + value2 'if we have
already seen that category, add to the total
countDict3(category) = countDict3(category) + value3 'if we have
already seen that category, add to the total
countDict4(category) = countDict4(category) + value4 'if we have
already seen that category, add to the total
Else
countDictPivot(category) = data(a, 1)

countDictUnit(category) = data(a, 2)
countDict(category) = value 'first time we find that category, create it
countDict2(category) = value2 'first time we find that category, create
it
countDict3(category) = value3 'first time we find that category, create
it
countDict4(category) = value4 'first time we find that category, create
it
End If
Next a

'Copy dictionary into an array


ReDim data(1 To countDict.Count, 1 To 6) As Variant
ReDim data(1 To countDict2.Count, 1 To 6) As Variant
ReDim data(1 To countDict3.Count, 1 To 6) As Variant
ReDim data(1 To countDict4.Count, 1 To 6) As Variant
ReDim data(1 To countDictPivot.Count, 1 To 6) As Variant
ReDim data(1 To countDictUnit.Count, 1 To 6) As Variant

a=1
For Each d In countDict
data(a, 1) = countDictPivot(d)
data(a, 2) = countDictUnit(d)
data(a, 3) = countDict(d)
data(a, 4) = countDict2(d)
data(a, 5) = countDict3(d)
data(a, 6) = countDict4(d)

a=a+1
Next d

'Puts the result back in the sheet in column D/E, including headers
With ActiveSheet
.Range("K1").Resize(UBound(data, 1), UBound(data, 2)) = data
End With
.Range("K1:P1").value = .Range("A1:F1").value
.Columns("A:J").EntireColumn.Delete
End With
'===============MERGE AND CENTER PIVOT===========

lngRows2 = Worksheets("Laporan Plant").Cells(65556, 1).End(xlUp).Row


Set rngMerge = Worksheets("Laporan Plant").Range((Worksheets("Laporan
Plant").Cells(1, 1)), (Worksheets("Laporan Plant").Cells(lngRows2, 1)))
'Set rngMerge2 = Range((Worksheets("Main").Cells(1, 2)),
(Worksheets("Main").Cells(lngRows, 2)))

Range("A1:F1").Interior.ColorIndex = 24

rngMerge.HorizontalAlignment = xlCenter
'rngMerge2.HorizontalAlignment = xlCenter
rngMerge.VerticalAlignment = xlCenter
'rngMerge2.VerticalAlignment = xlCenter

MergeAgainPlant:

For Each cell In rngMerge

If cell.value = cell.Offset(1, 0).value And IsEmpty(cell) = False Then


Worksheets("Laporan Plant").Range(cell, cell.Offset(1, 0)).Merge
Worksheets("Laporan Plant").Range(cell, cell.Offset(1,
0)).HorizontalAlignment = xlCenter
Worksheets("Laporan Plant").Range(cell, cell.Offset(1,
0)).VerticalAlignment = xlCenter

GoTo MergeAgainPlant
End If
Next

Application.DisplayAlerts = True
'Application.ScreenUpdating = True

'===============================CHOICE
PROVINSI===============================
Case Is = "Provinsi"
Application.DisplayAlerts = False
'If .Text = "Provinsi" Then
Sheets.Add.Name = "Laporan Provinsi"
lngRows = Worksheets("Raw").Cells(65556, 1).End(xlUp).Row
For i = 1 To lngRows

Worksheets("Laporan Provinsi").Cells(i, 1).value =


Worksheets("Raw").Cells(i, 2).value
Worksheets("Laporan Provinsi").Cells(i, 2).value =
Worksheets("Raw").Cells(i, 3).value
Worksheets("Laporan Provinsi").Cells(i, 3).value =
Worksheets("Raw").Cells(i, 6).value
Worksheets("Laporan Provinsi").Cells(i, 4).value =
Worksheets("Raw").Cells(i, 7).value
Worksheets("Laporan Provinsi").Cells(i, 5).value =
Worksheets("Raw").Cells(i, 8).value
Worksheets("Laporan Provinsi").Cells(i, 6).value =
Worksheets("Raw").Cells(i, 9).value
'Worksheets("Laporan Provinsi").Cells(i, 7).Value =
Worksheets("Mudah").Cells(i, 7).Value
'Worksheets("Laporan Provinsi").Cells(i, 8).Value =
Worksheets("Mudah").Cells(i, 8).Value
Next i

'==============URUTKAN YG SETIPE=============
'Worksheets("Main").Cells(3).CurrentRegion.Sort
key1:=Worksheets("Raw").Cells(3), Header:=xlYes 'change this to xlYes if
your table has header cells
Worksheets("Laporan Provinsi").Cells(2).CurrentRegion.Sort
key1:=Worksheets("Laporan Provinsi").Cells(2), Header:=xlYes 'change this to
xlYes if your table has header cells
Worksheets("Laporan Provinsi").Cells(1).CurrentRegion.Sort
key1:=Worksheets("Laporan Provinsi").Cells(1), Header:=xlYes 'change this to
xlYes if your table has header cells

'==============TAMBAH VALUE YG SETIPE=============


With Worksheets("Laporan Provinsi")

Set countDict = CreateObject("Scripting.Dictionary")


Set countDict2 = CreateObject("Scripting.Dictionary")
Set countDict3 = CreateObject("Scripting.Dictionary")
Set countDict4 = CreateObject("Scripting.Dictionary")
Set countDictPivot = CreateObject("Scripting.Dictionary")
Set countDictUnit = CreateObject("Scripting.Dictionary")

data = ActiveSheet.UsedRange 'Assumes data is in columns A/B

'Populate the dictionary: key = category / Item = count


For a = LBound(data, 1) To UBound(data, 1)
category = Trim(data(a, 1)) & Trim(data(a, 2))
value = data(a, 3)
value2 = data(a, 4)
value3 = data(a, 5)
value4 = data(a, 6)
If countDict.Exists(category) Then
countDict(category) = countDict(category) + value 'if we have already
seen that category, add to the total
countDict2(category) = countDict2(category) + value2 'if we have
already seen that category, add to the total
countDict3(category) = countDict3(category) + value3 'if we have
already seen that category, add to the total
countDict4(category) = countDict4(category) + value4 'if we have
already seen that category, add to the total
Else
countDictPivot(category) = data(a, 1)

countDictUnit(category) = data(a, 2)
countDict(category) = value 'first time we find that category, create it
countDict2(category) = value2 'first time we find that category, create
it
countDict3(category) = value3 'first time we find that category, create
it
countDict4(category) = value4 'first time we find that category, create
it
End If
Next a

'Copy dictionary into an array


ReDim data(1 To countDict.Count, 1 To 6) As Variant
ReDim data(1 To countDict2.Count, 1 To 6) As Variant
ReDim data(1 To countDict3.Count, 1 To 6) As Variant
ReDim data(1 To countDict4.Count, 1 To 6) As Variant
ReDim data(1 To countDictPivot.Count, 1 To 6) As Variant
ReDim data(1 To countDictUnit.Count, 1 To 6) As Variant

a=1
For Each d In countDict
data(a, 1) = countDictPivot(d)
data(a, 2) = countDictUnit(d)
data(a, 3) = countDict(d)
data(a, 4) = countDict2(d)
data(a, 5) = countDict3(d)
data(a, 6) = countDict4(d)

a=a+1
Next d

'Puts the result back in the sheet in column D/E, including headers
With ActiveSheet
.Range("K1").Resize(UBound(data, 1), UBound(data, 2)) = data
End With
.Range("K1:P1").value = .Range("A1:F1").value
.Columns("A:J").EntireColumn.Delete
End With
'===============MERGE AND CENTER PIVOT===========

lngRows2 = Worksheets("Laporan Provinsi").Cells(65556, 1).End(xlUp).Row


Set rngMerge = Worksheets("Laporan
Provinsi").Range((Worksheets("Laporan Provinsi").Cells(1, 1)),
(Worksheets("Laporan Provinsi").Cells(lngRows2, 1)))
'Set rngMerge2 = Range((Worksheets("Main").Cells(1, 2)),
(Worksheets("Main").Cells(lngRows, 2)))

Range("A1:F1").Interior.ColorIndex = 24

rngMerge.HorizontalAlignment = xlCenter
'rngMerge2.HorizontalAlignment = xlCenter
rngMerge.VerticalAlignment = xlCenter
'rngMerge2.VerticalAlignment = xlCenter

MergeAgainProvinsi:

For Each cell In rngMerge

If cell.value = cell.Offset(1, 0).value And IsEmpty(cell) = False Then


Worksheets("Laporan Provinsi").Range(cell, cell.Offset(1, 0)).Merge
Worksheets("Laporan Provinsi").Range(cell, cell.Offset(1,
0)).HorizontalAlignment = xlCenter
Worksheets("Laporan Provinsi").Range(cell, cell.Offset(1,
0)).VerticalAlignment = xlCenter

GoTo MergeAgainProvinsi
End If
Next

Application.DisplayAlerts = True
'Application.ScreenUpdating = True

'Application.ScreenUpdating = True
End Select
End With
End Sub
Private Sub CommandButton1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook

Dim myFilter As String

myFilter = "Excel Workbooks (*.xls; *.xlsx;*.xlsm),*.xls;*.xlsx;*.xslm"

Set wb1 = ActiveWorkbook

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Workbook to Parse", _
FileFilter:=myFilter)

If FileToOpen = False Then


MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)

For Each Sheet In wb2.Sheets


If Sheet.Visible = True Then
Sheet.Copy After:=wb1.Sheets(wb1.Sheets.Count)
End If
Next Sheet
wb2.Close
End If

End Sub

You might also like