Vba protect row or range

paroduso

Board Regular
Joined
Sep 11, 2013
Messages
103
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone...

I need help with this macro i found here...
This macro in my case is working only 50% because only protects cells with values...how can i do to protect empty cells in the same row?

i tried

`If wcell.Value = "" Then wcell.Locked = True`

But protects all my range

Macro:

Option Explicit
Public wr As Range, wcell As Range, fr As Range, ws As Worksheet

' this code goes at ThisWorkbook module <--------

Private Sub Workbook_Activate()
Initial
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Set wr = ws.Range("a1:w" & LastRow(Me.Name, "Enter Data Here"))

For Each wcell In wr
Select Case wcell.Interior.ColorIndex
Case xlNone
' no action
Case Else
wcell.Interior.ColorIndex = xlNone
If wcell.Value <> "" Then wcell.Locked = True ' no more changes
End Select
Next
End Sub

Private Sub Workbook_Open()
Initial
End Sub

Sub Initial()

Set ws = ThisWorkbook.Worksheets("Enter Data Here")
ws.Protect Password:="pw", userinterfaceonly:=True
Set wr = ws.Range("a1:w" & LastRow(Me.Name, "Enter Data Here"))
wr.Interior.ColorIndex = xlNone

For Each wcell In wr
wcell.Locked = False
If wcell.Value <> "" Then wcell.Locked = True
Next

End Sub

Public Function LastRow(wname$, which$) As Long
Workbooks(wname).Sheets(which).Activate
If WorksheetFunction.CountA(Cells) = 0 Then
LastRow = 0
Exit Function
End If
LastRow = Cells.Find(What:="*", After:=[a1], SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
End Function

Thank you all
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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