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