Code is only working sometimes

termeric

Active Member
Joined
Jun 21, 2005
Messages
280
I am trying to remove blank cells at the end of one of my sub routines. I've tried both sorting and deleting the blanks and in both cases it works just fine as long as I run the sub directly. However, this is set up to run as one of several subroutines that happens after a button is pressed. When its triggered through the button, the blank rows remain, creating problems downstream. Why does this seem to only work when I activate the sub directly?

Code:
Sub Regional_Legends()
'
' set the list for the regional legend based on years 1-4
'
Dim Row As Integer
'

'Copy Mkt, CFO, Segment from each for the 4 years
    Sheets("Year1").Columns("G:I").Copy Sheets("Regional Legend").Range("a1")
    Sheets("Year2").Columns("G:I").Copy Sheets("Regional Legend").Range("d1")
    Sheets("Year3").Columns("G:I").Copy Sheets("Regional Legend").Range("g1")
    Sheets("Year4").Columns("G:I").Copy Sheets("Regional Legend").Range("j1")
    
'count the number of used rows
Row = Sheets("Regional Legend").UsedRange.Rows.Count
    
'filter out the duplicates for each year
    Sheets("Regional Legend").Range("A1:C" & Row).RemoveDuplicates Columns:=Array(1, 2, 3), _
        Header:=xlYes
    
    Sheets("Regional Legend").Range("D1:F" & Row).RemoveDuplicates Columns:=Array(1, 2, 3), _
        Header:=xlYes
        
    Sheets("Regional Legend").Range("G1:I" & Row).RemoveDuplicates Columns:=Array(1, 2, 3), _
        Header:=xlYes
        
    Sheets("Regional Legend").Range("J1:L" & Row).RemoveDuplicates Columns:=Array(1, 2, 3), _
        Header:=xlYes
        
'recount rows
Row = Sheets("Regional Legend").UsedRange.Rows.Count

'combine the 4 years into one list
Sheets("Regional Legend").Range("D2:F" & Row).Copy Sheets("Regional Legend").Range("a" & Row + 1)
Sheets("Regional Legend").Range("G2:I" & Row).Copy Sheets("Regional Legend").Range("a" & Row * 2)
Sheets("Regional Legend").Range("J2:L" & Row).Copy Sheets("Regional Legend").Range("a" & Row * 3)
        
'recount rows
Sheets("Regional Legend").Range("D1:L" & Row).Clear
        
'filter out the duplicates for the combined list
Row = Sheets("Regional Legend").UsedRange.Rows.Count
Sheets("Regional Legend").Range("A1:C" & Row).RemoveDuplicates Columns:=Array(1, 2, 3), _
        Header:=xlYes
    
'isolate the Plan Market
Row = Sheets("Regional Legend").UsedRange.Rows.Count
    
Sheets("Regional Legend").Range("A1:C" & Row).Copy Sheets("Regional Legend").Range("D1")

    Sheets("Regional Legend").Range("D1:F" & Row).RemoveDuplicates Columns:=Array(1, 3), _
        Header:=xlYes
 
Sheets("Regional Legend").Columns("E").Delete

'Sort out blanks    
'Sheets("Regional Legend").Columns("A:C").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
'Sheets("Regional Legend").Columns("D:E").Sort Key1:=Range("E1"), Order1:=xlAscending, Header:=xlYes


'Delete Blanks
Dim rng As Range

  On Error GoTo NoBlanksFound
    Set rng = Range("A1:E" & Row).SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0

  rng.Rows.Delete Shift:=xlShiftUp

Exit Sub


NoBlanksFound:



    
    
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try replacing

Code:
  On Error GoTo NoBlanksFound
    Set rng = Range("A1:E" & Row).SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0

with
Code:
  On Error GoTo NoBlanksFound
    Set rng = [COLOR="#FF0000"]Sheets("Regional Legend").[/COLOR]Range("A1:E" & Row).SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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