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

前端 未结 5 684
后悔当初
后悔当初 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:07

    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.

提交回复
热议问题