Problem with an IF statment using Sheet name

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


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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try
Code:
      Else
         [COLOR=#0000ff]current[/COLOR].Rows("24:" & DeleteTo).Delete shift:=xlUp
      End If

      'rename Ungrouped sheets here
      With [COLOR=#0000ff]current[/COLOR]
          .Name = Replace(.Name, "Ungrouped ", "TopBottom 10 ")
      End With
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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