+ Reply to Thread
Results 1 to 5 of 5

Auto fit in merged cells

  1. #1
    LDF
    Guest

    Auto fit in merged cells

    I am trying to paste in the code to ensure the merged cells in my spreadsheet
    autofit the text that is written.
    I have followed previous queries on the subject, and used the code provided.
    Never having attempted this before, of course it isn't working and I don't
    know why.
    Any help would be appreciated

  2. #2
    Norman Jones
    Guest

    Re: Auto fit in merged cells

    Hi LDF,

    See David McRitchie's 'Getting Started With Macros And User Defined
    Functions' at:

    http://www.mvps.org/dmcritchie/excel/getstarted.htm



    ---
    Regards,
    Norman


    "LDF" <[email protected]> wrote in message
    news:[email protected]...
    >I am trying to paste in the code to ensure the merged cells in my
    >spreadsheet
    > autofit the text that is written.
    > I have followed previous queries on the subject, and used the code
    > provided.
    > Never having attempted this before, of course it isn't working and I don't
    > know why.
    > Any help would be appreciated




  3. #3
    Tom Ogilvy
    Guest

    Re: Auto fit in merged cells

    Perhaps this isn't your problem, but just to be sure:
    Assuming you are using Jim Rech's code, you have to run the code for it to
    do anything. Just putting it in a module does not cause it to autofit
    merged cells.

    Also, this line
    If .Rows.Count = 1 And .WrapText = True Then
    tells you that it only works for single row, multiple column merged cells
    (with WrapText set to true)

    --
    regards,
    Tom Ogilvy



    "LDF" <[email protected]> wrote in message
    news:[email protected]...
    > I am trying to paste in the code to ensure the merged cells in my

    spreadsheet
    > autofit the text that is written.
    > I have followed previous queries on the subject, and used the code

    provided.
    > Never having attempted this before, of course it isn't working and I don't
    > know why.
    > Any help would be appreciated




  4. #4
    Joe
    Guest

    Re: Auto fit in merged cells

    Is there any reason why this code would work fine in Excel 2003 but not in
    Excel 2000?

    Thanks very much,
    Joe

    Dim OldRng As Range ' DECLARED THIS AT TOP OF MODULE

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NewRwHt As Single
    Dim cWdth As Single, MrgeWdth As Single
    Dim c As Range, cc As Range
    Dim ma As Range
    Dim Protected As Boolean

    If Target.Column = 1 And (Target.Row > 16 And Target.Row < 42) Then
    Cells(Target.Row, 2).Select
    End If
    Protected = False
    Set c = Cells(Target.Row, 1)
    If OldRng Is Nothing Then Set OldRng = c
    If Not Intersect(OldRng, c) Is Nothing Then
    Application.ScreenUpdating = True
    If Me.ProtectContents Then
    Protected = True
    Me.Unprotect
    End If
    cWdth = c.ColumnWidth
    Set ma = c.MergeArea
    For Each cc In ma.Cells
    MrgeWdth = MrgeWdth + cc.ColumnWidth
    Next
    ma.MergeCells = False
    c.ColumnWidth = MrgeWdth
    c.EntireRow.AutoFit
    NewRwHt = c.RowHeight
    c.ColumnWidth = cWdth
    ma.MergeCells = True
    ma.RowHeight = NewRwHt
    cWdth = 0: MrgeWdth = 0
    ma.Locked = False
    If Protected Then Me.Protect
    Application.ScreenUpdating = True
    End If
    Set OldRng = Target
    Set c = Nothing
    Set OldRng = Nothing
    End Sub

    "Tom Ogilvy" wrote:

    > Perhaps this isn't your problem, but just to be sure:
    > Assuming you are using Jim Rech's code, you have to run the code for it to
    > do anything. Just putting it in a module does not cause it to autofit
    > merged cells.
    >
    > Also, this line
    > If .Rows.Count = 1 And .WrapText = True Then
    > tells you that it only works for single row, multiple column merged cells
    > (with WrapText set to true)
    >
    > --
    > regards,
    > Tom Ogilvy
    >
    >
    >
    > "LDF" <[email protected]> wrote in message
    > news:[email protected]...
    > > I am trying to paste in the code to ensure the merged cells in my

    > spreadsheet
    > > autofit the text that is written.
    > > I have followed previous queries on the subject, and used the code

    > provided.
    > > Never having attempted this before, of course it isn't working and I don't
    > > know why.
    > > Any help would be appreciated

    >
    >
    >


  5. #5
    Tom Ogilvy
    Guest

    Re: Auto fit in merged cells

    It was originally written in the xl97/xl2000 timeframe, so I don't see any
    reason it would not work in all versions after xl95 (merged cells were
    introduced in xl97).

    --
    Regards,
    Tom Ogilvy


    "Joe" <[email protected]> wrote in message
    news:[email protected]...
    > Is there any reason why this code would work fine in Excel 2003 but not in
    > Excel 2000?
    >
    > Thanks very much,
    > Joe
    >
    > Dim OldRng As Range ' DECLARED THIS AT TOP OF MODULE
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > Dim NewRwHt As Single
    > Dim cWdth As Single, MrgeWdth As Single
    > Dim c As Range, cc As Range
    > Dim ma As Range
    > Dim Protected As Boolean
    >
    > If Target.Column = 1 And (Target.Row > 16 And Target.Row < 42) Then
    > Cells(Target.Row, 2).Select
    > End If
    > Protected = False
    > Set c = Cells(Target.Row, 1)
    > If OldRng Is Nothing Then Set OldRng = c
    > If Not Intersect(OldRng, c) Is Nothing Then
    > Application.ScreenUpdating = True
    > If Me.ProtectContents Then
    > Protected = True
    > Me.Unprotect
    > End If
    > cWdth = c.ColumnWidth
    > Set ma = c.MergeArea
    > For Each cc In ma.Cells
    > MrgeWdth = MrgeWdth + cc.ColumnWidth
    > Next
    > ma.MergeCells = False
    > c.ColumnWidth = MrgeWdth
    > c.EntireRow.AutoFit
    > NewRwHt = c.RowHeight
    > c.ColumnWidth = cWdth
    > ma.MergeCells = True
    > ma.RowHeight = NewRwHt
    > cWdth = 0: MrgeWdth = 0
    > ma.Locked = False
    > If Protected Then Me.Protect
    > Application.ScreenUpdating = True
    > End If
    > Set OldRng = Target
    > Set c = Nothing
    > Set OldRng = Nothing
    > End Sub
    >
    > "Tom Ogilvy" wrote:
    >
    > > Perhaps this isn't your problem, but just to be sure:
    > > Assuming you are using Jim Rech's code, you have to run the code for it

    to
    > > do anything. Just putting it in a module does not cause it to autofit
    > > merged cells.
    > >
    > > Also, this line
    > > If .Rows.Count = 1 And .WrapText = True Then
    > > tells you that it only works for single row, multiple column merged

    cells
    > > (with WrapText set to true)
    > >
    > > --
    > > regards,
    > > Tom Ogilvy
    > >
    > >
    > >
    > > "LDF" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > I am trying to paste in the code to ensure the merged cells in my

    > > spreadsheet
    > > > autofit the text that is written.
    > > > I have followed previous queries on the subject, and used the code

    > > provided.
    > > > Never having attempted this before, of course it isn't working and I

    don't
    > > > know why.
    > > > Any help would be appreciated

    > >
    > >
    > >




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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