Sub MultiFormat()
'
' Keyboard Shortcut: Ctrl+m
'
' This macro will process an exported report with multiple machine data into individual machine reports and save the reports using the MachineID as the file name.
'
' Macro begins now...
' Dimension variables
Dim OrigWS As String
OrigWS = ActiveSheet.Name
' Strip Header Row
Rows("1:1").Select
Selection.Delete Shift:=xlUp
' Parse file
'Based on column A, data is filtered to individual sheets
'Creates sheets and sorts alphabetically in workbook
Dim LR As Long, i As Long, MyArr
Dim MyCount As Long, ws As Worksheet
Application.ScreenUpdating = False
Set ws = Sheets(OrigWS) 'edit to sheet with master data
ws.Activate
Rows(1).Insert xlShiftDown
Range("A1") = "Key"
Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("CC1"), Unique:=True
Columns("CC:CC").Sort Key1:=Range("CC2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
MyArr = Application.WorksheetFunction.Transpose(Range("CC2:CC" & Rows.Count).SpecialCells(xlCellTypeConstants))
Range("CC:CC").Clear
Range("A1").AutoFilter
For i = 1 To UBound(MyArr)
ws.Range("A1").AutoFilter Field:=1, Criteria1:=MyArr(i)
LR = ws.Range("A" & Rows.Count).End(xlUp).Row
If LR > 1 Then
If Not Evaluate("=ISREF('" & MyArr(i) & "'!A1)") Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = MyArr(i)
Else
Sheets(MyArr(i)).Move After:=Sheets(Sheets.Count)
Sheets(MyArr(i)).Cells.Clear
End If
ws.Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
Sheets(MyArr(i)).Range("A1")
ws.Range("A1").AutoFilter Field:=1
MyCount = MyCount + Sheets(MyArr(i)).Range("A" & Rows.Count).End(xlUp).Row - 1
' Store header info, format data
Dim UserID As String
UserID = Range("B2").Value
Dim UserDep As String
UserDep = Range("D2").Value
FormatData
' Compare applicatiosn against lists
Compare
' Update calculations
Application.Calculate
' Delete Core applications (those marked 0)
DelRec
' Sort ascending by column J
SortAscending
' Paste header in new worksheet
ThisWorkbook.Activate
Rows("1:7").Select
Selection.Copy
ws.Activate
Sheets(MyArr(i)).Activate
Range("A1").Select
Selection.Insert Shift:=xlDown
' Paste Section2 Header
' Code Here
' Paste Section3 Header
' Code Here
' Set User and Load
Range("B1").Value = UserID
Range("B3").Value = UserDep
' Set column width to auto
Columns("A:I").Select
Selection.Columns.AutoFit
'Save, title, and close
SaveTitleClose
End If
Next i
' Display success message
ws.Activate
ws.AutoFilterMode = False
LR = ws.Range("A" & Rows.Count).End(xlUp).Row - 1
Rows(1).Delete xlShiftUp
MsgBox "Rows with data: " & LR & vbLf & "Your files have been created! Please check them."
Application.ScreenUpdating = True
' Close the report data file
ActiveWorkbook.Close Saved = True
End Sub
Public Function FormatData()
' Delete unused columns and rows
Columns("A:K").Select
Selection.Delete Shift:=xlToLeft
Columns("G:I").Select
Selection.Delete Shift:=xlToLeft
' Change font size
Cells.Select
With Selection.Font
.Name = "Calibri"
.Size = 10
End With
End Function
Public Function SaveTitleClose()
' Save worksheet as new workbook, set title, and close.
Dim wb As Workbook
Dim Name As String
Name = ActiveSheet.Name
Worksheets(Name).Copy
Set wb = ActiveWorkbook
wb.BuiltinDocumentProperties("title") = Name & " Applications List"
wb.SaveAs FileName:="C:\WKSTEMP\" & Name & ".xlsx", FileFormat:=51
wb.Close
' Close the new worksheet
Application.DisplayAlerts = False
Worksheets(Name).Delete
Application.DisplayAlerts = True
End Function
Public Function Compare()
MaxRowNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
AutoFillRange = "J1:J" & MaxRowNumber
Range("J1").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(R[0]C[-9], [MOCK.xlsm]DELETE!R1C1:R500C1, 1, FALSE)), IF(ISERROR(VLOOKUP(R[0]C[-9], [MOCK.xlsm]SECTION1!R1C1:R500C1, 1, FALSE)), IF(ISERROR(VLOOKUP(R[0]C[-9], [MOCK.xlsm]SECTION2!R1C1:R500C1, 1, FALSE)), 3, 2), 1), 0)"
Range("J1").Select
Selection.AutoFill Destination:=Range(AutoFillRange), Type:=xlFillDefault
End Function
Public Function DelRec()
' This deletes rows containing the value 0 in column J
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the J column in this example
With .Cells(Lrow, "J")
If Not IsError(.Value) Then
If .Value = 0 Then .EntireRow.Delete
'This will delete each row with the Value 0
'in Column J, case sensitive.
End If
End With
Next Lrow
End With
End Function
Public Function SortAscending()
MaxRowNumber = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Range("A1:J" & MaxRowNumber).Sort Key1:=Range("J1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Function
Bookmarks