RoastandBrew
New Member
- Joined
- Mar 11, 2019
- Messages
- 1
Hi,
I am new the VBA and am having an issue with autofilters. I have the below code to lock the entire workbook:
Sub Lock_Workbook()
'Macro used to lock all worksheets within the workbook
'Code hides the activiation of all worksheets involved in the macro.
On Error GoTo errHandler
Application.ScreenUpdating = False
Dim pwd1 As String, pwd2 As String
pwd1 = InputBox("Please Enter the password")
If pwd1 = "" Then Exit Sub
pwd2 = InputBox("Please re-enter the password")
If pwd2 = "" Then Exit Sub
'Check if both the passwords are identical
If InStr(1, pwd2, pwd1, 0) = 0 Or _
InStr(1, pwd1, pwd2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken"
Exit Sub
End If
For Each ws In Worksheets
'UserInterfaceOnly allows VBA to paste data into locked cells
ws.Protect Password:=pwd1, UserInterfaceOnly:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
Next
'Code hides the activiation of all worksheets involved in the macro.
Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
'Message box appears if the macro successfully finishes
MsgBox "All sheets Protected."
Exit Sub
End Sub
This code seems to work fine, but when I run the below code, it bombs out at the autofilter line (Red Text). I have found that if I open the file, run the lock macro then the below code works fine. Anybody know why it would bomb out?
Sub Update_Labor_Cost_and_Production_Report()
'Macro used to update the Labor Cost & Prod Rep. Worksheet with all active phase codes with a forecasting method of "P"
'Code hides the activiation of all worksheets involved in the macro.
On Error GoTo errHandler
Application.ScreenUpdating = False
'Sorts & Copy and Pastes all "P" Forecasting Methods from the BCSQ Worksheet into the the Paste tab and then into the Labor Cost & Prod. Rep. Worksheet
Sheets("BCSQ").Visible = True
Sheets("Paste").Visible = True
Sheets("Labor Cost & Prod. Rep.").Select
Cells.Select
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
Sheets("BCSQ").Select
Cells.Select
On Error Resume Next
On Error GoTo 0
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Add Key:=Range("A2:A1000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Add Key:=Range("B2:B1000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("BCSQ").Sort
.SetRange Range("A1:BB600")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$1:$G$600").AutoFilter Field:=3, Criteria1:="P"
Range("A1").Select
Range("A1:B600,F1:G600").Select
Range("F1").Activate
Selection.Copy
Sheets("Paste").Select
Cells.Select
Application.CutCopyMode = False
Selection.ClearContents
Sheets("BCSQ").Select
Selection.Copy
Sheets("Paste").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A2:D246").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Labor Cost & Prod. Rep.").Select
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Range("$A$10:$BT$255").AutoFilter Field:=1, Criteria1:="<>"
Sheets("Paste").Select
Cells.Select
Range("A38").Activate
Application.CutCopyMode = False
Selection.ClearContents
Sheets("Labor Cost & Prod. Rep.").Select
'Hides the BCSQ and Paste Worksheets
Sheets("BCSQ").Visible = False
Sheets("Paste").Visible = False
'Filters the Job# column within the Labor Cost & Prod. Rep. Worksheet to remove blank cells
Sheets("Weekly Earned Labor Hours").Select
ActiveSheet.Range("$A$10:$BT$255").AutoFilter Field:=1, Criteria1:="<>"
'Activates the Macros worksheet
Sheets("Macros").Select
'Code hides the activiation of all worksheets involved in the macro.
Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
'Message box appears if the macro successfully finishes
MsgBox "Update Complete!"
End Sub
Thanks for the help!
I am new the VBA and am having an issue with autofilters. I have the below code to lock the entire workbook:
Sub Lock_Workbook()
'Macro used to lock all worksheets within the workbook
'Code hides the activiation of all worksheets involved in the macro.
On Error GoTo errHandler
Application.ScreenUpdating = False
Dim pwd1 As String, pwd2 As String
pwd1 = InputBox("Please Enter the password")
If pwd1 = "" Then Exit Sub
pwd2 = InputBox("Please re-enter the password")
If pwd2 = "" Then Exit Sub
'Check if both the passwords are identical
If InStr(1, pwd2, pwd1, 0) = 0 Or _
InStr(1, pwd1, pwd2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken"
Exit Sub
End If
For Each ws In Worksheets
'UserInterfaceOnly allows VBA to paste data into locked cells
ws.Protect Password:=pwd1, UserInterfaceOnly:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
Next
'Code hides the activiation of all worksheets involved in the macro.
Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
'Message box appears if the macro successfully finishes
MsgBox "All sheets Protected."
Exit Sub
End Sub
This code seems to work fine, but when I run the below code, it bombs out at the autofilter line (Red Text). I have found that if I open the file, run the lock macro then the below code works fine. Anybody know why it would bomb out?
Sub Update_Labor_Cost_and_Production_Report()
'Macro used to update the Labor Cost & Prod Rep. Worksheet with all active phase codes with a forecasting method of "P"
'Code hides the activiation of all worksheets involved in the macro.
On Error GoTo errHandler
Application.ScreenUpdating = False
'Sorts & Copy and Pastes all "P" Forecasting Methods from the BCSQ Worksheet into the the Paste tab and then into the Labor Cost & Prod. Rep. Worksheet
Sheets("BCSQ").Visible = True
Sheets("Paste").Visible = True
Sheets("Labor Cost & Prod. Rep.").Select
Cells.Select
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
Sheets("BCSQ").Select
Cells.Select
On Error Resume Next
On Error GoTo 0
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Add Key:=Range("A2:A1000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("BCSQ").Sort.SortFields.Add Key:=Range("B2:B1000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("BCSQ").Sort
.SetRange Range("A1:BB600")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$1:$G$600").AutoFilter Field:=3, Criteria1:="P"
Range("A1").Select
Range("A1:B600,F1:G600").Select
Range("F1").Activate
Selection.Copy
Sheets("Paste").Select
Cells.Select
Application.CutCopyMode = False
Selection.ClearContents
Sheets("BCSQ").Select
Selection.Copy
Sheets("Paste").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A2:D246").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Labor Cost & Prod. Rep.").Select
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Range("$A$10:$BT$255").AutoFilter Field:=1, Criteria1:="<>"
Sheets("Paste").Select
Cells.Select
Range("A38").Activate
Application.CutCopyMode = False
Selection.ClearContents
Sheets("Labor Cost & Prod. Rep.").Select
'Hides the BCSQ and Paste Worksheets
Sheets("BCSQ").Visible = False
Sheets("Paste").Visible = False
'Filters the Job# column within the Labor Cost & Prod. Rep. Worksheet to remove blank cells
Sheets("Weekly Earned Labor Hours").Select
ActiveSheet.Range("$A$10:$BT$255").AutoFilter Field:=1, Criteria1:="<>"
'Activates the Macros worksheet
Sheets("Macros").Select
'Code hides the activiation of all worksheets involved in the macro.
Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
'Message box appears if the macro successfully finishes
MsgBox "Update Complete!"
End Sub
Thanks for the help!