How to get the name of the Active sheet in a workbook?

Sougata Ghosh 161 Reputation points
2022-02-18T02:05:31.547+00:00

I want to get the name of the ActiveSheet of a workbook. But the following command is not working: owb.ActiveSheet.Name. My line of thought was that since owb.ActiveSheet returns a worksheet object, we would be able to use the NAME property on it. But it does not work. Can anyone pls help.

Imports System
Imports Microsoft.Office.Interop
Module Program
    Dim oxl As Excel.Application
    Dim owbs As Excel.Workbooks
    Dim owb As Excel.Workbook
    Dim osheet As Excel.Worksheet
    Sub Main(args As String())
        oxl = CreateObject("Excel.Application")
        oxl.Visible = True
        owb = oxl.Workbooks.Add
        osheet = owb.Worksheets.Add(, , 5,)
        Console.WriteLine("Active Sheet: {0}", owb.ActiveSheet.Name) 'This is not working
        Console.ReadLine()
    End Sub
End Module
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,712 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,833 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,501 Reputation points Microsoft Vendor
    2022-02-18T09:54:27.257+00:00

    @Sougata Ghosh , you could try the following code to get the active sheet of workbook.

     Sub Main()  
            Dim oxl As Excel.Application  
            Dim owbs As Excel.Workbooks  
            Dim owb As Excel.Workbook  
            Dim osheet As Excel.Worksheet  
            oxl = CreateObject("Excel.Application")  
            oxl.Visible = True  
            owb = oxl.Workbooks.Open("C:\\Users\\username\\Desktop\\test.xlsx")  
            osheet = owb.ActiveSheet  
            Console.WriteLine("Active Sheet: {0}", osheet.Name)   
            Console.ReadLine()  
        End Sub  
    

    Best regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,401 Reputation points
    2022-02-18T12:58:00.41+00:00

    Even though you have a solution I think it's prudent to bring up that using a strong type library (free) such as SpreadSheetLight (Excel is not required to be installed) makes life much easier. Example setting the active work sheet than asking what it is.

    Public Shared Sub ChangeActiveSheet(fileName As String, worksheet As String)
        Using doc As New SLDocument(fileName)
            doc.SelectWorksheet(worksheet)
            doc.Save()
        End Using
    End Sub
    Public Shared Function GetActiveSheet(fileName As String) As String
        Using doc As New SLDocument(fileName)
            Return doc.GetCurrentWorksheetName()
        End Using
    End Function
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.