XL 2003 VBA: Merge a Macro Code with a Worksheet Code

TechTank

Board Regular
Joined
Sep 5, 2011
Messages
92
Hi All,

I'm hoping someone can help with the following scenario:

I have a worksheet code that "auto-height's" a row with merged cells. The work sheet is protected with the cells required unlocked. However, when it executes it seems to lock the cell that contains the data and prevents users from deleting the row should they wish to do so. The code I'm using is:

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
ActiveSheet.Unprotect
With Target
If .MergeCells And .WrapText Then
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
     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
End If
End With
Application.ScreenUpdating = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowInsertingRows:=True, AllowDeletingRows:=True
    ActiveSheet.EnableSelection = xlUnlockedCells
    
End Sub

To get around this I can manually execute this macro:

Code:
Option Explicit
Sub Unlock_Cells()
ActiveSheet.Unprotect
    Range("D" & Cells.Rows.Count).End(xlUp).Select
        Selection.Locked = False
        Selection.FormulaHidden = False
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowInsertingRows:=True, AllowDeletingRows:=True
    ActiveSheet.EnableSelection = xlUnlockedCells
    
End Sub

I would, however, like to incorporate this so that once the Worksheet code has executed it runs the Macro code as well. I haven't been able to debug the 1st set of code to stop it from setting this cell to locked. (In the properties for the locked cell the box is shaded green for "Locked" and not the full "green tick" if you follow me.)

Can anyone help me merge the two codes together or know how to get the Worksheet code to "call" the macro?

My VBA skills are limited and I'm picking up a lot from the members here and trying to understand the code I'm finding.

Thank you for your time.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try putting

Code:
Call Unlock_Cells

at the appropriate point of your worksheet code.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top