Results 1 to 3 of 3

Thread: Searching for all open workbooks +

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Resolved Searching for all open workbooks +

    Hi everybody

    I have a problem with a piece of code that I would like to optimize a bit.

    What I currently have is;

    Code:
     Sub ovfData()
    Dim j As Double
    Dim k As Double
    Dim iSheet As Worksheet
    Dim nSheet As Worksheet
    Dim oSheet As Worksheet
    Dim ksheet As Worksheet
    Dim bsheet As Worksheet
    Dim dsheet As Worksheet
    Dim dtSheet As Worksheet
           
        'Names the different sheets to be copied
        Set dsheet = ThisWorkbook.Sheets("Data")
        Set dtSheet = Workbooks("A1.xls").Worksheets("Febos")
            
    Application.ScreenUpdating = False
      
    'Clears the sheet Data for new data to be transferred
    dsheet.Activate
    dsheet.Range(Cells(1, 1), Cells(65536, 256)).Clear
    dtSheet.Activate
    dtSheet.Range(Cells(1, 1), Cells(1, 1).End(xlToRight).End(xlDown)).Copy dsheet.Cells(1, 1)
    
    dsheet.Activate
    
    'Deletes empty columns
    For j = 256 To 1 Step -1
        If dsheet.Cells(1, j).Value = "" Then
            dsheet.Columns(j).Delete
        End If
    Next j
    
    Application.ScreenUpdating = True
    
    End Sub
    And what I would like is to avoid the part where I name the sheet form the 'other' workbook. I know upfront that my different workbooks are called (A1, A2, A3....A6, B1, B2) - but sometimes I don't have all of them open (The sheets in the workbooks are always named the same).

    So I would like to find a way to loop through the names, if the name of the workbook is open then copy the data - and then the rest of my program does some processing and I get an output.

    Any ideas?

    /Nicolaj
    Last edited by direktoren; Nov 12th, 2007 at 06:32 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Searching for all open workbooks +

    to get the open workbooks, try like
    vb Code:
    1. Dim w As Workbook
    2. For Each w In Workbooks
    3.     Debug.Print w.Name
    4. Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Searching for all open workbooks +

    Of course

    Thank you very much!

Posting Permissions

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



Click Here to Expand Forum to Full Width