0

I hope you can help I have the below piece of code and I am getting runtime error 13 on this line

If Sheets("Input").Range("A11:C100").Value = "" Then

What I am trying to achieve is to not let a user save the Excel workbook without populating the cells in the range ("A11:C100") The code works fine if it is just ("A11") but if I increase the range I get the error.

The rest of my code is below any help would be greatly appreciated.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

'Step 1: Check to see if Cell A1 is blank
    If Sheets("Input").Range("A11:C100").Value = "" Then

'Step 2: Blank: cancel the Close and tell the user
        Cancel = True
        MsgBox "Cells A11 to C100 can not be blank"
'Step 3: Not Blank; Save and Close
    Else
        ActiveWorkbook.Close SaveChanges:=True
    End If
End Sub

Pic of sheet enter image description here

2
  • 1
    Will you consider to loop and check all cells? you cannot compare a value with a range. Example like this: msdn.microsoft.com/en-us/library/office/ff195193.aspx Commented Oct 27, 2016 at 8:49
  • @ Alex: Thank you for the help. I would consider a loop. Commented Oct 27, 2016 at 9:26

1 Answer 1

1

Sheets("Input").Range("A11:C100").Value returns a Variant array with all cells content in the range, so you can't compare it to a string like ""

  • if you need to check if at least one cell of the range is populated then

    instead of:

    If Sheets("Input").Range("A11:C100").Value = "" Then
    

    use

    If WorksheetFunction.CountA(Sheets("Input").Range("A11:C100")) = 0 Then
    
  • if you need to check if all cells of the range are populated then

    instead of:

    If Sheets("Input").Range("A11:C100").Value = "" Then
    

    use

    If WorksheetFunction.CountA(Sheets("Input").Range("A11:C100")) < Sheets("Input").Range("A11:C100").Count Then
    
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the help. I need to check that all cells are populated. the code you gave me If WorksheetFunction.CountA(Sheets("Input").Range("A11:C100")) = Sheets("Input").Range("A11:C100").Count Then did not work it just turn the VBA area blank and did the same to the excel sheet have i done something wrong?
sorry there should be a "<" instead of a "=". see edited answer. Not clear what does "it just turn the VBA area blank" mean, though.
@3598756: The amended code worked perfectly. Thank you so much for your time and skills. Much respect from Dublin. :-) The "It just turns blank" the VBA editor just went white as if no code was there and the excel sheet went to a grey background as if waiting to open a new file. But it is fixed now, Again thank you so much for the assistance
Ha Ha:-) Yep. That's me Marcoing and VBAin for my life :-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.