So when you open an Excel Workbook, each sheet saves the last selection of cells you made from the previous time you used the sheet. In C#, i would like to deselect (de-focus, whatever...) all cells in every sheet.
There's always an active cell on the sheet which is active. The only thing you can do is select the least annoying cell, such as the first one.
Related
I have several files I need to open automatically and then import via SSIS.
Problem is, the relevant range in each file is different, and does not necessarily start at cell A1.
When I open a file, press CTRL+End, it will activate the last cell in the sheet. Pressing CTRL+A will now select exactly the range that I need. So this is what I'm trying to automate.
I can implement the first part, of finding the last cell, with
worksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
But I can't figure out how to mimic the Select All command, that's selecting only the adjacent cells with data.
currentRegion did it!
https://learn.microsoft.com/en-us/office/vba/api/excel.range.currentregion
worksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).CurrentRegion;
I have to edit an Excel sheet with a WPF in C# and already ordered the rows by one column. But now I need to cut a few rows and write them to another position in the sheet.
Is ther anyway to completely cut out an entire row and insert this row to another position in the sheet?
Looks like this has been answered
https://social.msdn.microsoft.com/Forums/vstudio/en-US/78059590-c294-47b4-a656-aec8ca51779f/c-excel-cut-or-copy-an-excel-row-and-move-to-another-row?forum=exceldev
I need to acquire range that is currently selected on some non-active worksheet.
If I select some arbitrary range on e.g Sheet1 (A3:B5) and switch to Sheet2, selection will be changed, and if I return back to Sheet1 selection will return too (A3:B5)
I know about Selection property within Application interface, but that only returns selection of currently active sheet of currently active document.
Is there address of selected range, that will help too since I could use:
worksheet.get_Range(address);
There is nothing like selection in sheet which is not active. I agree, when you move to any other sheet your selection will change to range which was selected when you left this sheet earlier. However, you are not able to check it if sheet is not active.
The only workaround I can think of is:
switch off ScreenUpdating property of application
activate the sheet you need check selection
check selection
return to appropriate sheet
turn on ScreeUpdating
The other option would be to remember selection address each time you left sheet using string Public variable and Sheet deactivate event.
how to move entire existing excel range to one row down using interop in C#?
I need to add a header in 1st row of existing excel which has only data.I want to move the entire rangel one setup down programatically in C#.
I saw moving a cell stuff,but moving the entrire range was not clear.
could you please help me with it.
I agree if you are running on a server, CloseXML is a better solution, but if you want to go the interop way:
Here is the code to add a row and shift the existing data down by one row.
// Inserts a new row at the beginning of the sheet
Microsoft.Office.Interop.Excel.Range a1 = sheet.get_Range( "A1", Type.Missing );
a1.EntireRow.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown,
Type.Missing );
After you have inserted your new row at the top, you can add the headers to the cells.
I assume you know how to open and access the sheets.
Im trying to build a addin that can take some a selection of cells from a excel sheet and then put them together in the first cell i have celected. Between the cells there will be a user selected seperator.
example:
I select the 3 cells A1-A3 with the values A B and C
I then bring up my addin and chose the seperator to be ", ".
The addin should then place a the String "A, B, C" in the cell A1.
But I cant figure out how to access the data I have selected in the excel sheet.
var range = (Range)Globals.ThisAddIn.Application.ActiveWindow.Selection;