How to only the last sheet name in an excel file

Hi,

I need to get only the last sheet name in an excel file, as per the process at every run a new sheet will added at the last.
Is that possible to get only the last sheet name and proceed further instead of going to all sheets?

@Prasaanth_S2

Excel Application Scope
Get Workbook Sheets
Output: sheetNames (List<String>)
lastSheetName = sheetNames(sheetNames.Count - 1)

Or

1 Like

Hi @Prasaanth_S2

Chek the below steps,
→ Use the Excel application scope and give the path of the excel file.
→ Inside Excel application scope activity insert the Get workbook sheets activity and create a variable in the Sheets field in properties as List_Sheets.
→ Then use the below expression to get the last sheet name in an excel.

List_Sheets.Last.toString

These are the Classic activities you have to enable the Classic options in the Activities panel filters.

Check the below image for better understanding,

Hope it helps!!

Hi @Prasaanth_S2

You can use this Invoke Code:

Dim excelApp As Microsoft.Office.Interop.Excel.Application = Nothing
Dim workbooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
Dim workbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
lastSheetName = String.Empty

Try
    ' Initialize Excel application
    excelApp = New Microsoft.Office.Interop.Excel.ApplicationClass()
    
    ' Open the workbook
    workbooks = excelApp.Workbooks
    workbook = workbooks.Open(excelfilepath) ' Change this to your file path

    ' Get the last worksheet
    Dim lastSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(workbook.Sheets(workbook.Sheets.Count), Microsoft.Office.Interop.Excel.Worksheet)
    lastSheetName = lastSheet.Name

    ' Output the last sheet name
    Console.WriteLine("Last Sheet Name: " & lastSheetName)

    ' Assign the last sheet name to the output argument
    lastSheetName = lastSheet.Name

Finally
    ' Cleanup
    If workbook IsNot Nothing Then workbook.Close(False)
    If excelApp IsNot Nothing Then excelApp.Quit()
    Marshal.ReleaseComObject(workbook)
    Marshal.ReleaseComObject(workbooks)
    Marshal.ReleaseComObject(excelApp)
    workbook = Nothing
    workbooks = Nothing
    excelApp = Nothing
    GC.Collect()
    GC.WaitForPendingFinalizers()
End Try

Invoked Arguments:

Make sure to import below namespace from Imports Panel

Microsoft.Office.Interop.Excel
System.Runtime.InteropServices

Hope it helps!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.