How can I use VBA to ignore green triangle error in range without looping cell by cell?

浪子不回头ぞ 提交于 2019-11-30 23:07:36

The preferred solution would be to convert the string to a number before you bring it into Excel. For example, when I am working with SQL and I have some numerical values stored as a NVARCHAR in the database I will use a CONVERT(int, colName) in the SQL statement when I am bringing it into Excel. This brings it in as a number and I no longer get that message.

Now, if this sort of option isn't available to you, you can fix that error another way. Simply set the range of values that have the number stored as text error equal to itself.

For example:

Sub Test()

    Sheets("Sheet1").Range("A1:A3").Value = Sheets("Sheet1").Range("A1:A3").Value

End Sub

Where A1:A3 in this example is the range of values you want to no longer store as text.

Since your numbers have leading zeroes, you can change the formatting of these cells to add these zeroes as such:

Sub Test()

    Sheets("Sheet1").Range("A1:A3").Value = Sheets("Sheet1").Range("A1:A3").Value
    'This assumes your numbers are 11 digits long
    'Thus 11132 would display as 00000011132
    Sheets("Sheet1").Range("A1:A3").NumberFormat = "00000000000"

End Sub

This will change the display to show leading zeroes again. If you are exporting this data in any fashion you may have to take steps to ensure that this particular column is exported as text and not number, but I can't help much more without specifics.

Joel Spolsky

The obvious answer (Range(...).Errors(3).Ignore = True) doesn't seem to work when the Range is larger than a single cell.

After a bit of experimentation, I found that you can manually select the range of cells and click the little pop-up menu that appears and tell it to ignore all errors in the range, but this doesn't appear to have a VBA equivalent.

Doing this experiment with the macro recorder on records nothing, which is usually a sign that the programmer at Microsoft who implemented this functionality was incompetent.

Sadly, I think this means that there is no solution other than looping.

Related

You can use workbook events to turn on and off the user's system setting, and restore the setting back to the original value when you're done.

In the ThisWorkbook object, put an Open event that takes note of their initial setting and then turns it off.

Dim MyErrorCheckValue as Boolean

Private Sub Workbook_Open()
    MyErrorCheckValue = Application.ErrorCheckingOptions.NumberAsText
    Application.ErrorCheckingOptions.NumberAsText = False
End Sub

Add a BeforeClose event to set it back to original value when closing the file.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.ErrorCheckingOptions.NumberAsText = MyErrorCheckValue
End Sub

Then add Activate and Deactivate events so it switches when the user opens or views a different spreadsheet.

Private Sub Workbook_Activate()
    Application.ErrorCheckingOptions.NumberAsText = False
End Sub

Private Sub Workbook_Deactivate()
    Application.ErrorCheckingOptions.NumberAsText = MyErrorCheckValue
End Sub

You could add similar events at the sheet level to turn it on and off when switching sheets within the workbook.

Would also be wise to add some error handling that sets it back to original value so you don't accidentally leave it in the wrong state in the unlikely event that your code bugs out somewhere.

I have created a nifty procedure to put the error in the 'ignore' list: Have fun

Sub SetInconsistentFormulaErrorsFalse(rng As Range, _
Optional arrErrortypes As Variant = Null, _
Optional bIgnoreErrors As Boolean = True)

Dim cl As Range
Dim i As Integer

If IsNull(arrErrortypes) Then
   arrErrortypes = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
End If

For i = 0 To UBound(arrErrortypes) - 1
    For Each cl In rng.Cells
        cl.Errors(arrErrortypes(i)).Ignore = bIgnoreErrors
    Next
Next i


Set cl = Nothing

End Sub

There appears to be an error in the code shared by the original poster. In order to get this to work I have add to add in the .Item of the error:

Dim rngCell As Range, bError As Byte
For Each rngCell In Selection.Cells

    For bError = 1 To 4

        With rngCell
            If .Errors.Item(bError).value Then
                .Errors.Item(bError).Ignore = True
            End If
        End With
    Next bError
Next rngCell
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!