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

前端 未结 5 648
后悔当初
后悔当初 2021-01-02 20:30

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 use

5条回答
  •  一整个雨季
    2021-01-02 21:03

    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.

提交回复
热议问题