Как создать сообщение об ошибке, если столбец содержит не числовое поле - VBA - PullRequest
0 голосов
/ 04 декабря 2018

Я пытаюсь написать код для вывода сообщения об ошибке, если значения таблицы не числовые.Я не получаю никаких сообщений об ошибках, но код не выполняет задачу.Любая помощь?Код ниже:

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 голосов
/ 04 декабря 2018

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
0 голосов
/ 05 декабря 2018
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

Это перебирает все листы и проверяет наличие нечисловых полей.

0 голосов
/ 04 декабря 2018

Проблема здесь в вашем операторе 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.

...