我写了这段代码并给出了错误。
我想为有错误的单元格提示错误消息。
Public Sub OnErrorDemo()
On Error GoTo ErrorHandler
Dim LastCol As Long
Dim LastRow As Long
Dim rng As Range, iLength As Integer
LastCol = ActiveSheet.UsedRange.Columns.Count
LastRow = ActiveSheet.UsedRange.Rows.Count
ErrorHandler:
For h = 1 To Range("A60").End(xlUp).Row
Set rng = ActiveSheet.Range("A1:A&h")
Select Case IsEmpty(rng)
Case True
MsgBox rng.Address & " is empty"
Case Else
Select Case IsNumeric(rng)
Case True
MsgBox rng.Address & " has a numeric value"
Select Case rng.HasFormula
Case True
MsgBox rng.Address & " also has a formula"
End Select
Case Else
iLength = Len(rng)
MsgBox rng.Address & " has a Text length of " & iLength
End Select
End Select
Next h
End Sub
答案 0 :(得分:1)
检查代码的这一行,我认为这可能会导致错误:
Set rng = ActiveSheet.Range("A1:A&h")
而是使用它:
Set rng = ActiveSheet.Range("A1:A" & h)