peterrudge
New Member
- Joined
- Nov 6, 2011
- Messages
- 17
I am using a macro to create a cutlist from an excel workbook. There are four sheets in the work book. There is a paste special command that runs on each sheet. On two of the sheets the command returns values only in cells that are not empty. On the other two sheets all the empty cells are filled with zeros. I can't figure out how to keep the empty cells from being filled with zeros. Can you help?
Sample Excel Doc I am trying to use macro on
Macro I am trying to execute:
Sample Excel Doc I am trying to use macro on
Macro I am trying to execute:
Code:
Sub CutlistCM()
'
' UnfinishedPartsCutlistCM Macro
' Unfinished parts cultist. Units CM
'
'
ActiveSheet.Name = "Unfinished Parts Units CM"
Sheets.Add After:=Sheets(Sheets.Count)
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Finished Parts Units CM"
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Faces Units CM"
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "Order Units CM"
'
'Change names of columns to work with move routine below
'
Sheets("Unfinished Parts Units CM").Select
Rows("1:1").Select
Selection.Replace What:="PATH", Replacement:="-PART-", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="RIP", Replacement:="-RIP-", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="XCUT", Replacement:="-XCUT-", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="MATERIAL", Replacement:="-MATERIAL-", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="NOTES", Replacement:="-NOTES-", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="W_ORDER", Replacement:="-ORDER-", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Z_CUTLIST", Replacement:="-CUTLIST-", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
'Move columns to correct order
'
Dim aCols() As Variant, z As Long, iColCnt As Long
Dim rFind As Range, rLook As Range
aCols = Array("-PART-", "-RIP-", "-XCUT-", "-MATERIAL-", "-NOTES-", "-ORDER-", "-CUTLIST-")
Set rLook = ActiveSheet.Range("1:1")
For z = LBound(aCols) To UBound(aCols)
Set rFind = rLook.Find(What:=aCols(z))
If Not rFind Is Nothing Then
If ActiveSheet.Columns(z + 1).Address <> rFind.EntireColumn.Address Then
rFind.EntireColumn.Cut
ActiveSheet.Columns(z + 1).Insert
End If
End If
Next z
Application.CutCopyMode = False
'
'Copy entire contents to all sheets
'
Sheets("Unfinished Parts Units CM").Select
Range("A1:BW52").Select
Selection.Copy
Sheets("Finished Parts Units CM").Select
ActiveSheet.Paste
Sheets("Faces Units CM").Select
ActiveSheet.Paste
Sheets("Order Units CM").Select
ActiveSheet.Paste
'
'Filter the rows we want on each sheet
'
Sheets("Unfinished Parts Units CM").Select
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=7, Criteria1:="Yes"
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=4, Criteria1:= _
"=1/4 Int Material", Operator:=xlOr, Criteria2:="=3/4 Int Material"
Sheets("Finished Parts Units CM").Select
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=7, Criteria1:="Yes"
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=4, Criteria1:= _
"=1/4 Ext Material", Operator:=xlOr, Criteria2:="=3/4 Ext Material"
Sheets("Faces Units CM").Select
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=7, Criteria1:="Yes"
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=4, Criteria1:= _
"=Faces"
Sheets("Order Units CM").Select
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$BZ$132").AutoFilter Field:=6, Criteria1:= _
"=Yes"
'
'Delete hidden rows and columns
'
Sheets("Unfinished Parts Units CM").Select
For lp = 256 To 1 Step -1 'loop through all columns
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1 'loop through all rows
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next
Sheets("Finished Parts Units CM").Select
For lp = 256 To 1 Step -1 'loop through all columns
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1 'loop through all rows
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next
Sheets("Faces Units CM").Select
For lp = 256 To 1 Step -1 'loop through all columns
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1 'loop through all rows
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next
Sheets("Order Units CM").Select
For lp = 256 To 1 Step -1 'loop through all columns
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1 'loop through all rows
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next
'
'Delete columns we don't need
'
Sheets("Unfinished Parts Units CM").Select
Columns("E:BZ").Select
Selection.Delete Shift:=xlToLeft
Sheets("Finished Parts Units CM").Select
Columns("E:BZ").Select
Selection.Delete Shift:=xlToLeft
Sheets("Faces Units CM").Select
Columns("F:BZ").Select
Selection.Delete Shift:=xlToLeft
Sheets("Order Units CM").Select
Columns("F:BZ").Select
Selection.Delete Shift:=xlToLeft
'
'Change values to CM
'
Sheets("Unfinished Parts Units CM").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "2.54"
Range("H1").Select
Selection.Copy
Range("B2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-3
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.0"
Range("H1").Select
ActiveCell.FormulaR1C1 = ""
Sheets("Finished Parts Units CM").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "2.54"
Range("H1").Select
Selection.Copy
Range("B2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-3
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.0"
Range("H1").Select
ActiveCell.FormulaR1C1 = ""
Sheets("Faces Units CM").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "2.54"
Range("H1").Select
Selection.Copy
Range("B2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-3
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.0"
Range("H1").Select
ActiveCell.FormulaR1C1 = ""
Sheets("Order Units CM").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "2.54"
Range("H1").Select
Selection.Copy
Range("B2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-3
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
SkipBlanks:=True, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.0"
Range("H1").Select
ActiveCell.FormulaR1C1 = ""
'
'Sort by material, rip, then xcut
'
Sheets("Unfinished Parts Units CM").Select
Range("A1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("D2:D132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("B2:B132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("C2:C132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort
.SetRange Range("A1:G132")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Finished Parts Units CM").Select
Range("A1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("D2:D132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("B2:B132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("C2:C132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort
.SetRange Range("A1:G132")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Faces Units CM").Select
Range("A1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("D2:D132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("B2:B132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("C2:C132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort
.SetRange Range("A1:G132")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Order Units CM").Select
Range("A1:G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("D2:D132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("B2:B132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort.SortFields.Add Key _
:=Range("C2:C132"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Unfinished Parts Units CM").Sort
.SetRange Range("A1:G132")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Call BlankRowUnfin
Call BlankRowFin
Call SumRIPRowsUnFin
Call SumRIPRowsFin
Call Formatting
End Sub
'
'Add a blank row when the RIP value changes
'
Sub BlankRowUnfin()
Sheets("Unfinished Parts Units CM").Select
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 2 Step -1
If Cells(lRow, "B") <> Cells(lRow - 1, "B") Then Rows(lRow).EntireRow.Insert
Next lRow
End Sub
Sub BlankRowFin()
Sheets("Finished Parts Units CM").Select
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 2 Step -1
If Cells(lRow, "B") <> Cells(lRow - 1, "B") Then Rows(lRow).EntireRow.Insert
Next lRow
End Sub
Sub SumRIPRowsUnFin()
'
'Sum RIP rows
'
Sheets("Unfinished Parts Units CM").Select
Dim StartRow As Integer
Dim EndRow As Integer
StartRow = 3
EndRow = Range("D65536").End(xlUp).Offset(1, 0).Row
For i = StartRow To EndRow
If Cells(i, "C") = "" And i > StartRow Then
Cells(i, "C").Formula = "=SUM(C" & StartRow & ":C" & i - 1 & ")/244"
Cells(i, "D").Formula = "Rips"
StartRow = i + 1
End If
Next
End Sub
Sub SumRIPRowsFin()
Sheets("Finished Parts Units CM").Select
Dim StartRow As Integer
Dim EndRow As Integer
StartRow = 3
EndRow = Range("D65536").End(xlUp).Offset(1, 0).Row
For i = StartRow To EndRow
If Cells(i, "C") = "" And i > StartRow Then
Cells(i, "C").Formula = "=SUM(C" & StartRow & ":C" & i - 1 & ")/244"
Cells(i, "D").Formula = "Rips"
StartRow = i + 1
End If
Next
End Sub
Sub Formatting()
'
'Remove extra words in PART column and format RIP and XCUT for easier reading
'
Sheets("Unfinished Parts Units CM").Select
Cells.Select
Selection.Replace What:="Model*/WorkPort*/", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Columns("A:A").ColumnWidth = 19.43
Columns("B:C").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Sheets("Finished Parts Units CM").Select
Cells.Select
Selection.Replace What:="Model*/WorkPort*/", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Columns("A:A").ColumnWidth = 19.43
Columns("B:C").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Sheets("Faces Units CM").Select
Cells.Select
Selection.Replace What:="Model*/WorkPort*/", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Columns("A:A").ColumnWidth = 19.43
Columns("B:C").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Sheets("Order Units CM").Select
Cells.Select
Selection.Replace What:="Model*/WorkPort*/", Replacement:="", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Columns("A:A").ColumnWidth = 19.43
Columns("B:C").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
'
'Add some lines to help highlight rips totals
'
Sheets("Unfinished Parts Units CM").Select
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:="Rips", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
Selection.FormatConditions(1).StopIfTrue = False
Sheets("Finished Parts Units CM").Select
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:="Rips", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
Selection.FormatConditions(1).StopIfTrue = False
'
'Change page settings to show file name and sheet name as headers / footers. Printer header on all pages
'
Sheets("Unfinished Parts Units CM").Select
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
End With
With ActiveSheet.PageSetup
.CenterHeader = "&F"
.CenterFooter = "&A"
End With
Sheets("Finished Parts Units CM").Select
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
End With
With ActiveSheet.PageSetup
.CenterHeader = "&F"
.CenterFooter = "&A"
End With
Sheets("Faces Units CM").Select
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
End With
With ActiveSheet.PageSetup
.CenterHeader = "&F"
.CenterFooter = "&A"
End With
Sheets("Order Units CM").Select
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
End With
With ActiveSheet.PageSetup
.CenterHeader = "&F"
.CenterFooter = "&A"
End With
End Sub