How do I insert a new line at the end of a used range in excel using .net or vba?
I have a range value that contains the used range values for an excel sheet, now I cannot insert a row at the end of the range scope
static public void SpreedsheetOpen(string ExcelFile)
{
xlApp = new Excel.Application();
xlApp.Visible = false;
xlWorkBook = xlApp.Workbooks.Open(ExcelFile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlRange = xlWorkSheet.UsedRange;
}
in this xlRange = xlWorkSheet.UsedRange; I am getting xl range value for a range in a range object, how do I insert a newline at the end of that range.
+3
source to share
1 answer
Dim LastRow As Long, x As Long
'Select your range
xlRange.Select
'determine the last row of the selection/range
x = Selection.Rows(1).Row
LastRow = Selection.Rows.Count + x - 1
'Select only the last row
Rows(LastRow).Select
'Insert a new row under the selected row
Selection.Offset(1).EntireRow.Insert
+1
source to share