Consulting

Results 1 to 4 of 4

Thread: Help with activating sheets

  1. #1

    Help with activating sheets

    I have a VBA code that activates two different sheets in the same xlsx workbook. Or, at least I think it should. But the code below does not switch active sheets. The debug return text is always either 'Sheet1' repeated or 'Sheet5' repeated. Can someone help me understand why? This is running in powerpoint, by the way.

        'open excel spreadsheet    
        Set xlApp = New Excel.Application
        xlApp.Workbooks.Open xltbl, True, True
        xlApp.Workbooks(1).Worksheets(xlssheet).Activate
        q = ActiveSheet.UsedRange.Rows.Count
        For r = 2 To q
    
    
                xlApp.Workbooks(1).Worksheets("Sheet1").Activate
                Debug.Print ActiveSheet.Name
    
    
            
                'Change table
                xlApp.Workbooks(1).Worksheets("Sheet5").Activate
                Debug.Print ActiveSheet.Name

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you should have opened the thread in PowerPoint Help forum.

    try this:

    Sub test()
    'Tools - References - Microsoft Excel x.x Object Library
        
        Dim xlApp As Excel.Application
        Dim xlWbk As Excel.Workbook
        Dim xlSht As Excel.Worksheet
        
        Set xlApp = New Excel.Application
        xlApp.Visible = True 'or False
        Set xlWbk = xlApp.Workbooks.Open("C:\files\my_file.xlsm") 'change to suit
        
        For Each xlSht In xlWbk.Worksheets
            Debug.Print xlSht.Name & " - " & xlSht.UsedRange.Rows.Count
        Next
        
        xlApp.Quit
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It worked fine in my test.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thanks for the tips I got it to work in a similar fashion as mancubus indicated. And I'll find the powerpoint forum for future questions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •