Clear contents with cell count

mjtolent

New Member
Joined
Oct 1, 2020
Messages
14
Office Version
  1. 2013
Platform
  1. Windows
Hi,

This is a frustrating one as I can't understand why my code isn't working. I've tried several different variants of below, to no avail.

Basically, I'm trying to clear the contents of cell C23, if there are less than 4 values in a range. Cell Z500 is using a count formula to count the values in the range. But no matter what the contents are cleared from cell C23 when the file is opened. I've tried using an Else clause, but I'm getting an error regarding no 'Block If'

Thanks in advance
Mj

VBA Code:
Private Sub Workbook_Open()

Dim myrange1 As Range
Set myrange1 = Sheets("Contract Award").Range("Z500")
If myrange1 < 4 Then Sheets("Contract Award").Select
Range("$C$23").ClearContents
Range("A1").Select
    

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi, and Welcome to Mr. Excel!

Untested, so test on a COPY of your WB first!

VBA Code:
Private Sub Workbook_Open()

Dim myrange1 As Range
Set myrange1 = Sheets("Contract Award").Range("Z500")
If myrange1.value < 4 Then Sheets("Contract Award").Select
Range("$C$23").ClearContents
Range("A1").Select
End if
End Sub
 
Upvote 0
Actually, this is better...

VBA Code:
Private Sub Workbook_Open()
Dim myrange1 As Range
Set myrange1 = Sheets("START").Range("Z500")
If myrange1.Value < 4 Then
    With Sheets("Contract Award")
        .Range("$C$23").ClearContents
        .Range("A1").Select
    End With
End If
 
Upvote 0
VBA Code:
Private Sub Workbook_Open()
    Dim myrange1 As Range
    Set myrange1 = Sheets("Contract Award").Range("Z500")
    If myrange1.Value < 4 Then
        Sheets("Contract Award").Select
        Range("$C$23").ClearContents
        Range("A1").Select
    End If
End Sub
 
Upvote 0
Such a small change, but works a treat.
Many thanks for taking the time.
MJ
 
Upvote 0
Pleasure - whichever solution you used.
 
Upvote 0
Pleasure - whichever solution you used.
I think mohadin posted because the code in post number 3 will error at
VBA Code:
.Range("A1").Select
if the "Contract Award" sheet isn't the ActiveSheet when the workbook is opened.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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