Oh, Excel! You fry my brain, you buggy git.
If you want to slow down VBA code then you add loads of sheet read and writes. Equally, to speed things up, you get rid of the read writes. I’ve been scraping data from the web and for each worksheet row of data, I could find an individual element and write it to a cell, then move to the next and repeat to complete the row. This is vastly speeded up by collecting data in to an array and then pasting it in one go, neat little trick.
This appeared to be working fine until I noticed a date value not pulling through consistently. I was holding the date, as read from a webpage, in the format ddmmyy. However, upon writing to the worksheet it altered to mmddyy, but only when the day value was less than 10. Once the day was 10 or greater, the value appeared in the correct format.
I tried to solve this. I tried setting the format of the cell in the worksheet before writing – didn’t work. I tried formatting within the array – didn’t work. Even though it appears correct in the array when viewing in the locals window of VBA, the problem persisted. I checked the regional settings on my computer – all ok. The array holds lots of values of differing data types and all save and paste ok except this. I even tried pulling the day, month and year value separately and then combine prior to writing – no better.
Searched the web and found some reference to the problem but no solutions.
I eventually solved this though through just guessing. I changed the format of the saved date value within the array to yymmdd. Now, when the array is pasted to the sheet, the regional settings display the value in the correct format, ddmmyy. Seems a ridiculous bug and I’ve no idea why VBA would do this only with certain dates and not throw an error. It’s like it’s guessing at autocorrecting but I can’t turn it off.