简体   繁体   English

从C#打开文件时Excel宏无法正确执行

[英]Excel macro doesn't execute correctly when opening file from c#

I have a macro that format cells based on condition. 我有一个宏,可以根据条件格式化单元格。 Here is the code : 这是代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Set MyPlage = Sheets("Report").Range("E13:E1500")
For Each Cell In MyPlage
    If Cell.Value = "L" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 3
    ElseIf Cell.Value = "K" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 44
    ElseIf Cell.Value = "J" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 10
    ElseIf Cell.Value = "ü" Then
        Cell.Borders.ColorIndex = 1
    ElseIf Cell.Value = "" And Cell.Offset(0, 1).Value <> "" Then
        Cell.Borders.ColorIndex = 1
    Else
    Cell.Borders.ColorIndex = 2
End If

Next

The macro executes before saving the workbook. 宏在保存工作簿之前执行。 It works perfectly from excel. 它可以从excel完美地工作。

My problem is that I have an C# application that open this excel file and updates it with data. 我的问题是我有一个C#应用程序,可以打开此excel文件并用数据更新它。

When I save the file (from code) and open the file (from desktop or anywhere) I see that the macro have been runn but the colors (formatting) aren't correct for certain cells. 当我保存文件(从代码)并打开文件(从桌面或任何地方)时,我看到宏已运行,但某些单元格的颜色(格式)不正确。

For example, if a cell value is "OK", the macro format the cell should have the color "red". 例如,如果单元格值为“ OK”,则该单元格的宏格式应为“红色”。 When I save the workbook from Excel, all the cells with "OK" value are red. 当我从Excel保存工作簿时,所有具有“确定”值的单元格均为红色。 Great! 大!

But when I run my application that open the file, make changes, and save it, some of the "OK" cells are "red" (great!) but other are "green" (bad!). 但是,当我运行打开文件,进行更改并保存的应用程序时,某些“确定”单元格为“红色”(很好!),而另一些为“绿色”(不好!)。

Does anyone have an idea? 有人有主意吗?

Thank you 谢谢

One suggestion that might be worth trying: move the VBA code into a new routine, eg 一个可能值得尝试的建议:将VBA代码移到新例程中,例如

Public Sub UpdateFormats
    Set MyPlage = Sheets("Report").Range("E13:E1500")
    For Each Cell In MyPlage
    ....
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  call UpdateFormats
End Sub

and then explicitly call the routine from C# before saving and closing the sheet. 然后在保存和关闭工作表之前从C#中显式调用该例程。 You could also call the same routine from the BeforeSave handler. 您也可以从BeforeSave处理程序中调用同一例程。

This might allow you to observe the routine being called whilst the sheet is still open - an improvement over having to open the sheet after it has been closed. 这可能使您可以观察到仍在打开床单时调用的例程-与在关闭床单后必须打开床单相比有所改善。

No I cannot call the macro from code because the appli is an appli that report MPP tasks to XLS file and the XLS file format can change depending on person who use it. 不,我无法从代码中调用宏,因为该应用程序是将MPP任务报告给XLS文件的应用程序,并且XLS文件格式可以根据使用它的人而改变。 Somethimes the XLS file has no macro. Somethimes XLS文件没有宏。

I found my answer. 我找到了答案。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Set MyPlage = Sheets("Report").Range("E13:E1500")
For Each Cell In MyPlage
    If Cell.Value = "L" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 3
    ElseIf Cell.Value = "K" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 44
    ElseIf Cell.Value = "J" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 10
    ElseIf Cell.Value = "ü" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 1
    ElseIf Cell.Value = "" And Cell.Offset(0, 1).Value <> "" Then
        Cell.Borders.ColorIndex = 1
        Cell.Font.ColorIndex = 1
    Else
    Cell.Borders.ColorIndex = 2
End If

Next

End Sub With this code, the format changes every time even for black font for which I didn't specifiy value. End Sub使用此代码,即使对于我未指定值的黑色字体,格式每次也会更改。 That is why when a value changed, the cell with green font before remained grenn after for a cell that should have a black font. 这就是为什么当值更改时,对于应该具有黑色字体的单元格,之前带有绿色字体的单元格保持为grenn之后的原因。

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

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