how do I create event-activated macros that check every cell?

J

JLC

I'm so thankful for Jim Rech's code* (and those who directed me to it)
that allows auto-expansion of merged cells, but I have a *shared
workbook *with at least *10 sheets*, each of which has more than *40
different merged cells *that may need auto-expansion. Because there are
so many merged cells, not to mention Excel-phobic users typing into
them, I want to avoid personally running this macro for each cell that
needs expansion. Unfortunately, my _very_ rudimentary VBA skills are of
no help.

Given that, I have several questions:


- Can I have the macro run automatically for each sheet, checking all
the merged cells (or simply all cells), when the user saves or exits?

- Can I have the macro check every single cell in the workbook? I'd
like to run this version manually.

- Can the macro activation event be when the user has finished
entering text and moves to a new cell whether by


- tabbing,
- hitting enter, or
- clicking on the next cell they want to modify?
If so, does anyone know how expensive this would be in terms of time
and memory? All my users are accessing the workbook remotely and
already have a good deal of lag due to outdated equipment.


*'Jim Rech's AutoFitMergedCellRowHeight Code'
(http://tinyurl.com/aknxy)
 
B

Bob Phillips

Is this any good for you

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1:H10"

On Error GoTo ws_exit:
Application.EnableEvents = FalseSub AutoFitMergedCellRowHeight()
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
Dim cell As Range
Dim sh As Worksheet

If Target.MergeCells Then
With Target.MergeArea
If .Rows.Count = 1 And .Cells(1).WrapText = True Then
''<<.Cells(1) added
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
ws_exit:
Application.EnableEvents = True
End Sub

'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.


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
J

JLC

That works exactly how I'd hoped -except- that it changes the width o
the first column in the merged cell. Can we change the code so that i
doesn't alter column widths?

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)

I can't wait until I'm able to do some of this on my own... ;
 
J

JLC

Nevermind. When I turn on sheet protection and allow formatting of cell
row, and column, the macro works perfectly. Of course, I don't want m
users actually doing any formatting. If they figure out they can chang
things, there's no telling what will happen. Any suggestions about ho
to skirt this issue?

Thanks so much!
j
 
B

Bob Phillips

If you turn full protection on with a password, and then in the code, turn
it off, do your stuff, and then turn it back on.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top