我正在尝试返回Excel工作表中列中的最小和最大日期。 我有一个我在VBA中创建的程序,我在C#中重写,并且我不太确定这一部分。 我使用的原始代码是;
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")
我有覆盖的排序和格式区域,但我不能完全确定“SYear =”或“EYear =”位。
有没有人知道从列中找到最小值和最大值的“简单”方法?
到目前为止我在c#中的代码是;
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());
感谢拉尔夫的快速启动。
答案 0 :(得分:1)
所以@Ralph让我思考。我很确定它不应该像看起来那么困难,并且命令应该足够相似。所以我做了一点挖掘,发现我哪里出错了(或者更具体地说,我不去的地方)。编辑了我的代码,以显示工作变体,以防将来有人需要它,并为大脑中的踢腿+1拉尔夫。 :)
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());