Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel Sorting VBA
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#
---------------------------------------------------------------------------
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 25-Jul-12 2:17am
Edited 25-Jul-12 2:18am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
Smile | :)
  Permalink  
v2
Comments
ayerunka at 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 at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100