cfdh_edmundo
Board Regular
- Joined
- Nov 9, 2005
- Messages
- 133
Hi,
I am trying to write a macro that loops through each worksheet in the workbook and does the following:
A) Deletes grouped rows (this bit works)
B) If the worksheet has "Ungrouped" in the name, then only show the top 10 and bottom 10 items, delete the rest and rename the sheet "TopBottom 10*" instead of "Ungrouped*"
(The data in the sheet is already pre ordered as I need it)
(There is superflous data in the initial rows of the sheet, so the data I need starts at row 14, and the top ten is always in rows 14:24)
(There are multiple sheets with "Ungrouped" in the sheet name)
Something seems to be going wrong with the second task. I think my if-statement-in-an-if-statement is causing problems and this part of the code is not working and it doesnt advance beyond the first occurance of "Ungrouped*"
I also think my IF OR OR OR statement is not the most efficient way of doing this (it might be better to use a Like function).
Can anyone see where I'm going wrong?
Many thanks,
Chris
I am trying to write a macro that loops through each worksheet in the workbook and does the following:
A) Deletes grouped rows (this bit works)
B) If the worksheet has "Ungrouped" in the name, then only show the top 10 and bottom 10 items, delete the rest and rename the sheet "TopBottom 10*" instead of "Ungrouped*"
(The data in the sheet is already pre ordered as I need it)
(There is superflous data in the initial rows of the sheet, so the data I need starts at row 14, and the top ten is always in rows 14:24)
(There are multiple sheets with "Ungrouped" in the sheet name)
Something seems to be going wrong with the second task. I think my if-statement-in-an-if-statement is causing problems and this part of the code is not working and it doesnt advance beyond the first occurance of "Ungrouped*"
I also think my IF OR OR OR statement is not the most efficient way of doing this (it might be better to use a Like function).
Can anyone see where I'm going wrong?
Many thanks,
Chris
Code:
Sub SortingMacro()
'PURPOSE: Delete Only Rows that are Grouped (i.e. "Detail") within every sheet in the file
'SECONDARY PURPOSE: Also remove non Top/Bottom10 stocks and then rename sheet from "ungrouped"
Dim Current As Worksheet
Dim x As Long
Dim LastRow As Long
Dim rng As Range
Dim LastACell As Long
Dim DeleteTo As Long
'***************************
'Line below loops through every sheet in the file
For Each Current In Worksheets
'--------------
'Retrieve Range of Cells with Data
Set rng = Current.UsedRange
'Determine Last Row in Data Set
LastRow = rng.Rows.Count
'Loop Through Rows and Delete Rows that are Grouped
For x = 1 To LastRow
If rng.Rows(x).OutlineLevel > 1 Then
rng.Rows(x).EntireRow.Delete
x = x - 1
End If
Next x
'--------------
'Now Delete non Top/Bottom 10 rows, IF the sheet name is Ungrouped
If Current.Name = "Ungrouped (1M)" Or Current.Name = "Ungrouped (6M)" Or Current.Name = "Ungrouped (3M)" Or Current.Name = "Ungrouped (12M)" Or Current.Name = "Ungrouped (YTD)" Then
LastACell = Current.Cells(Current.Rows.Count, "A").End(xlUp).Row
DeleteTo = LastACell - 10
If DeleteTo < 35 Then
MsgBox "NOTICE:" & Chr(13) & Chr(10) & "There are 20 stocks or fewer" & Chr(13) & " in this " & Chr(34) & "Ungrouped" & Chr(34) & " tab"
Else
Rows("24:" & DeleteTo).Select
Selection.Delete Shift:=xlUp
End If
'rename Ungrouped sheets here
With ActiveSheet
.Name = Replace(.Name, "Ungrouped ", "TopBottom 10 ")
End With
End If
'Move to Next Worksheet in file (and repeat)
Next
'***************************
End Sub