A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Congratulations on finding the solution!
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
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.
Congratulations on finding the solution!
I found out the issue. I just need to disable background refresh in query then it is working.
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...
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.