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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,785
Office Version
  1. 365
Platform
  1. Windows
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
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,785
Office Version
  1. 365
Platform
  1. Windows
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
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
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
 

mjtolent

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

ADVERTISEMENT

Such a small change, but works a treat.
Many thanks for taking the time.
MJ
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,785
Office Version
  1. 365
Platform
  1. Windows
Pleasure - whichever solution you used.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,966
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,035
Messages
5,622,332
Members
415,894
Latest member
silverhaze

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
Top