【发布时间】:2016-11-05 12:37:52
【问题描述】:
我需要扫描某个范围内的某些单元格。如果某些单元格为空,则消息“单元格为空”将写入该特定的空单元格中。我一直在尝试以下方法:
Sub Empty()
Sheets("My sheet").Select
If Range("C5:C12,C15:C22,C25:C32,C36:C43,C46:C53,C56:C63,C66:C73,C76:C83,D4,D14,D24,D35,D45,D55,D65,D75").Value = "" Then
Range("C5:C12,C15:C22,C25:C32,C36:C43,C46:C53,C56:C63,C66:C73,C76:C83,D4,D14,D24,D35,D45,D55,D65,D75").Value = "cell is empty"
End If
End sub
I am getting the error: run time error #13 - type mismatch.
为了帮助可能遇到与我相同问题的任何其他人,我将补充下面介绍的工作解决方案。请注意,我添加了一个错误处理来阻止消息“运行时错误 '1004': No Cells were found" 以及一个用于扫描符合您需求的特定工作表的数组:
Sub myEmpty()
Dim rng As Range
On Error GoTo NoBlanks
Dim MyArray As Worksheet
For Each MyArray In ActiveWorkbook.Worksheets
Select Case MyArray.Name
Case Is = "Sheet 1", "Sheet 2", "Sheet 3", "Sheet n-1", "Sheet n"
With MyArray
Set rng = .Range("C5:C12,C15:C22,C25:C32,C36:C43,C46:C53,C56:C63,C66:C73,C76:C83,D4,D14,D24,D35,D45,D55,D65,D75")
If CBool(Application.CountBlank(rng)) Then
rng.SpecialCells(xlCellTypeBlanks).Value = "cell is empty"
End If
End With
Case Else
End Select
Next MyArray
NoBlanks:
CreateObject("WScript.Shell").Popup " There are no empty cells", 0.7, "INfo:"
End Sub
【问题讨论】:
标签: vba excel error-handling