lonewolf9899
New Member
- Joined
- Jul 26, 2010
- Messages
- 18
Hello,
I have found and tweaked a macro but can't quite work out all the bugs.
I have a spreadsheet with multiple sheets ("10", "20", "30", "40","50","70","90"). Each sheet has the same info in it. What I want to do is DELETE all rows in a sheet that do NOT match the Tab name (example; in the sheet named "10", I want to delete all rows that have the value "20", "30", etc in column A, so only "10" rows are in the "10" tab).
The macro below works great...on the first tab. So the "10" tab is all set, I move to the "20" tab. I tweak the macro so only "20" rows will stay, but when I run the macro, I get the error message :
COMPILE ERROR, Duplicate Declaration in Current Scope. The "Dim rng As Range, cell As Range, del As Range" is highlighted.
How Can I fix the macro so each sheet deletes the appropriate rows?
Sub Macro1()
' This macro deletes all rows on the active worksheet
' that have certain criteria
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "0" _
Or (cell.Value) = "20" _
Or (cell.Value) = "30" _
Or (cell.Value) = "40" _
Or (cell.Value) = "50" _
Or (cell.Value) = "70" _
Or (cell.Value) = "90" Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
End Sub
I have found and tweaked a macro but can't quite work out all the bugs.
I have a spreadsheet with multiple sheets ("10", "20", "30", "40","50","70","90"). Each sheet has the same info in it. What I want to do is DELETE all rows in a sheet that do NOT match the Tab name (example; in the sheet named "10", I want to delete all rows that have the value "20", "30", etc in column A, so only "10" rows are in the "10" tab).
The macro below works great...on the first tab. So the "10" tab is all set, I move to the "20" tab. I tweak the macro so only "20" rows will stay, but when I run the macro, I get the error message :
COMPILE ERROR, Duplicate Declaration in Current Scope. The "Dim rng As Range, cell As Range, del As Range" is highlighted.
How Can I fix the macro so each sheet deletes the appropriate rows?
Sub Macro1()
' This macro deletes all rows on the active worksheet
' that have certain criteria
Dim rng As Range, cell As Range, del As Range
Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each cell In rng
If (cell.Value) = "0" _
Or (cell.Value) = "20" _
Or (cell.Value) = "30" _
Or (cell.Value) = "40" _
Or (cell.Value) = "50" _
Or (cell.Value) = "70" _
Or (cell.Value) = "90" Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
End Sub