Consulting

Results 1 to 3 of 3

Thread: Need code to copy data from closed workbook

  1. #1

    Need code to copy data from closed workbook

    Hi,
    I have a workbook with various sheets. First sheet of my current workbook is "StartData". I have more data files with a sheet name "EndData".
    I want to copy data from Range B5:BP250 of Sheet "EndData" of any closed workbook. (User will select which file to get data from). The data need to be pasted in the current work book in the sheet "StartData" in B5:BP250

    Thanks in advance.

  2. #2
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,302
    Location
    Hi gspandi0503 welcome to the forum,

    Perhaps the below will get you started:
    Sub Test()
        Dim sFile As String, tWb As Workbook, tWs As Worksheet
        Dim sWb As Workbook, sWs As Worksheet
    
        Set tWb = ThisWorkbook
        Set tWs = tWb.Sheets("StartData")
    
        With Application.FileDialog(msoFileDialogFilePicker)
            If .Show = -1 Then
                sFile = .SelectedItems(1)
            End If
        End With
        
        If sFile <> "" Then
            Set sWb = Workbooks.Open(sFile)
            DoEvents
            Set sWs = sWb.Sheets("EndData")
            tWs.Range("B5:BP250").Value = sWs.Range("B5:BP250").Value
            sWb.Close False
        End If
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

  3. #3
    Thank you so much. It worked. Cheers...

Posting Permissions

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