I have some large data sets that I am automating and distributing. I want to eliminate the little green triangles that warn the user about numbers stored as text. I have used the following code but it's VERY slow on massive sheets.
Range(Cells(1, 1), Cells(lastrow, lColumn)).Select
'kill those dang green cell triagles
Dim rngCell As Range, bError As Byte
For Each rngCell In Selection.Cells
For bError = 3 To 3 Step 1
With rngCell
If .Errors(bError).Value Then
.Errors(bError).Ignore = True
End If
End With
Next bError
Next rngCell
As you can see I already cut it down to 1/7th of the time by not looping through every error just the one I am trying to prevent but it's still VERY slow.
Also I already know about
Application.ErrorCheckingOptions.NumberAsText = False
But I don't want to use it as I do not want to change users system settings. I want the effect of the loop without looping through all cells. Can I some how tell Excel to stop checking an entire range without looping cell by cell?
Any effective and fast way to do this would be very helpful. Thank you in advance!!
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.
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.
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
来源:https://stackoverflow.com/questions/29708312/how-can-i-use-vba-to-ignore-green-triangle-error-in-range-without-looping-cell-b