Hello!
I've find the following code which can autofit all protected/unprotected row with merged cells and text wrap on.
(Macro is taken from https://www.mrexcel.com/board/thread...-cells.556709/)
Code: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 ProtectStatus As Boolean With Target If .MergeCells And .WrapText Then ProtectStatus = Me.ProtectContents If ProtectStatus Then Me.Unprotect "password" Set c = Target.Cells(1, 1) cWdth = c.ColumnWidth Set ma = c.MergeArea For Each cc In ma.Cells MrgeWdth = MrgeWdth + cc.ColumnWidth Next Application.ScreenUpdating = False On Error Resume 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 On Error GoTo 0 Application.ScreenUpdating = True If ProtectStatus Then Me.Protect "password" End If End With End Sub
Requirement:
The code should be applied to some specific range only, not in the whole sheet.
Alternatively,
I want to escape some merged cells from autofit even if the wrap text is on for these cells.
I tried to solve this problem by myself and added a simple line in the code (just before the line With Target)
and it works.Code:If Not Intersect(Target, Range("B35:Z35", "D44:R44")) Is Nothing Then
But, as soon as I added the third range,
it stopped working by highlighting the word Range and with the pop up-Code:If Not Intersect(Target, Range("B35:Z35", "D44:R44", "G55:Z55")) Is Nothing Then
"Compile Error: Wrong number of argument or invalid property assignment"
I know, I'm wrong, but where, I don't know.
Thanks!
(This thread is also cross posted at https://www.mrexcel.com/board/thread...-only.1145918/)




Reply With Quote
Bookmarks