VBA code protected cells working with clear code.

Anonymous
2021-11-16T23:07:56+00:00

I have a template with tab sheet1 and sheet2. In sheet1, there is a command2 button to filter data and fillout column A to F in sheet2. and there is refresh button in sheet2 to refresh query table in column H to pulling data from sheet1.

I wonder if there is a code to protected the sheet2 column A to F and H to prevent user accident add/edit or delete data. But it should allow command2 button and refresh button to clear and filling out new information.

Here is the code for command2 button.

Private Sub CommandButton2_Click()

Dim UserRange As Range

Sheets("SHEET2").Range("A:A,C:C,E:E,H3:H100").Clear

Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("SHEET1")

On Error Resume Next

Set UserRange = Application.Range("E23:E500")

On Error GoTo 0

If UserRange Is Nothing Then

    Exit Sub

End If       

UserRange.AdvancedFilter xlFilterCopy, CopyToRange:=Sheets("SHEET2").Range("A1"), Unique:=True

'------------------

Set sh = ThisWorkbook.Sheets("SHEET1")

On Error Resume Next

Set UserRange = Application.Range("H23:H500")

On Error GoTo 0

If UserRange Is Nothing Then

    Exit Sub

End If

UserRange.AdvancedFilter xlFilterCopy, CopyToRange:=Sheets("SHEET2").Range("C1"), Unique:=True

'------------------

Set sh = ThisWorkbook.Sheets("SHEET1")

On Error Resume Next

Set UserRange = Application.Range("K23:K500")

On Error GoTo 0

If UserRange Is Nothing Then

    Exit Sub

End If       

UserRange.AdvancedFilter xlFilterCopy, CopyToRange:=Sheets("SHEET2").Range("E1"), Unique:=True

End Sub

Here is the refresh VBA Code

Sub RefreshAll()

ActiveWorkbook.RefreshAll

End Sub

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Newest
  1. HansV 460.5K Reputation points MVP Volunteer Moderator
    2021-11-17T22:48:21+00:00

    Congratulations on finding the solution!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-11-17T22:40:14+00:00

    I found out the issue. I just need to disable background refresh in query then it is working.

    Was this answer helpful?

    0 comments No comments
  3. HansV 460.5K Reputation points MVP Volunteer Moderator
    2021-11-17T22:20:59+00:00

    The error message implies that there is a protected sheet when you try to refresh all.

    Since the code already unprotects Sheet2, the error message must refer to another sheet...

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-11-17T22:18:40+00:00

    the msg "The cell or chart you're trying to change is on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password"

    The refresh logo button to refresh all page. It will refresh the query table in sheet2 to reflect data in sheet1 table.

    Was this answer helpful?

    0 comments No comments
  5. HansV 460.5K Reputation points MVP Volunteer Moderator
    2021-11-17T22:09:47+00:00

    Do you get an error message when you click the refresh button? If so, what does it say?

    Was this answer helpful?

    0 comments No comments