Moderators: Jason Susnjara, Larry Epplin, Clint Buechlein, Scott G Vaal, Jason Susnjara, Larry Epplin, Clint Buechlein, Scott G Vaal
Code: Select all
Sub CabinetSizeReport()
'=========================================================================================
'The Following Code will extract the Cabinet Sizes from The SheetComponetListing Worksheet
'and put them on a new worksheet named CabinetSizeReport.
'=========================================================================================
'Inform The user about this macro
MsgBox "This Macro will Insert a new WorkSheet Named CabinetSizeReport and extract the cabinet Numbers, Cabinet sizes and Type of Cabinet from the SheetComponetListing worksheet. PLEASE NOTE THAT SOME ASSEMBLIES MAY BE LISTED MULTABLE TIMES ALSO SOME CABINETS OR ASSEMBLIES MAY NOT USE SHEET STOCK IN THEIR CONSTRUCTION THEREFORE THESE CABINETS WILL NOT BE LISTED IN THIS REPORT"
'==============================================================================================
'Loop through the worksheets to check and see if thie CabinetSizeReport worksheet already exist
'==============================================================================================
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
If ws.Name = "CabinetSizeReport" Then
GoTo ImportData
Else: End If
Next
'==============================================
'Add The CabinetSize Worksheet To This workbook
'==============================================
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "CabinetSizeReport"
ImportData:
'Variables
Dim ShtCompLst As Worksheet
Set ShtCompLst = ActiveWorkbook.Sheets("SheetComponentListing")
Dim Sht2 As Worksheet
Set Sht2 = ActiveWorkbook.Sheets("CabinetSizeReport")
'Find The Last Row For Sheet2
If Sht2.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
Lr = 2
Else
Lr = Sht2.Cells(Rows.Count, 1).End(xlUp).Row
End If
'Clear any existing Data From Ws
Sht2.Activate
Sht2.Columns.EntireColumn.ClearContents
'Select all data from The SheetComponentListing Ws To Copy
'Find The Last row For The SheetComponetListing Ws
If ShtCompLst.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
ShtCompLr = 2
Else
ShtCmpLr = ShtCompLst.Cells(Rows.Count, 1).End(xlUp).Row
End If
ShtCompLst.Activate
ShtCompLst.Range("A1:H" & ShtCmpLr).Copy
'Paste The Data To Sheet2
Sht2.Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Delete Unwanted Columns
Sht2.Range("B1:G" & Lr).EntireColumn.Delete
'Remove Duplicates
Columns("A:B").Select
ActiveSheet.Range("A:B").RemoveDuplicates Columns:=2, Header:=xlYes
Range("A2").Select
'TextToColumns (Put Cabinet Number In its Own Column)
Sht2.Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(17, 1)), TrailingMinusNumbers:=True
'Get Rid Of The - In The Cabinet Name Column
If Sht2.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
Lr = 2
Else
Lr = Sht2.Cells(Rows.Count, 1).End(xlUp).Row
End If
For x = 2 To Lr
Cells(x, 2).Activate
ActiveCell.Replace What:="-", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next x
'AutoFit Columns and Rows
Sht2.Columns.AutoFit
Sht2.Rows.AutoFit
Sht2.Range("A1").Select
'==========================================================================================
'Get Rid Of The Cabinet Names and Leave Just The Cabinet Sizes in Column C
'==========================================================================================
If Sht2.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
Lr = 2
Else
Lr = Sht2.Cells(Rows.Count, 1).End(xlUp).Row
End If
For x = 2 To Lr
Pos1 = InStr(1, Sht2.Range("C" & x).Value, " ")
On Error Resume Next
Range("D" & x).Value = Mid(Sht2.Range("C" & x).Value, Pos1 + 3)
'========================================================================
'Filter out the Assemblies
'========================================================================
Pos1 = InStr(1, Sht2.Range("C" & x).Value, "Assembly Cab. No.")
On Error Resume Next
Range("D" & x).Value = Mid(Sht2.Range("C" & x).Value, Pos1 + 0)
Next x
'==========================================================================
'Remove The Assemblies Name
'==========================================================================
For x = 2 To Lr
Pos2 = InStr(1, Sht2.Range("D" & x).Value, " ")
On Error Resume Next
Range("D" & x).Value = Mid(Sht2.Range("D" & x).Value, Pos2 + 0)
Next x
'Delete Column C
Sht2.Range("C1").EntireColumn.Delete
'Remove Wrap Text
Columns("A:C").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlTop
.WrapText = False
End With
'Get Rid of the Tripple Blank spaces
If Sht2.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
Lr = 2
Else
Lr = Sht2.Cells(Rows.Count, 1).End(xlUp).Row
End If
For x = 2 To Lr
Cells(x, 3).Activate
ActiveCell.Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next x
'=====================================================
'Put the cabinet sizes and type into there own columns
'=====================================================
'Seperate Cabinet Width
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="""", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
TrailingMinusNumbers:=True
'Seperate Cabinet Height
Columns("D:D").Select
Selection.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="X", FieldInfo:=Array(Array(1, 9), Array(2, 1)), TrailingMinusNumbers:=True
'Seperate Cabinet Height
Columns("E:E").Select
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="X", FieldInfo:=Array(Array(1, 9), Array(2, 1)), TrailingMinusNumbers:=True
'Seperate Cabinet Type
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="(", FieldInfo:=Array(Array(1, 9), Array(2, 1)), TrailingMinusNumbers:=True
'Remove the ) in the cabinet type
Cells.Replace What:=")", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'Put the column headers in the new columns
Sht2.Range("C1") = "Width"
Sht2.Range("D1") = "Height"
Sht2.Range("E1") = "Depth"
Sht2.Range("F1") = "Type"
'AutoFit Columns and Rows
Sht2.Columns.AutoFit
Sht2.Rows.AutoFit
Sht2.Range("A1").Select
'====================================================================================================
'Loop Through the Qty,Width,Hight,Depth Columns and Make sure that the Numbers are not stored as Text
'====================================================================================================
If Sht2.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
Lr = 2
Else
Lr = Sht2.Cells(Rows.Count, 1).End(xlUp).Row
End If
For x = 2 To Lr
Cells(x, 1).Activate
ActiveCell.Value = (ActiveCell.Value + 0)
Cells(x, 3).Activate
ActiveCell.Value = (ActiveCell.Value + 0)
Cells(x, 4).Activate
ActiveCell.Value = (ActiveCell.Value + 0)
Cells(x, 5).Activate
ActiveCell.Value = (ActiveCell.Value + 0)
Next x
Application.ScreenUpdating = True
End Sub