VBA Code:
With ActiveWorkbook.Worksheets("YourSheet").Sort '<<<<Edit sheet name
.SortFields.Clear
.SortFields.Add2 Key:=Range("A1").CurrentRegion _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A1").CurrentRegion
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Thank you so much for the reply. I had given up on a response!
It still doesn't work. Also, I am having issues with inserting missing numbers. However, my original issue is much more problematic since I haven't been able to correct the issue. I can't get past this hurdle.
Since the original post, I have tweaked my code to the following, updating code to your suggestion. I know I shouldn't use select, but I don't know how to get around it.
Sub TEST_Sample_MrExcel_Board_Suggestion()
Dim ws As Worksheet
Dim rng9 As Range
Dim wsPL As Worksheet
Dim LastCol As Long
Dim i As Long
Dim lastRow As Long
Set wsPL = Worksheets("Price List ")
' Loops through each sheet in workbook
For Each ws In ThisWorkbook.Worksheets
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set rng9 = Range("A1:Az" & lastRow)
'Skips over sheet named Price List
If ws.Name <> wsPL.Name Then
ws.Activate
' Deletes row 2, then row 3 and inserts a blank row in Row 1
ws.Rows("2:2").Delete
ws.Rows("3:3").Delete
ws.Rows("1:1").Insert
' Cuts row 3 and pastes to row 1
Range("A3:AZ3").Cut
Cells(1, 1).Select
ws.Paste
' Deletes row 3
ws.Rows("3:3").Delete
' Range("a1:az80").Activate
Range("a1:az" & lastRow).Select
'Sorts columns left to right
'Updated to your suggestion
With ws.Sort
.SortFields.Clear
.SortFields.Add2 Key:=Range("A1").CurrentRegion _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A1").CurrentRegion
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End If
Next ws
End Sub
Also, am I posting the code correctly; i.e. Code Tags?
I read the short guide, but I don't understand the explanation.
Sample Data:
Group | SKU: | Description | C | WT | S | W | D | H | 1 | 2 | 3 | 4 | 5 | L1 | L2 | | | | | | | | | | |
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | | | | | | | | | | | | | | | | | |
1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 4 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
Group | SKU: | DESCRIPTION | C | WT | S | W | D | H | 1 | 2 | 3 | 4 | 5 | L1 | L2 | | | | | | | | | | |
343 PADDY | 343-07 | LAF SINGLE | 29.8 | 90 | 1.5 | 40 | 39 | 41 | $340 | $345 | $350 | $355 | $360 | $465 | $485 | | | | | | | | | | |
344 PADDY | 343-05P | POWER HEADREST LAF SINGLE | 29.8 | 108 | 1.5 | 40 | 39 | 41 | $440 | $445 | $450 | $455 | $460 | $565 | $585 | | | | | | | | | | |
345 PADDY | 343-05P NL | POWER HEADREST LAF SINGLE SEAT | 29.8 | 108 | 1.5 | 40 | 39 | 41 | $465 | $470 | $475 | $480 | $485 | $590 | $610 | | | | | | | | | | |
346 PADDY | 343-08 | RAF SINGLE SEAT | 29.8 | 90 | 1.5 | 40 | 39 | 41 | $340 | $345 | $350 | $355 | $360 | $465 | $485 | | | | | | | | | | |
347 PADDY | 343-06P | POWER HEADREST RAF SINGLE | 29.8 | 108 | 1.5 | 40 | 39 | 41 | $440 | $445 | $450 | $455 | $460 | $565 | $585 | | | | | | | | | | |
348 PADDY | 343-06P NL | POWER HEADREST RAF SINGLE SEAT | 29.8 | 108 | 1.5 | 40 | 39 | 41 | $465 | $470 | $475 | $480 | $485 | $590 | $610 | | | | | | | | | | |
The second set of code below the sample data works well, except when inserting missing column numbers, it skips some blank columns and inserts the missing numbers based on the far-right column (AZ). I would like it not to skip those missing columns. The 4 in Row 3Column 17 above is actually in Column 26.
Sub Insert_Missing_Columns_by_Number()
'
'********************************
' INSERT MISSING COLUMN
'********************************
Dim ws As Worksheet
Dim wsPL As Worksheet
Set wsPL = Worksheets("Price List ")
' Loops through each sheet in workbook
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> wsPL.Name Then
ws.Activate
Range("A1").Activate
' Insert row in Row 1
Rows("2:2").Insert
Range("A2").Activate
' Find missing column numbers based on row 2 = FINAL HEADER NUMBER
ActiveCell.Formula2R1C1 = _
"=IFERROR(SMALL(IF(COUNTIF(R[1]C:R[1]C[25],COLUMN(C1:C26))=0,COLUMN(C1:C26),""""),COLUMN(C1:C26)),"""")"
' Copies range A1:Z1 and pastes values to row 1
ws.Range("A2").Copy
' Copies range A1:Z1 and pastes values to first black column in row 2
Range("A2:Z2").Copy
Cells(3, Columns.Count).End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next ws
End Sub