简体   繁体   English

使用 VB.net 在 Excel 中复制条件格式?

[英]Copying Conditional formatting in Excel using VB.net?

I have a column 'A' in worksheet 'two' and another column 'B' in worksheet 'one' !我在工作表“二”中有一列“A”,在工作表“一”中有另一列“B”!

I want to copy the conditional formatting of Column A in two to Column B in one !我想将 A 列的条件格式一并复制到 B 列!

Basically, I want to copy everything from Column A in two to Column B in one except the data !基本上,我想将除数据之外的所有内容从两列 A 复制到一列 B !

Please help !!请帮忙 !!

I have:我有:

sheetA & sheetB object as Objects in my code
I am using VB.NET

I am a newbie in Vb.net - Please Help !我是 Vb.net 的新手 - 请帮忙!

Here's a bit of a push:这里有一点推动:

    oExcel = CreateObject("Excel.Application")
    Book = oExcel.Workbooks.Open("C:\Users\Jonathan\Documents\Test2.xlsx")
    Sheet = Book.Worksheets(1)
    SourceRange = Sheet.Range("A1:A" & Sheet.Range("A1").End(Excel.XlDirection.xlDown).Row)
    DestRange = Sheet.Range("D1:D" & Sheet.Range("D1").End(Excel.XlDirection.xlDown).Row)

The meat of it is this part, where you take the format conditions from one range and add it to the other.它的核心是这部分,您从一个范围获取格式条件并将其添加到另一个范围。 This assumes there is only one criterion for formatting.这假设只有一个格式化标准。 More than one would require that you would step over all of them (I think there are up to 3) by using SourceRange.FormatConditions(i) in a loop from 1 to SourceRange.FormatConditions.Count不止一个需要您通过在从 1 到SourceRange.FormatConditions.Count的循环中使用SourceRange.FormatConditions(i)来遍历所有这些(我认为最多有 3 个)

    DestRange.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, SourceRange.FormatConditions(1).Operator, SourceRange.FormatConditions(1).Formula1)
    DestRange.FormatConditions(1).Interior.ColorIndex = SourceRange.FormatConditions(1).Interior.ColorIndex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM