+ Reply to Thread
Results 1 to 2 of 2

Reference opened Listbox workbook in for loop

Hybrid View

  1. #1
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Reference opened Listbox workbook in for loop

    I am trying to reference the name of a workbook selected using a listbox in a for loop. I am simply copying worksheets from the workbook in the listbox to a new worksheet where the code is located. I keep getting an error. I changed a couple of things to make it easier to see where I wanted to reference the name. OPW is the name I'm referencing.


    'Opens each worksheet selected as individual spreadsheets
        Dim i, added_sheet_count As Integer
            Workbooks.Open ListBox1.List(i)
        'Name of open workbook
        OPW = ActiveWorkbook.Name
        
        Unload UserForm2
    
        'Moves individual sheets into the same workbook adding it to the first page position each time
        i = 0
        Counter_temp = 0
        For Each w In OPW
            If w.Name <> ThisWorkbook.Name Then
                w.Sheets(1).copy before:=ThisWorkbook.Sheets(1)
                w.Sheets(2).copy After:=ThisWorkbook.Sheets(1)
                w.Sheets(3).copy After:=ThisWorkbook.Sheets(2)
                w.Sheets(4).copy After:=ThisWorkbook.Sheets(3)
                
                'Check for Counter data in PA Sheet
                If w.Sheets(5).Cells(7, 1).Value <> Empty Then
                    w.Sheets(5).copy After:=ThisWorkbook.Sheets(4)
                    Counter_temp = 1
                End If
                
                i = i + 1
            End If
        Next w

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Reference opened Listbox workbook in for loop

    Hi,

    The syntax for getting the activeworkbook name is
    your_variable = ActiveWorkbook.Name

    Don't know whether this is actually what you want bit it seems to be what you're asking for

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1