如果列包含非数字字段,如何创建错误消息-VBA

时间:2018-12-04 20:35:17

标签: excel-vba

如果表的值为非数字值,我试图编写代码以产生错误消息。我没有收到任何错误消息,但是代码没有完成任务。有什么帮助吗?下面的代码:

Sub Refresh()
'
' Warning Code to check if all values are numeric
'-----------------------------------------------------------------------

Dim sh As Worksheet
Dim i As Integer
Dim bisnumberic As Boolean

bIsNumeric = True

For Each sh In ActiveWorkbook.Sheets
    Select Case sh.Name
        Case "AltA", "AltB", "AltC1", "AltC2"
            Dim lRow As Long
            lRow = Cells(Rows.Count, 1).End(xlUp).Row
            For i = 3 To lRow
                If Not IsNumeric(Cells(i, 1).Value) Then
                    bisnumber = False
                End If
            Next i
    End Select
Next sh

    If bIsNumeric = False Then
            'There are non-numeric values in your range
            MsgBox "There are non-numeric values in your range. Go check-            
yourself and try again."
        Else
 '-----------------------------------------------------------------------
' Code to summarize data           Sheets("AlternativeSummary").Select
        Range("B5").Select
        ActiveSheet.PivotTables("PivotTable5").PivotCache.Refresh
        MsgBox "Complete"
        'All values in your range are numeric

End If


End Sub

3 个答案:

答案 0 :(得分:0)

问题出在您的if语句中:

            If Not IsNumeric(Cells(i, 1).Value) Then
                bisnumber = False
            End If

您正在更改bisnumber变量。

后来,您实际上检查了bIsNumeric变量以查看某物是否为数字。您需要更改:

            If Not IsNumeric(Cells(i, 1).Value) Then
                bisnumber = False
            End If

收件人:

            If Not IsNumeric(Cells(i, 1).Value) Then
                bIsNumeric = False
            End If

良好的代码编写能力-欢迎使用Stack Overflow。

答案 1 :(得分:0)

lRow = Cells(Rows.Count, 1).End(xlUp).Row应该有一个明确定义的父级工作表。请记住,您正在浏览一系列工作表。与If Not IsNumeric(Cells(i, 1).Value) Then相同。

Sub Refresh()
    '
    ' Warning Code to check if all values are numeric
    '-----------------------------------------------------------------------

    Dim sh As Worksheet, i As Long, bIsNumeric As Boolean, lRow As Long, errng as range

    bIsNumeric = True

    For Each sh In ActiveWorkbook.Worksheets
        Select Case sh.Name
            Case "AltA", "AltB", "AltC1", "AltC2"
                lRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
                For i = 3 To lRow
                    If Not IsNumeric(sh.Cells(i, 1).Value) Then
                        bIsNumeric = False
                        set errng  = sh.Cells(i, 1)
                        Exit For
                    End If
                Next i
        End Select
        If Not bIsNumeric Then Exit For
    Next sh

    If Not bIsNumeric Then
        'There are non-numeric values in your range
        MsgBox "There are non-numeric values in your range. Go check yourself and try again." _
               & chr(10) & errng.address(0, 0, external:=true)
    Else
        '---------------------------------------------------
        ' Code to summarize data
        With Sheets("AlternativeSummary")
            .Activate
            .Range("B5").Select
            .PivotTables("PivotTable5").PivotCache.Refresh
            MsgBox "Complete"
            'All values in your range are numeric
        end with
    End If

End Sub

答案 2 :(得分:0)

Option Explicit
Sub Refresh()
'
' Warning Code to check if all values are numeric
'-----------------------------------------------------------------------

Dim sh As Worksheet
Dim i As Integer
Dim bIsNumeric As Boolean

bIsNumeric = True

For Each sh In ActiveWorkbook.Sheets
    Dim lRow As Long
    lRow = sh.Cells(Rows.Count, 1).End(xlUp).Row
    For i = 3 To lRow + 1
        If Not IsNumeric(sh.Cells(i, 1).Value) Then
            bIsNumeric = False
        End If
    Next i
Next sh

If bIsNumeric = False Then 'There are non-numeric values in your range
    MsgBox "There are non-numeric values in your range. Go check-yourself and try again."
Else
 '-----------------------------------------------------------------------
' Code to summarize data           Sheets("AlternativeSummary").Select
Range("B5").Select
ActiveSheet.PivotTables("PivotTable5").PivotCache.Refresh
MsgBox "Complete"
'All values in your range are numeric

End If


End Sub

这将遍历所有工作表并检查非数字字段。