Compile error: Duplicate declaration in current scope

slam

Well-known Member
Joined
Sep 16, 2002
Messages
919
Office Version
  1. 365
  2. 2019
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
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Now I get Run-time error '1004': No cells were found. This line is highlighted when I hit debug

.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
 
Upvote 0
That means that there were not any blanks. Try

Code:
With Intersect(Columns(c), ActiveSheet.UsedRange)
    .UnMerge
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    On Error GoTo 0
    .Value = .Value
End With
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,223
Members
452,896
Latest member
IGT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top