VBA, If Cells Blank End Sub

MrMaker

Board Regular
Joined
Jun 7, 2018
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

A simple one no doubt........I have inherited the below code and would like to amend to include a statement at the beginning to echo the following: IF CELL Z1 ON SHEET 6 IS BLANK THEN END SUB, OTHERWISE DO ALL THE BELOW.

Can anyone advise please?

Thanks very much in advance

VBA Code:
Private Sub Workbook_Open()
Dim Ws As Worksheet
Dim Crit As String
Application.ScreenUpdating = False
Crit = Sheets("SHEET1").Range("A1").Value
For Each Ws In Worksheets
Select Case Ws.Name
Case "SHEET2", "SHEET3"
Ws.UsedRange.AutoFilter 1, "<>" & Crit
Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
Ws.AutoFilterMode = False
Ws.Range("A2:A" & Ws.Rows.Count).RowHeight = 25
Ws.Activate
Ws.Cells.Copy
Ws.Range("A1").PasteSpecial Paste:=xlValues
Ws.Range("A1").Select
Application.CutCopyMode = False
End Select
Select Case Ws.Name
Case "SHEET4"
Ws.Range("A1").Select
End Select
Next Ws
With Sheets("SHEET1")
.Range("A1").Value = Crit
.Rows.RowHeight = 36
.Visible = xlVeryHidden
Worksheets("Summary").Activate
ThisWorkbook.Protect Password:="123", Structure:=True, Windows:=True
End With
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Verify that the sheet name is correct "SHEET6".
Add the following line to the macro, it is highlighted in blue.

Rich (BB code):
Private Sub Workbook_Open()
Dim Ws As Worksheet
Dim Crit As String
Application.ScreenUpdating = False

If Sheets("SHEET6").Range("Z1").Value = "" Then Exit Sub

Crit = Sheets("SHEET1").Range("A1").Value
For Each Ws In Worksheets
Select Case Ws.Name
Case "SHEET2", "SHEET3"
Ws.UsedRange.AutoFilter 1, "<>" & Crit
Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
Ws.AutoFilterMode = False
Ws.Range("A2:A" & Ws.Rows.Count).RowHeight = 25
Ws.Activate
Ws.Cells.Copy
Ws.Range("A1").PasteSpecial Paste:=xlValues
Ws.Range("A1").Select
Application.CutCopyMode = False
End Select
Select Case Ws.Name
Case "SHEET4"
Ws.Range("A1").Select
End Select
Next Ws
With Sheets("SHEET1")
.Range("A1").Value = Crit
.Rows.RowHeight = 36
.Visible = xlVeryHidden
Worksheets("Summary").Activate
ThisWorkbook.Protect Password:="123", Structure:=True, Windows:=True
End With
End Sub
 
Upvote 0
Solution
Easy when you know how :biggrin:
Thanks very much for your really quick help!
All the best
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top