Thanks to Richard MacCutchan
private void cmdEnterData_Click(object sender, EventArgs e)
{
bool found = false;
int intRowIndex;
string targetSheetName = txtDate.Text.Substring(0, 3);
targetSheetName = targetSheetName + " " + DateTime.Now.Year.ToString();
Excel.Worksheet targetWorksheet = (Excel.Worksheet)frmFoodPantry.theWorkbook.Worksheets[targetSheetName];
targetWorksheet.Activate();
int emptyRow = frmFoodPantry.FindEmptyRow(RegGlobals.StartingRow, targetWorksheet.Index, RegGlobals.dateCol);
for(intRowIndex = RegGlobals.StartingRow; intRowIndex < emptyRow && !found; intRowIndex++)
{
DateTime dtSheetDate = targetWorksheet.Cells[intRowIndex, RegGlobals.intDateCol].Value;
if (DateTime.Parse(txtDate.Text) < dtSheetDate)
{
Excel.Range rngRow = frmFoodPantry.theWorkbook.ActiveSheet.Range[RegGlobals.dateCol + emptyRow.ToString()].EntireRow;
rngRow.Delete(XlDeleteShiftDirection.xlShiftUp);
rngRow = frmFoodPantry.theWorkbook.ActiveSheet.Range[RegGlobals.dateCol + intRowIndex.ToString()].EntireRow;
rngRow.Insert();
found = true;
}
}
LoadData(intRowIndex - 1, targetSheetName);
if (EditsMade)
{
frmFoodPantry.objApp.DisplayAlerts = false;
frmFoodPantry.theWorkbook.Save();
frmFoodPantry.objApp.DisplayAlerts = true;
EditsMade = false;
}
}