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: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. 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
  3. Anonymous
    2021-11-17T21:07:14+00:00

    There are three tabs in the workbook. but they are not protected. Only the one in sheet2 is protected by the code you provided above.

    Was this answer helpful?

    0 comments No comments
  4. HansV 460.5K Reputation points MVP Volunteer Moderator
    2021-11-17T19:42:17+00:00

    Do you have other protected sheets in the workbook? If so, you should unprotect and protect them too.

    Was this answer helpful?

    0 comments No comments
  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more