0
Dim Lrow,LColumn as Long
Sheet1.Activate
If WorksheetFunction.CountA(Cells) > 0 Then
LRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End If

Can someone help to convert this vba code to C# getting lastrow and lastcolumn in a worksheet.

AAP
  • 169
  • 1
  • 2
  • 17
  • refer http://stackoverflow.com/questions/17430418/get-the-last-cell-column-row-of-a-excel-range-object – sowjanya attaluri Jul 05 '16 at 11:17
  • Link provided will return used range not the actual range. Used range could contain many empty rows and columns in a range. – AAP Jul 05 '16 at 11:43
  • Thanks, I appreciate your help, but still we need to focus on one column/row to get the last cell. If you don't known which column to focus because data in the spreadsheet are uneven. If I guess for one column to get Last Row that would be wrong. Therefore the above vba code works a specialist and can never go wrong finding last column and last row. – AAP Jul 05 '16 at 12:17

1 Answers1

0

refer this link https://social.msdn.microsoft.com/Forums/office/en-US/5fa24ca9-2949-4442-b0f1-742e941cfe5a/how-to-get-the-last-cell-on-a-sheet-in-c-like-endxlup-in-excel-vba?forum=exceldev

or Try this to get last row and last column of excel sheet,

   Microsoft.Office.Interop.Excel._Worksheet worksheet = null;           

   // get the reference of first sheet. By default its name is Sheet1.
   // store its reference to worksheet
   worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);

   int rowcount = worksheet.Rows.Count;
   int colcount = worksheet.Columns.Count;
sowjanya attaluri
  • 903
  • 2
  • 9
  • 27
  • This function gives rowcount=1048576 and colcount=16384 meaning all cells in a worksheet not the last row or last column of the data. – AAP Jul 05 '16 at 12:35
  • Thanks for your help. This also has pitfalls. Read:[link](http://stackoverflow.com/questions/25110873/excel-application-cells-specialcellsxlcelltypelastcell-returning-bottom-of-wor) – AAP Jul 05 '16 at 12:40
  • try http://www.dailyfreecode.com/forum/find-last-cell-value-particular-column-26162.aspx – sowjanya attaluri Jul 05 '16 at 12:41
  • This link is again focus on a column to get last row. – AAP Jul 05 '16 at 12:44
  • After having lots of research and with my long experience with excel and vba. I found the only solution to find accurate results. Therefore, I would like to convert this in to C# code. – AAP Jul 05 '16 at 12:59
  • oh congrats.post the answer you tried that may help others. – sowjanya attaluri Jul 05 '16 at 13:00
  • The vba code in my question is the only perfect solution but unfortunately I couldn't be able to convert into C# code. – AAP Jul 05 '16 at 13:03