May 25 2023 10:54 AM
I can't figure out how to copy conditional formatting rules
the task is to copy a rule (not a cell format, but a rule) from, for example, a file.xlsx (not necessarily this type, any format that supports storing conditional formatting rules is interested in)
and apply it to a previously opened workbook containing data ...
and all this must be done on VBA in excel 2016 (to support versions starting from 2016)
without connecting third-party libraries or using third-party applications (to avoid communicating with permissions)
everything I could find doesn't satisfy at least one concept
Please help
May 25 2023 12:45 PM
Copying only conditional formatting would be complicated.
Copying all formatting, including conditional formatting, is doable - copy the source range and paste special with xlPasteFormats to the target range. But if you have rules that refer to other cells than the ones being formatted, the result might be unpredictable.
May 25 2023 04:14 PM
I would like you to understand. there are no values in the cells of the file in which the rule is stored - conditional formatting in this case cannot be picked up by the Range method or any other method applicable to the cells
May 26 2023 01:57 AM
Formatting (including conditional formatting) can be copied/pasted regardless of whether a cell contains a value (or formula) or not...
May 26 2023 02:10 AM - edited May 26 2023 02:12 AM
could you give an example of the code
accessing the file.xltx
copying a rule
from there and applying it to a previously opened sheet of the book.xlsm
the fact is that I have already tried, either I don't know something, or you have a different version, not 2016 excel
May 26 2023 02:27 AM
From which sheet and which range in file.xltx do you want to copy the formatting to which sheet and which range in book.xlsm?
May 26 2023 03:15 AM - edited May 26 2023 03:17 AM
From what sheet
1st
and in what range
is all
in the file.xlsx do you want to copy
formatting to which sheet
1st
and in what range
is all
in book.xlsm?
in .xltx I have a rule that applies to the entire sheet
XML code from .xltx
...
- <conditionalFormatting sqref="A1:XFD1048576">
- <cfRule priority="1" dxfId="0" type="expression">
<formula>xxx</formula>
</cfRule>
</conditionalFormatting>
...
May 26 2023 03:27 AM
The macro could look like this:
Sub CopyFormatting()
Workbooks("file.xltx").Worksheets(1).Cells.Copy
Workbooks("book.xlsm").Worksheets(1).Cells.PasteSpecial Paste:=xlPasteFormats
End Sub
May 26 2023 04:11 AM
May 26 2023 04:21 AM
If the .xlsm file is the one that contains the macro, and the file created from the template is the active workbook when you run the macro:
Sub CopyFormatting()
ActiveWorkbook.Worksheets(1).Cells.Copy
ThisWorkbook.Worksheets(1).Cells.PasteSpecial Paste:=xlPasteFormats
End Sub
May 26 2023 05:21 AM - edited May 26 2023 06:18 AM
ThisWorkbook call my add-in (.xlam)
the macro is run from the add-in
as I understand it, when I open the template file, it will become active
we need to get back to the file.xlsm somehow
PS
it's magical! .Name works the way it was supposed
to be solved through it
thank you so much for your help...
I don't understand how it works, but I figured out how to dig into the names of windows only after your answer... non-trivial, I'm used to using the full name (path+name+extension)