A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
The below code could be reduced to one line
.Range(.Cells(4, "P"), .Cells(lngLastRow, "P")) = Date
.Range(.Cells(4, "P"), .Cells(lngLastRow, "P")).NumberFormat = "dd mmm, yyyy"
The Format function returns a String (Text) that can no longer be used as a date without conversion back to a date. I very much doubt that the one line of code to set the Number Format is going to make any measurable difference to the time taken to run the code. In fact, if it is actually measurable, it would not surprise me if using the Format function takes longer due to the work involved in making the conversion to a string.
Also I noticed when there is a trailing space in wsSource >> Table >> Column Name it throws error code 9.
I had previously indicated that I had a problem with one of the headers in a previous post as per the following paragraph.
Note where I have copied the columns that I am now using the table column name in lieu of the column number (It makes it self documenting and you don't have to count up the columns. If any fail then probably due to a leading or trailing space on the column header in the table (I actually had one of these)
The table headers need to be corrected before using them because they must match exactly to the column name used in the code.
It is not possible to remove the additional spaces in the header from within the line of code. (It must be done before using the specific line of code).
Another problem is that they are not necessarily spaces, particularly if the data has been downloaded from elsewhere and they can be some other non printable character that appears like a space and the functions Trim and/or Clean do not remove them.
If you would like to provide me with a full copy of all of the headers in the table then I will provide some code that should ensure the headers are correct before the remaining code runs.
To provide the list of column headers.
- Add a worksheet to the workbook containing the table.
- Select and copy the table headers.
- Select the first cell in the new worksheet and PasteSpecial -> Values -> Transpose -> OK (Values and Transpose both selected in the same dialog)
- You should now have a vertical list of the table headers.
- Copy the list and paste into your reply on the forum.
- You can delete the extra worksheet.