Which worksheet is active...

Posted on 2004-11-08
Last Modified: 2010-05-02
For i=1 to wb.Worksheets.count
Set ws=wb.Worksheets(i)
if ws.Selected=True Then MsgBox ws.Name '<-- this code doen't give the active or selected ws name.
Next i

What is correct code to do this?

Question by:Mike Eghtebas
    LVL 5

    Expert Comment

    The ActiveSheet object will always have the active sheet.  Can get it's .Name property
    LVL 5

    Accepted Solution

    To clarify ...

    msgbox wb.ActiveSheet.Name
    LVL 8

    Expert Comment

    As RougeSolutions said,

    Dim i As Integer
    Dim ws As Worksheet
    Dim wb As Workbook

    Set wb = ActiveWorkbook

    For i = 1 To wb.Worksheets.Count
        Set ws = wb.Worksheets(i)
        If ws.Name = "Sheet2" Then MsgBox ws.Name '<-- this code doen't give the active or selected ws name.
    Next i

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now