简体   繁体   中英

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.

My problem is that I have an C# application that open this excel file and updates it with data.

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". When I save the workbook from Excel, all the cells with "OK" value are red. 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

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. You could also call the same routine from the BeforeSave handler.

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. Somethimes the XLS file has no macro.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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