Clear contents with cell count

mjtolent

New Member
Joined
Oct 1, 2020
Messages
12
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,760
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,760
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
793
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
12
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,760
Office Version
  1. 365
Platform
  1. Windows
Pleasure - whichever solution you used.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,671
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,119,060
Messages
5,575,862
Members
412,688
Latest member
KYLE1788
Top