I am trying to return the minimum and maximum dates in a column in an excel sheet. i have a program that i created in VBA, that i am rewriting in C# and am not quite sure on this part. The original code i used was;
WB.Activate
DataInputRows = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count
ActiveSheet.Range("S2:T" & Cells(Rows.Count, "A").End(xlUp).Row).NumberFormat = "dd/mm/yyyy"
ActiveSheet.Range("V2:V" & Cells(Rows.Count, "A").End(xlUp).Row).NumberFormat = "dd/mm/yyyy"
ActiveSheet.Range("A1:" & LastColumn & Cells(Rows.Count, "A").End(xlUp).Row).Sort _
Key1:=Range("T1"), Header:=xlYes
SYear = Format(WorksheetFunction.Min(Range("T1:T" & DataInputRows)), "dd/MM/yyyy")
EYear = Format(WorksheetFunction.Max(Range("T1:T" & DataInputRows)), "dd/MM/yyyy")
I have the sort and format area covered, but i cant quite get the "SYear =" or "EYear = " bit nailed down.
Does anyone know a "Simple" way of finding the minimum and maximum value from a column?
The code i have in c# so far is;
DataInputRows = InputSheet.UsedRange.Rows.Count;
Excel.Range rng = (Excel.Range)OutputSheet.Cells[2, 19];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)OutputSheet.Cells[2, 20];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)OutputSheet.Cells[2, 22];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);
var SYear = (This is where i need the minimum value of column T (20))
GOT IT!!
DataInputRows = InputSheet.UsedRange.Rows.Count;
Excel.Range rng = (Excel.Range)InputSheet.Cells[2, 19];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)InputSheet.Cells[2, 20];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)InputSheet.Cells[2, 22];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);
Excel.WorksheetFunction wsf = ObjApp.WorksheetFunction;
rng = (Excel.Range)InputSheet.Cells[2, 20];
var SYear = wsf.Min(rng);
DateTime dt = DateTime.FromOADate(SYear);
MessageBox.Show(dt.ToString());
Thank you to Ralph for the jumpstart.
So @Ralph got me thinking. I was pretty sure it shouldnt be as difficult as it seems, and the commands should be similar enough. so i did a little digging and found where i was going wrong (or more specifically where i wasnt going). edited my code to show the working variant in case anyone needs it in future and +1'd Ralph for the kick in the brain. :)
DataInputRows = InputSheet.UsedRange.Rows.Count;
Excel.Range rng = (Excel.Range)InputSheet.Cells[2, 19];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)InputSheet.Cells[2, 20];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
rng = (Excel.Range)InputSheet.Cells[2, 22];
rng.EntireColumn.NumberFormat = "dd/MM/yyyy";
SourceRange.Sort(SourceRange.Columns[20, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlYes, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal);
Excel.WorksheetFunction wsf = ObjApp.WorksheetFunction;
rng = (Excel.Range)InputSheet.Cells[2, 20];
var SYear = wsf.Min(rng);
DateTime dt = DateTime.FromOADate(SYear);
MessageBox.Show(dt.ToString());
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.