fix error 1004 when the sheet is protected

kar2rost

Banned user
Joined
Jul 6, 2022
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
Hello and have a good time
I want to record a text value in column b with this button


VBA Code:
Sub Button37_Click()

 ActiveCell.Value = Range("h1").Value
End Sub

I have locked all cells of the sheet
And it is locked in another column after registering the sheet number by this code

VBA Code:
Sub commandbutton_click5()
  Dim sh As Worksheet
  Dim f As Range
 
  Set sh = ThisWorkbook.Sheets("sheet1")
  Set f = sh.Range("A:A").Find(sh.Range("f7"), , xlValues, xlWhole, , , False)
 
  If f Is Nothing Then
    sh.Unprotect
    With sh.Range("A" & sh.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row)
      .Value = sh.Range("f7").Value
      .Offset(0, 1).Select
      .Locked = True
    End With
    sh.Protect
  Else
    MsgBox "this number is available"
    f.Select
  End If
End Sub

I want to first if accidentally on one of the cells of column a
It was clicked and then on button 37, it will give a message that only numbers can be entered and dont allowed and value in the cell dont be clean

Secondly, after the sheet is locked, if a cell on the page is accidentally clicked and button 37 is pressed, it should not give error 1004.
Can I do this?

Is there a way to make the buttons work only in the specified range and do nothing in other ranges?
Thankful
 

Attachments

  • 0.jpg
    0.jpg
    35.3 KB · Views: 11
  • 1.jpg
    1.jpg
    43.2 KB · Views: 10
  • 2.jpg
    2.jpg
    32.1 KB · Views: 11
  • solution 2.jpg
    solution 2.jpg
    57.9 KB · Views: 11
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The first part is not entirely clear, but I expect you want to just test the cell is numeric

VBA Code:
Sub abcd()
With Range("H1")
    If IsNumeric(.Value) Then
        ActiveCell.Value = .Value
    End If
End With
End Sub

You cannot use find on a protected sheet, so in your second macro move the unprotect earlier in the procedure, so before you set your two variables.

Yes, you can specify a range of cells for the code to work in using intersect

VBA Code:
Sub defg()
If Not Intersect(ActiveCell, Range("A2:G20")) Is Nothing Then
    ' code to execute if the activecell is within A2:G20
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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