I've received several pieces of code to help with a project from a few of you on here. I'm now trying to piece this all together so it runs upon worksheet activate.
It basically unmerges, sorts, and remerges while accounting for some other variables. Could someone tell me why I'm not able to put this all together? The error is on ", c" at the end of the "Dim headerRow, lastRow, c" line.
I kinda get what the error is saying, but don't know exactly what to change.....
Private Sub Worksheet_Activate()
For Each c In Array(1, 22)
With Intersect(Columns(c), ActiveSheet.UsedRange)
.UnMerge
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
Next
Range("A2:X36").Sort Key1:=Range("W2"), Order1:=xlAscending, _
Key2:=Range("X2"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Dim headerRow, lastRow, c
On Error GoTo safeExit
headerRow = 1
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
f = headerRow + 1
Application.DisplayAlerts = False
Do Until f > lastRow
l = f + 1
Do Until (Cells(l, 1) <> Cells(l - 1, 1)) Or (Cells(l, 22) <> Cells(l - 1, 22))
l = l + 1
Loop
If f <> l Then
For c = 1 To 22 Step 21
With Range(Cells(f, c), Cells(l - 1, c))
.merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Next c
End If
f = l
Loop
safeExit:
Application.DisplayAlerts = True
For Each c In Array(1, 22)
With Intersect(Columns(c), ActiveSheet.UsedRange)
.UnMerge
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
Next
End Sub
It basically unmerges, sorts, and remerges while accounting for some other variables. Could someone tell me why I'm not able to put this all together? The error is on ", c" at the end of the "Dim headerRow, lastRow, c" line.
I kinda get what the error is saying, but don't know exactly what to change.....
Private Sub Worksheet_Activate()
For Each c In Array(1, 22)
With Intersect(Columns(c), ActiveSheet.UsedRange)
.UnMerge
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
Next
Range("A2:X36").Sort Key1:=Range("W2"), Order1:=xlAscending, _
Key2:=Range("X2"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Dim headerRow, lastRow, c
On Error GoTo safeExit
headerRow = 1
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
f = headerRow + 1
Application.DisplayAlerts = False
Do Until f > lastRow
l = f + 1
Do Until (Cells(l, 1) <> Cells(l - 1, 1)) Or (Cells(l, 22) <> Cells(l - 1, 22))
l = l + 1
Loop
If f <> l Then
For c = 1 To 22 Step 21
With Range(Cells(f, c), Cells(l - 1, c))
.merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Next c
End If
f = l
Loop
safeExit:
Application.DisplayAlerts = True
For Each c In Array(1, 22)
With Intersect(Columns(c), ActiveSheet.UsedRange)
.UnMerge
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
Next
End Sub
Last edited: