Consulting

Results 1 to 9 of 9

Thread: Solved: Copying data from another workbook which name is unknown

  1. #1

    Solved: Copying data from another workbook which name is unknown

    Hi,

    My situation is as follows:

    I have 2 open workbooks and I'd like to copy data from one workbook to the other workbook. Assuming that I don't know the name of the workbook from which I want to copy the data, is there a simple way to do this?

    Thanks!

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Which is copying to which?

  3. #3
    Copy data from the workbook which name is unknown, to the workbook which name is known.

  4. #4
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Why don't you try opening the unknown workbook from the known workbook and then the file name will be "known" to VBA

  5. #5
    How can I open it if I can't refer to it by its name?

  6. #6
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Use the FileOpenDialog box to select the file and then transfer that value to a variable. Then VBA will know the name of the file.

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by NomalAnomaly
    Copy data from the workbook which name is unknown, to the workbook which name is known.
    Okay, here is a very simple example. We could use a userform (see attached) with a listbox to list any other open workbooks.

    The userform and code goes in the 'known' wb.

    Option Explicit
        
    Private Sub cmdCancel_Click()
        Unload Me
    End Sub
        
    Private Sub cmdOK_Click()
    Dim wb As Workbook
        
        Set wb = Workbooks(lstOpenWBs.Value)
        
        'do whatever copying
        wb.Worksheets(1).Copy _
            After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
        Unload Me
    End Sub
        
    Private Sub UserForm_Initialize()
    Dim wb As Workbook
        
        cmdOK.Enabled = False
        
        If Workbooks.Count > 1 Then
            For Each wb In Workbooks
                If Not wb.Name = ThisWorkbook.Name Then
                    lstOpenWBs.AddItem wb.Name
                End If
            Next
            lstOpenWBs.ListIndex = 0
            cmdOK.Enabled = True
        Else
            Me.Caption = "No Other Workbooks Open!"
        End If
    End Sub
    Hope that helps,

    Mark

  8. #8
    Hi Mark

    Thank you very much for your reply.

    The above code will certainly do it.

    NormalAnomaly

  9. #9
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Glad to help :-)

Posting Permissions

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