Organizing by color - excel

I have run into an issue, I have a script thanks to several members here has allowed me to import directly from a file into a workbook. With that same script I've incorporated the ability to organize by color red on top, and green underneith. I thought I could write the code to make it so that yellow would be in the middle myself, but it doesn't take into concideration it seems the difference between yellow and red, although I thought I did make that difference noticable though the script.
If someone can look at this and tell me where I am going wrong it would be greatly apprecaited.
This is what I get now for an end result, the yellow is on the bottom of the sheet btw after importing.
The code for some reason isn't reading right, so attached is also a link to the sheet, with the added file to import.
Zip File
or the files seperate here...
Sheet
CSV
Here is my code:
Option Explicit
Sub Update_POT()
Dim wsPOD As Worksheet
Dim wsPOT As Worksheet
Dim wsPOA As Worksheet
Dim cel As Range
Dim lastrow As Long, fstcell As Long, i As Long, Er As Long, lstCol As Long, lstRow As Long, strFile As String
Set wsPOD = Sheets("PO Data")
Set wsPOT = Sheets("PO Tracking")
Set wsPOA = Sheets("PO Archive")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
With wsPOD
.Columns("A:AB").ClearContents
.Range("Y1").Formula = "=COUNTIFS('PO Tracking'!$D:$D,$C1,'PO Tracking'!$C:$C,$D1,'PO Tracking'!$F:$F,$G1)"
.Range("Z1").Formula = "=IF($M1,"""",""Different"")"
.Range("AA1").Formula = "=IF(ISBLANK($C1),0,1)"
.Range("AB1").Formula = "=IF($O1,""Full"","""")"
End With
strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please selec text file...")
With wsPOD.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=wsPOD.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
With wsPOD
'first bring columns F:G up to match their line
For Each cel In Intersect(.UsedRange, .UsedRange.Offset(5), .Columns(6))
If cel = vbNullString And cel.Offset(, -2) <> vbNullString Then
.Range(cel.Offset(1), cel.Offset(1, 1)).Copy cel
cel.Offset(1).EntireRow.Delete
End If
Next
'now fil columns A:D to match PO Date and PO#
For Each cel In Intersect(.UsedRange, .UsedRange.Offset(5), .Columns(1))
If cel = vbNullString And cel.Offset(, 5) <> vbNullString Then
.Range(cel.Offset(-1), cel.Offset(-1, 3)).Copy cel
End If
Next
lastrow = wsPOD.Cells(Rows.Count, "J").End(xlUp).Row
fstcell = wsPOD.Cells(Rows.Count, "N").End(xlUp).Row
wsPOD.Range("Y1:AB1").Copy wsPOD.Range("M" & fstcell & ":P" & lastrow)
wsPOD.Range("M:P").Calculate
End With
With Intersect(wsPOD.UsedRange, wsPOD.Columns("P"))
.AutoFilter 1, "<>Full"
With Intersect(.Offset(2).EntireRow, .Parent.Range("A:P"))
.EntireRow.Delete
End With
.AutoFilter
End With
With Intersect(wsPOD.UsedRange, wsPOD.Columns("N"))
.AutoFilter 1, "<>Different"
With Intersect(.Offset(2).EntireRow, .Parent.Range("A:P"))
.EntireRow.Delete
End With
.AutoFilter
End With
'Final Adjustments before transfering over to PO Tracking.
With wsPOD
.AutoFilterMode = False
lastrow = wsPOD.Cells(Rows.Count, "A").End(xlUp).Row
Intersect(.UsedRange, .Range("A4:A" & lastrow)).Cut .Range("Q3")
Intersect(.UsedRange, .Columns("D")).Cut .Range("R1")
Intersect(.UsedRange, .Columns("C")).Cut .Range("S1")
Intersect(.UsedRange, .Columns("B")).Cut .Range("T1")
Intersect(.UsedRange, .Columns("G")).Cut .Range("U1")
Intersect(.UsedRange, .Columns("F")).Cut .Range("V1")
End With
With wsPOD
wsPOD.Columns("A:P").ClearContents
lastrow = wsPOD.Cells(Rows.Count, "Q").End(xlUp).Row
wsPOD.Range("Q3:V" & lastrow).Copy wsPOT.Cells(Rows.Count, "B").End(xlUp).Offset(1)
End With
'Format PO Tracking
With wsPOT
.Range("Q1:U1").Copy
lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("V1:X1").Copy .Range("H3:J" & lastrow)
.Range("N2:O2").Copy .Range("N3:O" & lastrow)
.Range("P1:V1").Copy
.Range("B3:H" & lastrow).PasteSpecial xlPasteFormats
.Range("K3:K" & lastrow).Borders.Weight = xlThin
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
.Range("H:J").Calculate
.Sort.SortFields.Clear
'Sort PO Tracking
'Sort Reds
.Sort.SortFields.Add(.Range("J3:J" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(1)
.Sort.SortFields.Add Key:=Range( _
"J3:J30" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
'Sort Yellows
.Sort.SortFields.Add(.Range("I3:I" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(2)
.Sort.SortFields.Add Key:=Range( _
"I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
'Sort Greens
.Sort.SortFields.Add(.Range("I3:I" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(3)
.Sort.SortFields.Add Key:=Range( _
"I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With .Sort
.SetRange wsPOT.Range("B2:K" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
With wsPOD
wsPOD.Columns("Q:X").ClearContents
wsPOD.Cells(1, 25).Value = "=COUNTIFS('PO Tracking'!$D:$D,$C1,'PO Tracking'!$C:$C,$D1,'PO Tracking'!$F:$F,$G1)"
wsPOD.Cells(1, 27).Value = "=IF(ISBLANK($C1),0,1)"
wsPOD.Range("Y1:AB1").Copy wsPOD.Range("M5:P5")
End With
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
End With
End Sub

You have to remove the line
.Sort.SortFields.Add Key:=Range( _
"I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
in
'Sort Yellows
.Sort.SortFields.Add(.Range("I3:I" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(2)
.Sort.SortFields.Add Key:=Range( _
"I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
You cannot have have the same duplicate sort conditions for both Yellow and Green in the same column. Remove that line and try again.

Related

Incorrect Dynamic Range in Excel VBA Macro

I have the following code in a macro to copy the contents of C2 and D2 cells of the active worksheet and copy it down to the last row of the table.
1 Range("C:D").Insert Shift:=xlToRight
2 Range("C2").FormulaR1C1 = "=RIGHT(RC[2],4)"
3 Range("D2").FormulaR1C1 = "=LEFT(RC[1],LEN(RC[1])-5)"
4 lastrow = Range("A" & Rows.Count).End(xlUp).Row
5 Range("C2").Copy Range("C2:C" & lastrow)
6 lastrow = Range("B" & Rows.Count).End(xlUp).Row
7 Range("D2").Copy Range("D2:D" & lastrow)
I've been using this code for nearly 3 years now for my daily work but i just suddenly came across a file in which the dynamic range is not selected correctly at line 4 and 6. The sheet has around 30k lines but the formula only copies to the first 284 lines in both column C and D.
I first thought i was referring to a wrong sheet so i tried "lastrow = Activesheet.Range("A" & Rows.Count).End(xlUp).Row"as well as lastrow = Sheet2.Range("A" & Rows.Count).End(xlUp).Rowto the same result.
Anyone can help me figure this out? I'm a bit rusty since it's been a while since i looked at anything remotely related to code...
(Can provide the file if needed)
Sub SSFormula()
Application.ScreenUpdating = False
Dim strPath As String
Dim lastcol%, dest As Range, lastrow%, pt As PivotTable
Dim iCol As Long
Dim iColEnd As Long
Dim strFileName As String
Dim strFileNameTemp As String
Application.DisplayAlerts = False
strFileNameTemp = Environ("USERPROFILE") & "\Desktop\SHELFSTOCK\" & Format(Now(), "DD-MMM-YYYY - hh mmAMPM") & " SHELFSTOCK" & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFileNameTemp, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.Workbooks.Open (strFileNameTemp)
On Error Resume Next
For x = 1 To 200
If Not Range("a1").Offset(0, x).IsEmpty(ActiveCell.Value) Then Range("A:A").Offset(0, x).TextToColumns Destination:=Range("A1").Offset(0, x), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Next x
Range("C:D").Delete Shift:=xlToLeft
lastrow = Sheets("Group_PositionList").Range("a" & Rows.Count).End(xlUp).Row
lastcol = Sheets("Group_PositionList").Cells(lastrow, Columns.Count).End(xlToLeft).Column
Sheets.Add
Set dest = Sheets("sheet1").[A1]
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Group_PositionList!R1C1:R" & lastrow & "C" & _
lastcol, Version:=6).CreatePivotTable TableDestination:=dest, TableName:="PivotTable2", DefaultVersion:=6
Set pt = Sheets("sheet1").PivotTables("PivotTable2")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Product_ID")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Name")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable2").RowAxisLayout xlTabularRow
ActiveSheet.PivotTables("PivotTable2").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable2")
.ColumnGrand = False
.RowGrand = False
End With
Set pt = ActiveSheet.PivotTables(1)
With pt
For Each pf In .PivotFields
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
End With
Set pt = ActiveSheet.PivotTables(1)
With pt
iCol = 1
iColEnd = .PivotFields.Count
For iCol = 1 To iColEnd
With .PivotFields(iCol)
If .Orientation = 0 Then
.Orientation = xlDataField
End If
End With
Next iCol
End With
With ActiveSheet.PivotTable
.ManualUpdate = True
For Each pf In .DataFields
With pf
.Function = xlSum
.NumberFormat = "#,##0"
End With
Next pf
.ManualUpdate = False
End With
With ActiveSheet.PivotTables("PivotTable2").DataPivotField
.Orientation = xlRowField
.Position = 3
End With
pt.TableRange1.Copy
Sheets.Add
Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Cells.Columns.AutoFit
Cells.Replace What:="sum of ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:=".pln", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Range("C:D").Insert Shift:=xlToRight
.Range("C2:C" & lRow).Formula = "=RIGHT(E2,4)"
.Range("D2:D" & lRow).Formula = "=LEFT(E2,LEN(E2)-5)"
Cells.Copy
Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("E:E").Delete Shift:=xlToLeft
Range("C1").FormulaR1C1 = "OUTLET"
Range("D1").FormulaR1C1 = "DISPLAY CATEGORY"
Range("E1").FormulaR1C1 = "SHELF STOCK"
Cells.Columns.AutoFit
Cells.Replace What:="0", Replacement:="", LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
On Error GoTo Next_Block:
Range("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Next_Block:
Rows("1:1").AutoFilter
Cells.Columns.AutoFit
Sheets("Sheet2").Name = "Position_by_Fixture"
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Sheets("Group_PositionList").Delete
Application.DisplayAlerts = True
Range("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Application.DisplayAlerts = False
strFileName = Environ("USERPROFILE") & "\Desktop\SHELFSTOCK\" & Range("'Position_by_Fixture'!D2").Text & " SHELFSTOCK" & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFileName, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
Kill (strFileNameTemp)
Application.ScreenUpdating = True
End Sub
As suggested in the comments above, try this code (UNTESTED)
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
.AutoFilterMode = False
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("C:D").Insert Shift:=xlToRight
.Range("C2:C" & lRow).Formula = "=RIGHT(E2,4)"
.Range("D2:D" & lRow).Formula = "=LEFT(E2,LEN(E2)-5)"
End With
End Sub

Sort ranges from 2 columns repeatedly in vba

With below code I could sort data (marked with blue background) from 2 columns based on "B" column. Similarly I want to repeat the same for each blue block. I've highlighted cells manually just for illustration. Any help will be appreciated.
Code:
Sub SortRanges()
Dim firstcell As String
With Columns("B")
.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlValues).Activate
firstcell = ActiveCell.Row
End With
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveWorkbook.Worksheets("Sheet4").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet4").Sort.SortFields.Add Key:=Range("B" & firstcell & ":B" & firstcell + 5), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet4").Sort
.SetRange Range("A" & firstcell & ":B" & firstcell + 5)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Try a loop down your column and, as it looks like everything is blocks of 5, do something like:
lr = cells(rows.count,1).end(xlup).row
For i = 1 to lr
if cells(i,1).interior.color = Blue Then `FIX THIS TO MATCH THE BLUE YOU WANT
Range(Cells(i,1),Cells(i+5,2)).Sort key1:=Range(Cells(i,2),Cells(i+5,2)), order1:=xlAscending, Header:=xlNo
i=i+5
End if
next i
I may have not understood the part about your highlighting... if that blue is the "highlight" then you can modify the above such that:
lr = cells(rows.count,1).end(xlup).row
For i = 1 to lr
if not isempty(cells(i,2)) Then
Range(Cells(i,1),Cells(i+5,2)).Sort key1:=Range(Cells(i,2),Cells(i+5,2)), order1:=xlAscending, Header:=xlNo
i=i+5
End if
next i
One more thing... if you can just run 2 sorts in sequence, second should be your final sort, like:
lr = cells(rows.count,1).end(xlup).row
For i = 1 to lr
if not isempty(cells(i,2)) Then
Range(Cells(i,1),Cells(i+5,2)).Sort key1:=Range(Cells(i,1),Cells(i+5,1)), order1:=xlAscending, Header:=xlNo
Range(Cells(i,1),Cells(i+5,2)).Sort key1:=Range(Cells(i,2),Cells(i+5,2)), order1:=xlAscending, Header:=xlNo
i=i+5
End if
next i

Trying to sort by custom order

I am trying to sort a list order to the order of BMC-, CSR-, MC- and CSR-. This list exist on column B and has data on column C.
Sub telecomsorter()
Dim vCustom_Sort As Variant, rr As Long
vCustom_Sort = Array("BMC-", "CSR-", "MC-", "LC-", Chr(42))
Application.AddCustomList ListArray:=vCustom_Sort
With Worksheets("TELECOM")
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row 'Find the last row for the given table
Range("B13:C" & LastRow).Select
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Add2 Key:=Range("B13:B47"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("TELECOM").Sort
.SetRange Range("B13:C" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
I want the list to be in the order of:
BMC-
CSR-
MC-
LC-
If there are multiple strings that are the same, then I want to sort according to the order of column C.
Give it a try to this... I removed the array and add the sorting values directly to the Sort properties.
CustomOrder:="BMC-,CSR-,MC-,LC-," & Chr(42) & ""
Option Explicit
Sub telecomsorter()
Dim LastRow As Long
With Worksheets("TELECOM")
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
Range("B13:C" & LastRow).Select
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Add2 Key:=Range( _
"B14:B" & LastRow & ""), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"BMC-,CSR-,MC-,LC-," & Chr(42) & "", DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Add2 Key:=Range( _
"C14:C" & LastRow & ""), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("TELECOM").Sort
.SetRange Range("B13:C" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
EDIT: I couldn't figure it out why the LC- always comes before the MC-, so I made a small function to do the workaround... it may not be the ideal solution but it works.
Option Explicit
Sub telecomsorter()
Dim LastRow As Long
Dim First As Long
Dim Last As Long
With Worksheets("TELECOM")
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
Range("B13:C" & LastRow).Select
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TELECOM").Sort.SortFields.Add Key:=Range( _
"B14:B" & LastRow & ""), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"BMC-*,CSR-*,MC-*,LC-*," & Chr(42) & "", DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("B14:C" & LastRow)
.Header = xlNo
.Orientation = xlTopToBottom
.Apply
End With
Call SortSpecial("LC-*", xlDescending)
Call SortSpecial("MC-*", xlAscending)
Call SortSpecial("LC-*", xlAscending)
End With
End Sub
Public Function SortSpecial(ByVal StrVal As String, ByVal SortOrder As XlSortOrder)
Dim First As Long
Dim Last As Long
First = Columns("B").Find(StrVal, , xlValues, , xlRows, xlNext, , , False).Row
Last = Columns("B").Find(StrVal, , xlValues, , xlRows, xlPrevious, , , False).Row
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("B" & First & ":B" & Last), SortOn:=xlSortOnValues, Order:=SortOrder, DataOption:=xlSortNormal
With ActiveSheet.Sort
If SortOrder = xlAscending Then
.SetRange Range("B" & First & ":C" & Last)
Else
Last = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
.SetRange Range("B" & First & ":C" & Last)
End If
.Header = xlNo
.Orientation = xlTopToBottom
.Apply
End With
End Function

Run-time error only after successful runs - Excel VBA

I have an Excel VBA macro that works successfully the first two times I run it, but the third time it gives this error:
Run-Time Error '1004'
The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.
If I restart Excel it works the first two times, then gives the error again. Why would this happen? Here's my code:
Dim rawData As Object
Dim report As Object
Dim areaCodes As Object
Set rawData = Sheets("RawData")
Set report = Sheets("Report")
Set areaCodes = Sheets("AreaCodes")
report.Cells.Clear
report.Cells.ClearFormats
stateCol = rawData.Cells(1, 1).EntireRow.Find(What:="state", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
Dim MyRange As Range
Set MyRange = rawData.Cells(1, stateCol)
With rawData
lastRow = .Cells(Rows.Count, MyRange.Column).End(xlUp).Row
.Range(.Cells(2, stateCol), .Cells(lastRow, stateCol)).Copy
End With
With report
.Range("A3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Range(.Cells(3, 1), .Cells(lastRow + 1, 1)).RemoveDuplicates Columns:=1, Header:=xlNo
End With
lastRow = report.Cells(Rows.Count, Range("A1").Column).End(xlUp).Row
report.Range("B3").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(rawData!C[" & stateCol - 2 & "],report!RC[-1])"
Range("B3").AutoFill Destination:=Range("B3:B" & lastRow)
Range("B" & lastRow + 1).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" & lastRow - 2 & "]C:R[-1]C)"
Range("C3").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/R" & lastRow + 1 & "C[-1]"
Range("C3").AutoFill Destination:=Range("C3:C" & lastRow)
Range("C" & lastRow + 1).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" & lastRow - 2 & "]C:R[-1]C)"
Range("C:C").NumberFormat = "0.0%"
Range("A2:A" & lastRow + 1).Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
report.Range("A2").Value = "State"
report.Range("A2").Font.Bold = True
report.Range("A:A").HorizontalAlignment = xlCenter
report.Range("A3").FormulaR1C1 = "=INDEX(areaCodes!R2C5:R52C5,MATCH(report!RC[1],AreaCodes!R2C6:R52C6,0))"
Range("A3").Select
ActiveCell.AutoFill Destination:=Range("A3:A" & lastRow)
With report
newLastRow = .Cells(Rows.Count, Range("C1").Column).End(xlUp).Row - 1
.Range(.Cells(3, 3), .Cells(newLastRow, 3)).Copy
.Range("C3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
With report
.Sort.SortFields.Add Key:=Range("C2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
newLastRow = .Cells(Rows.Count, Range("C1").Column).End(xlUp).Row - 1
With .Sort
.SetRange Range("A2:D" & newLastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
you should qualify the range in your sort instruction, this will produce an error if the sheet report is not activated
With report
.Sort.SortFields.Add Key:=.Range("C2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
newLastRow = .Cells(Rows.Count, .Range("C1").Column).End(xlUp).Row - 1
With .Sort
.SetRange report.Range("A2:D" & newLastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With

Double Organize not working

I have a script:
Sub wsPOT()
Dim wsPOT As Worksheet
Dim cel As Range
Dim lastrow As Long, lastrow2 As Long, fstcell As Long, i As Long, Er As Long, lstCol As Long, lstRow As Long
Set wsPOT = Sheets("PO Tracking")
With wsPOT
wsPOT.Range("Q1:U1").Copy
lastrow = wsPOT.Cells(Rows.Count, "B").End(xlUp).Row
wsPOT.Range("V1:X1").Copy wsPOT.Range("H3:J" & lastrow)
wsPOT.Range("N2:O2").Copy wsPOT.Range("N3:O" & lastrow)
wsPOT.Range("P1:V1").Copy
wsPOT.Range("B3:H" & lastrow).PasteSpecial xlPasteFormats
wsPOT.Range("K3:K" & lastrow).Borders.Weight = xlThin
lastrow = wsPOT.Cells(Rows.Count, "B").End(xlUp).Row
wsPOT.Range("H:J").Calculate
wsPOT.Range("B3:K" & lastrow).Sort key1:=Range("H3:H" & lastrow), order1:=xlAscending
lastrow2 = wsPOT.Cells(Rows.Count, "H").End(xlUp).Row
wsPOT.Range("B3:K" & lastrow2).Sort key1:=Range("H3:H" & lastrow2), order1:=xlDescending
End With
End Sub
This is meant to ultimately organize the sheet so that the the late jobs are on top, and then those are organized by oldest to youngest.
It seems not to work. What seems to happen is that the first orginization works fine, but then it seems to ignore the second criteria.
Attached is the sheet, with script.
https://dl.dropbox.com/u/3327208/Excel/Orga.xlsm
If someone can help look at this it be apprecaited.
Is this what you are trying?
Option Explicit
Sub wsPOT()
Dim wsPOT As Worksheet
Dim cel As Range
Dim lastrow As Long, lastrow2 As Long, fstcell As Long
Dim i As Long, Er As Long, lstCol As Long, lstRow As Long
Set wsPOT = Sheets("PO Tracking")
With wsPOT
.Range("Q1:U1").Copy
lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("V1:X1").Copy .Range("H3:J" & lastrow)
.Range("N2:O2").Copy .Range("N3:O" & lastrow)
.Range("P1:V1").Copy
.Range("B3:H" & lastrow).PasteSpecial xlPasteFormats
.Range("K3:K" & lastrow).Borders.Weight = xlThin
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
.Range("H:J").Calculate
.Sort.SortFields.Clear
'~~> Sort on Red Icon First, putting it on top
.Sort.SortFields.Add(.Range("J3:J" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(1)
'~~> Sort on Values of Red icon in descending order
.Sort.SortFields.Add Key:=Range( _
"J3:J" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
'~~> Sort on Green Icon Next, putting it on top after Red
.Sort.SortFields.Add(.Range("I3:I" & lastrow), _
xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
IconSets(4).Item(3)
'~~> Sort on Values of Green icon next in descending order
.Sort.SortFields.Add Key:=Range( _
"I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With .Sort
.SetRange wsPOT.Range("B2:K" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub

Resources