I am writing a script that copies data from one workbook to another. The latter is being used as a sort of database (not my idea). As a test I am copying ~300 rows of data, of which 3 columns conditional formatting and the rest are plain text. Copying the text is easy and near instantaneous but the formatting is more difficult. Currently I am using the below code to copy the formatted cells:
thisSheet.Range("G" & CStr(rRow), "I" & CStr(rRow)).Copy
masterSheet.Range("G" & CStr(mRow), "I" & CStr(mRow)).PasteSpecial (xlPasteAll)
With ~300 rows this takes about 40 seconds, which is too slow. I cannot copy a range consisting of multiple rows as they are not pasted sequentially.
I experimented with the following code to try and copy formats.
masterSheet.Range("G" & CStr(mRow), "I" & CStr(mRow)).value = thisSheet.Range("G" & CStr(rRow), "I" & CStr(rRow)).value
masterSheet.Range("G" & CStr(mRow), "I" & CStr(mRow)).Font.Color = thisSheet.Range("G" & CStr(rRow), "I" & CStr(rRow)).Font.Color
masterSheet.Range("G" & CStr(mRow), "I" & CStr(mRow)).Interior.ColorIndex = thisSheet.Range("G" & CStr(rRow), "I" & CStr(rRow)).Interior.ColorIndex
'cell color and font color are the only things i am interested in
This code executes in about 3 seconds, but none of the formatting applied by the conditional formatting is copied.
Is there a more efficient way of copying cell and font colors that were applied by conditional formatting?
Try adding this to the start of your code:
And this to the end of it:
If this isn’t working to improve your speed to where you need I’d suggest hard coding the conditional formatting into the VBA.
So for example if one of the conditional formatting rules made the cell Red if the number was over 100. Add that check into the VBA while it’s copying the values and set the destination cell ot the desired format based on its value.