Runtime Error 13 type mismatch Excel

Conell8383

Board Regular
Joined
Jul 26, 2016
Messages
66
I hope you can help I have the below piece of code and I am getting runtime error 13 on this line

Code:
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.


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


'Step 1: Check to see if Cell A11 to C100 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


 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Conell8383,

Try changing the following line of code from this:

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


To this:

Code:
    If Sheets("Input").Range("A11:C100") = vbEmpty Then
 
Upvote 0
This will produce the same error I guess - trying to compare multiple values against a single one (hence no problem when only A11 is compared)
A way to achieve your task is to loop through all cells in the range and check each one.
Another way:
Code:
if Join(Application.Transpose(Sheets("Input").Range("A11:C100").Value), "") = "" then
However - this means that if even only one cell is not empty the evaluation result will be TRUE.
 
Upvote 0
@Bobsan42 and Hhiker 95: Cheers for the help guys. I got there in the end. the code that worked for is below. Again thank you for the help.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Step 1: Check to see if Cell A1 is blank
    If WorksheetFunction.CountA(Sheets("Input").Range("A11:C100")) < Sheets("Input").Range("A11:C100").Count 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
 
Upvote 0
@Bobsan42 and Hhiker 95: Cheers for the help guys. I got there in the end. the code that worked for is below. Again thank you for the help.

Conell8383,

You are welcome.

Glad that you were able to solve your own request.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,540
Members
449,515
Latest member
lukaderanged

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