Results 1 to 3 of 3

Help modify code to auto-fit multiple named ranges

Threaded View

onmyway Help modify code to auto-fit... 02-24-2015, 09:37 AM
onmyway Re: Help modify code to... 02-24-2015, 12:31 PM
onmyway Re: Help modify code to... 02-24-2015, 01:06 PM
  1. #1
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Help modify code to auto-fit multiple named ranges

    Hi guys,

    I need some help auto fitting text in some merged cells.

    I got the following code, thanks to Debra Dalgleish (http://blog.contextures.com/archives...ll-row-height/), that works great for a single named range. But, the problem is that I have multiple named ranges I would like to auto fit on the same sheet.

    Is there perhaps a way i can do this? perhaps a loop or something? from either one range name to the next, or loop from row to row?

    My ranges are named DelayDetail; DelayDetail2; DelayDetail3 etc.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim MergeWidth As Single
    Dim cM As Range
    Dim AutoFitRng As Range
    Dim CWidth As Double
    Dim NewRowHt As Double
    Dim str01 As String
    str01 = "DelayDetail"
    
      If Not Intersect(Target, Range(str01)) Is Nothing Then
        Application.ScreenUpdating = False
        On Error Resume Next
        Set AutoFitRng = Range(Range(str01).MergeArea.Address)
    
        With AutoFitRng
          .MergeCells = False
          CWidth = .Cells(1).ColumnWidth
          MergeWidth = 0
          For Each cM In AutoFitRng
              cM.WrapText = True
              MergeWidth = cM.ColumnWidth + MergeWidth
          Next
          'small adjustment to temporary width
          MergeWidth = MergeWidth + AutoFitRng.Cells.count * 0.66
          .Cells(1).ColumnWidth = MergeWidth
          .EntireRow.AutoFit
          NewRowHt = .RowHeight
          .Cells(1).ColumnWidth = CWidth
          .MergeCells = True
          .RowHeight = NewRowHt
        End With
        Application.ScreenUpdating = True
      End If
    
    End Sub
    Thank you!
    Last edited by onmyway; 02-24-2015 at 01:06 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Auto updating Charts with Dynamically named Ranges
    By Xiophoid in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 03-19-2013, 01:18 PM
  2. Replies: 0
    Last Post: 10-15-2012, 07:24 AM
  3. [SOLVED] Modify Auto Clear - VB Code
    By SVTF in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2012, 02:31 PM
  4. Code for Multiple Named Ranges for Data Validation
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2011, 12:57 PM
  5. Named ranges-How would I write the code for that?
    By Steve C in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-10-2005, 04:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1