I am going to show a bit of a contrived example, so bear with it.
Our product makes use of CSV files for transitional data, data sent between an Excel user interface, a Java program to manipulate and transfer it to a SQL backend. We have a VBA script that handles all the Excel work in the following order:
Load all 8 CSV files into 8 sheets in a single Excel document. Then iterate over batches of the data doing the following:
'Loop over data:
Dim r As Range
...
r.NumberFormat = "General"
r.Formula = r.Formula
'End loop
This causes the entire sheet to be populated with data from CSV, with number cells to have text appearance and Excel formulas to remain unevaluated. Running r.Formula = r.Formula triggers all the functions to evaluate properly. The only problem is the number formatting.
The CSV files sometimes contain nested CSV. For example, a single cell may contain “1,2,3,15,654” These cells always appear as text. However, there is an edge case, wherein the cells could be pretty print numbers, such as “10,456,345” Excel will convert these numbers into Number cells after evaluating all the functions, and strip out all of the commas. While the 20,000 or so rows in the document are otherwise correct, the 4 or so rows this affects breaks the entire system.
Is there a way to trigger Excel to evaluate the functions from CSV without changing the cell formatting entirely from VBA? Changing formats from CSV to SYLK is not an option, as the Java CSV Generator is handled by a different division.
You could turn formatting to text on all the cells (Cells.NumberFormat=”@”) then loop over them and use your code on cells which start with ‘=’.
If performance is an issue you should put the worksheet content in an array, work on the array and put it back to the sheet.
If you post more code and sample data people will be able to have a closer look.
EDIT
for example, putting the following values in column A (from A1 to A4) of “Sheet1”, with a Text formatting:
and using the following code:
The result is put in column B and is :