1

Can you help me rewriting the following Excel VB code to C#?

Range("C9:E11").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
2
  • If you don't mind me asking, why? Also, are you having trouble with any particular aspect of the C# for this, or are you looking for help to get started? Thanks :) Commented Jun 15, 2009 at 15:09
  • This sounds like a homework question... Commented Jun 17, 2009 at 3:50

2 Answers 2

5

What you're looking for is Excel Automation. Pretty much this means using a set of COM objects provided by Excel to remote control Excel from another application.

Anything you can do with with VBA you can achieve with Automation (OK, almost anything).

If you google for "Excel Automation C#" you'll get lots of hits. How to automate Microsoft Excel from Microsoft Visual C# .NET was the first returned to me, and looks like a good place to get started.

Hope this helps,

Sign up to request clarification or add additional context in comments.

Comments

2
 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "=1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.