Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can anyone help me to convert the following Excel VBA code to C# - this is sorting on cell colour in excel :-

There is a table in an excel file with columns as TowerName, Incident, Problem and Change. The column Incident contains values as cell colour in Red, yellow and Green. I need to sort this table such that it will sort on Incident column by red colour then by yellow colour. I have recorded macro for doing this which is copied above. Please help me how would i do this programatically in c# console application.

I need to convert this piece of VBA code to C#
---------------------------------------------------------------------------
VB
Range("E3:H18").Select 
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear 
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("F4:F18"), _ xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0) 
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range("F4:F18"), _ 
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 255, 0) 
With ActiveWorkbook.Worksheets("Sheet1").Sort 
.SetRange Range("E3:H18") 
.Header = xlYes 
.MatchCase = False 
.Orientation = xlTopToBottom 
.SortMethod = xlPinYin 
.Apply 
End With
Posted
Updated 25-Jul-12 1:18am
v2

1 solution

C#
Range["E3:H18"].Select();
ActiveWorkbook.Worksheets["Sheet1"].Sort.SortFields.Clear();
ActiveWorkbook.Worksheets["Sheet1"].Sort.SortFields.Add(Range["F4:F18"], _ xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0, 0); 
ActiveWorkbook.Worksheets["Sheet1"].Sort.SortFields.Add(Range["F4:F18"], _ 
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 255, 0) ;
using (ActiveWorkbook.Worksheets["Sheet1"].Sort())
{
.SetRange Range["E3:H18"] ;
.Header = xlYes ;
.MatchCase = False ;
.Orientation = xlTopToBottom ;
.SortMethod = xlPinYin ;
.Apply(); 
}


refer below link to convert vb code to c# (or other language conversion)
http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]

Happy Coding!
:)
 
Share this answer
 
v2
Comments
ayerunka 25-Jul-12 7:31am    
I am using interop excel class in c# for doing this. The given answer is not valid since the methods like Range, Sort, activeworkbook are not used directly in C#
Aarti Meswania 25-Jul-12 7:39am    
where it's giving error hover mouse on that if it's saying 'sort ' is method then write it as 'sort()' because method must have round brackets, when you will write opening round bracket '(' in-telly-sense will show you the possible parameter list also.
ayerunka 26-Jul-12 2:53am    
No.
The given solution is not helping me in my c# code. It is giving errors on everyline.
Aarti Meswania 26-Jul-12 2:59am    
which errors you got, please describe in brief (or copy paste error statements)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900