简体   繁体   中英

Runtime Error 13 type mismatch Excel

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 在此处输入图片说明

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM