Consulting

Results 1 to 5 of 5

Thread: Event macro for auto-fit row height of merged cells

  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    7
    Location

    Event macro for auto-fit row height of merged cells

    I have the following code for auto-fitting the row height of a merged cell. I want the macro to run automatically after a user has entered text and have no experience writing this kind of macro (today is the first time I have actually created and run a macro) so I would need very basic instructions.


    [VBA]''Simulates row height autofit for a merged cell if the active cell..
    '' is merged.
    '' has Wrap Text set.
    '' includes only 1 row.
    ''Unlike real autosizing the macro only increases row height
    '' (if needed). It does not reduce row height because another
    '' merged cell on the same row may needed a greater height
    '' than the active cell.
    Sub AutoFitMergedCellRowHeight()
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    If ActiveCell.MergeCells Then
    With ActiveCell.MergeArea
    If .Rows.Count = 1 And .WrapText = True Then
    Application.ScreenUpdating = False
    CurrentRowHeight = .RowHeight
    ActiveCellWidth = ActiveCell.ColumnWidth
    For Each CurrCell In Selection
    MergedCellRgWidth = CurrCell.ColumnWidth +
    MergedCellRgWidth
    Next
    .MergeCells = False
    .Cells(1).ColumnWidth = MergedCellRgWidth
    .EntireRow.AutoFit
    PossNewRowHeight = .RowHeight
    .Cells(1).ColumnWidth = ActiveCellWidth
    .MergeCells = True
    .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
    CurrentRowHeight, PossNewRowHeight)
    End If
    End With
    End If
    End Sub [/VBA]

    Regards,
    Rackle
    Last edited by Bob Phillips; 09-15-2011 at 10:59 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use worksheet change

    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1:H5" '<<<< change to suit

    On Error GoTo ws_exit

    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

    With Target

    'do your stuff here on Target
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub[/vba]

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2011
    Posts
    7
    Location
    is it supposed to look like this? I inserted my original code over the green target message.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1:H5" '<<<< change to suit

    On Error GoTo ws_exit

    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

    With Target

    ''Simulates row height autofit for a merged cell if the active cell..
    '' is merged.
    '' has Wrap Text set.
    '' includes only 1 row.
    ''Unlike real autosizing the macro only increases row height
    '' (if needed). It does not reduce row height because another
    '' merged cell on the same row may needed a greater height
    '' than the active cell.
    Sub AutoFitMergedCellRowHeight()
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    If ActiveCell.MergeCells Then
    With ActiveCell.MergeArea
    If .Rows.Count = 1 And .WrapText = True Then
    Application.ScreenUpdating = False
    CurrentRowHeight = .RowHeight
    ActiveCellWidth = ActiveCell.ColumnWidth
    For Each CurrCell In Selection
    MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
    Next
    .MergeCells = False
    .Cells(1).ColumnWidth = MergedCellRgWidth
    .EntireRow.AutoFit
    PossNewRowHeight = .RowHeight
    .Cells(1).ColumnWidth = ActiveCellWidth
    .MergeCells = True
    .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
    CurrentRowHeight, PossNewRowHeight)
    End If
    End With
    End If
    End Sub
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, you just insert the bit of code to work on a single row. I didn't look hard enough to try and figure it all out, I assumed you knew what it is doing.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Sep 2011
    Posts
    7
    Location
    I know nothing about this stuff. I have two codes now, my original code and one you have given me. I understand where to put yours but do I need to alter it based on what looks like instructions you inserted in green? Where do I put my original code?

Posting Permissions

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