Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I managed to get the range working correctly to include the whole row, but deleting the row fails. It does not produce an error; it just does not delete the row. Finding the empty row works fine, and I placed a marker several rows below the empty row. The result is the empty row is not getting deleted. The sheet is not protected.

The code is:
C#
private void cmdEnterData_Click(object sender, EventArgs e)
   {
      bool found = false;
      int intRowIndex;

      string targetSheetName = txtDate.Text.Substring(0, 3);
      // Add the current year to the name 
      //which gives the full name of the sheet
      targetSheetName = targetSheetName + " " + DateTime.Now.Year.ToString();
      // Get the worksheet
      Excel.Worksheet targetWorksheet =
      (Excel.Worksheet)frmFoodPantry.theWorkbook.Worksheets[targetSheetName];
      targetWorksheet.Activate();
      // Find the first empty row using the sheet index number
      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;

        // For debugging
        string temp1 = rngRow.Address;

       rngRow.EntireRow.Delete(XlDeleteShiftDirection.xlShiftUp);

       // Saving the file for debugging
       frmFoodPantry.objApp.DisplayAlerts = false;
       frmFoodPantry.theWorkbook.Save();
       frmFoodPantry.objApp.DisplayAlerts = true;

       targetWorksheet.Rows.Insert(intRowIndex,1);
       found = true;
     }
   }
   LoadData(intRowIndex, targetSheetName);
   if (EditsMade)
   {
      frmFoodPantry.objApp.DisplayAlerts = false;
      frmFoodPantry.theWorkbook.Save();
      frmFoodPantry.objApp.DisplayAlerts = true;
      EditsMade = false;
   }
}


What I have tried:

C#
targetWorksheet.Cells.Delete(rngRow); (will freeze running)
rngRow.EntireRow.Delete(XlDeleteShiftDirection.xlShiftUp);
((Range)rngRow.Rows[emptyRow]).Delete();
rngRow.Rows.Delete(XlDeleteShiftDirection.xlShiftUp);
rngRow.Delete(XlDeleteShiftDirection.xlShiftUp);
Posted
Updated 29-Aug-23 0:41am
v6
Comments
Richard MacCutchan 24-Aug-23 4:02am    
I just tried a simple test and the row gets deleted correctly. You will need to gather more information, probably by using the debugger.
Andre Oosthuizen 24-Aug-23 6:15am    
I did not test but have you tried -
rngRow.Delete(XlDeleteShiftDirection.xlShiftUp);
PaulaJoannAllen 24-Aug-23 7:19am    
I just tried it and had no results; I know I am making some simple mistake.
Richard MacCutchan 24-Aug-23 7:24am    
Use the debugger to check what is happening. There is no point in saying things like, "I know I am making some simple mistake.", as that tells us nothing useful.
PaulaJoannAllen 24-Aug-23 7:29am    
It worked

1 solution

Thanks to Richard MacCutchan

private void cmdEnterData_Click(object sender, EventArgs e)
{
   bool found = false;
   int intRowIndex;

   // Get the month for the sheet we want to enter the data into
   string targetSheetName = txtDate.Text.Substring(0, 3);
   // Add the current year to the name which gives the full name of the sheet
   targetSheetName = targetSheetName + " " + DateTime.Now.Year.ToString();
   // Get the worksheet
   Excel.Worksheet targetWorksheet = (Excel.Worksheet)frmFoodPantry.theWorkbook.Worksheets[targetSheetName];
   targetWorksheet.Activate();
   // Find the first empty row using the sheet index number
   int emptyRow = frmFoodPantry.FindEmptyRow(RegGlobals.StartingRow, targetWorksheet.Index, RegGlobals.dateCol);
            
   for(intRowIndex = RegGlobals.StartingRow; intRowIndex < emptyRow && !found; intRowIndex++)
   {
      // Get the new cell date and time for comparison to the date and time entered
      DateTime dtSheetDate = targetWorksheet.Cells[intRowIndex, RegGlobals.intDateCol].Value;
      if (DateTime.Parse(txtDate.Text) < dtSheetDate)
      {
         // Assign the range for the empty row.
         Excel.Range rngRow = frmFoodPantry.theWorkbook.ActiveSheet.Range[RegGlobals.dateCol + emptyRow.ToString()].EntireRow;
         // Delete the empty row to make room for the new row
         rngRow.Delete(XlDeleteShiftDirection.xlShiftUp);
         // Redifine the range for the row to insert
         rngRow = frmFoodPantry.theWorkbook.ActiveSheet.Range[RegGlobals.dateCol + intRowIndex.ToString()].EntireRow;
         // Inserting a row into the worksheet above the older date
                    rngRow.Insert();
                    found = true;
     }
  }
  // Load the data into the new row
  LoadData(intRowIndex - 1, targetSheetName);
  if (EditsMade)
  {
     frmFoodPantry.objApp.DisplayAlerts = false;
     frmFoodPantry.theWorkbook.Save();
     frmFoodPantry.objApp.DisplayAlerts = true;
     EditsMade = false;
  }
}
 
Share this answer
 
v3

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