Consulting

Results 1 to 2 of 2

Thread: Updating code for "Reducing Excel File Size" by DRJ

  1. #1
    VBAX Tutor
    Joined
    Feb 2008
    Location
    New York
    Posts
    215
    Location

    Updating code for "Reducing Excel File Size" by DRJ

    Hello,

    I had found the following code by DRJ in this forum. It reduced my file from 8 MB to 1.5 MB. Thanks DRJ, for you contribution. Now, I am wondering if there can be another code that would include the named ranges or formulas when any new rows are added in the workbook so that I do not have to update the formulas and ranges when new rows are added. Thanks.



    Option Explicit


    Sub ExcelDiet()

    Dim j As Long
    Dim k As Long
    Dim LastRow As Long
    Dim LastCol As Long
    Dim ColFormula As Range
    Dim RowFormula As Range
    Dim ColValue As Range
    Dim RowValue As Range
    Dim Shp As Shape
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    On Error Resume Next

    For Each ws In Worksheets
    With ws
    'Find the last used cell with a formula and value
    'Search by Columns and Rows
    On Error Resume Next
    Set ColFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    Set ColValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    Set RowFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Set RowValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    On Error Goto 0

    'Determine the last column
    If ColFormula Is Nothing Then
    LastCol = 0
    Else
    LastCol = ColFormula.Column
    End If
    If Not ColValue Is Nothing Then
    LastCol = Application.WorksheetFunction.Max(LastCol, ColValue.Column)
    End If

    'Determine the last row
    If RowFormula Is Nothing Then
    LastRow = 0
    Else
    LastRow = RowFormula.Row
    End If
    If Not RowValue Is Nothing Then
    LastRow = Application.WorksheetFunction.Max(LastRow, RowValue.Row)
    End If

    'Determine if any shapes are beyond the last row and last column
    For Each Shp In .Shapes
    j = 0
    k = 0
    On Error Resume Next
    j = Shp.TopLeftCell.Row
    k = Shp.TopLeftCell.Column
    On Error Goto 0
    If j > 0 And k > 0 Then
    Do Until .Cells(j, k).Top > Shp.Top + Shp.Height
    j = j + 1
    Loop
    If j > LastRow Then
    LastRow = j
    End If
    Do Until .Cells(j, k).Left > Shp.Left + Shp.Width
    k = k + 1
    Loop
    If k > LastCol Then
    LastCol = k
    End If
    End If
    Next

    .Range(Cells(1, LastCol + 1).Address & ":IV65536").Delete
    .Range(Cells(LastRow + 1, 1).Address & ":IV65536").Delete
    End With
    Next

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub
    -u

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can create Dynamic Ranges using the Offset function
    =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),1)
    Is this what you're after?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •