VBA: Fix scroll bar but keep named range

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
Folks:
This code fixes my scrollbars without having to save the document first, but I am getting an error: I carry over certain entries to a summary sheet, then delete the ones I don't want. On the summary sheet is a named range = the entire column AF. If I delete all the rows with populated cells in column AF, (all rows below header except total line) then my named range = #REF and it ruins my reference. How can I avoid this (populate the header row?) - Thank you - Rowland

Code:
Sub DeleteUnusedFormats(wksScroll As Worksheet)
'routine resets scrollbar by deleting unused rows below data
Dim lLastRow As Long
Dim lLastColumn As Long
Dim lRealLastRow As Long
Dim lRealLastColumn As Long

    'note: May need to place sheet.activate code before this sub is called
    wksScroll.Activate
    With Range("A1").SpecialCells(xlCellTypeLastCell)
        lLastRow = .Row
        lLastColumn = .Column
    End With

    lRealLastRow = Cells.Find("*", Range("A1"), xlFormulas, , _
        xlByRows, xlPrevious).Row

    lRealLastColumn = Cells.Find("*", Range("A1"), xlFormulas, , _
        xlByColumns, xlPrevious).Column

    If lRealLastRow < lLastRow Then
        Range(Cells(lRealLastRow + 1, 1), Cells(lLastRow, 1)).EntireRow.Delete
    End If

    If lRealLastColumn < lLastColumn Then
        Range(Cells(1, lRealLastColumn + 1), Cells(1, lLastColumn)) _
            .EntireColumn.Delete
    End If

    ActiveSheet.UsedRange   'resets LastCell
    
    'cursor default to top left cell = A1
    Application.Goto Reference:=Range("a1"), Scroll:=True
End Sub

p.s. Tried just using
Code:
 ActiveSheet.UsedRange
but not enough, eventhough the rows seem not to be populated below grand total
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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