Excel VBA:消息框,显示最后的错误列表

时间:2015-05-11 15:58:04

标签: excel vba excel-vba

我是VBA的新手,我非常感谢您对以下内容的帮助。下面的代码搜索A列中的空白,突出显示它们,然后每当一个单元格与单元格位置空白时显示一条消息(例如“No Value,in $ A $ 1”)。我将这些单元格位置复制到另一个名为“结果”的选项卡中。

我需要以下帮助。我想以某种方式让消息显示一次具有空白及其位置的单元格列表。我不希望每次找到一个空白单元格时弹出消息(我可以在文件中有数百个空白单元格,然后单击“确定”,每个单元格都不实用)。然后,该值列表将复制到我的“结果”表中。

以下是我目前的代码:

Sub CeldasinInfo()

Dim i As Long, r As Range, coltoSearch As String

coltoSearch = "A"

For i = 1 To Range(coltoSearch & Rows.Count).End(xlUp).Row
    Set r = Range(coltoSearch & i)
    If Len(r.Value) = 0 Then
         r.Interior.ColorIndex = 3 ' Red
          r.Select
          MsgBox "No Value, in " & r.Address
          Sheets("Results").Range("A" & Sheets("Results").Range("A" &   Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = r.Address
    End If
Next
End Sub

提前非常感谢。我非常感谢你对此的帮助。

2 个答案:

答案 0 :(得分:1)

这样的事情对你有用:

Sub CeldasinInfo()

Dim i As Long, r As Range, coltoSearch As String
Dim Result as String
Dim ErrCount as integer

ErrCount = 0
coltoSearch = "A"
coltoSearch = Range("1:1").find(What:="Hours", LookIn:=xlValues, LookAt:=xlWhole).Column

Result = "No Value in:" & vbcrlf
For i = 1 To Range(coltoSearch & Rows.Count).End(xlUp).Row
    Set r = Range(coltoSearch & i)
    If Len(r.Value) = 0 Then
         r.Interior.ColorIndex = 3 ' Red
          r.Select
'          MsgBox "No Value, in " & r.Address
          Result = Result & r.Address & vbcrlf
          ErrCount = ErrCount + 1
          if ErrCount Mod 10 = 0 then    'change to 15 or 20 or whatever works well
            MsgBox Result
            Result = "No Value in:" & vbcrlf
          End If
          Sheets("Results").Range("A" & Sheets("Results").Range("A" &   Rows.Count).End(xlUp).Row).Offset(1, 0).Formula = r.Address
    End If
Next

If ErrCount > 0 then
  MsgBox "There were " & ErrCount & " errors detected." & vbcrlf & result
else
  MsgBox "No errors detected"
End If
End Sub

这将为您提供MsgBox中单独一行的每个地址。如果可能存在数百个错误,这将导致非常MsgBox输出,并且我不确定它将如何处理。您可能需要在计数器中添加并显示每10,15或20个错误的消息,以获得更好看的输出。

答案 1 :(得分:0)

如果您的单元格确实为空,则可以避免使用范围循环并使用SpecialCells

Sub CeldasinInfo()

Dim rng1 As Range
Dim coltoSearch As String

coltoSearch = "A"

On Error Resume Next
Set rng1 = Columns(coltoSearch).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0

If rng1 Is Nothing Then Exit Sub

rng1.Interior.ColorIndex = 3 ' Red
MsgBox "No Value, in " & rng1.Address

End Sub