PDA

View Full Version : [SOLVED:] Need code to copy data from closed workbook



gspandi0503
06-01-2022, 02:59 AM
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.

georgiboy
06-07-2022, 02:14 AM
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

gspandi0503
06-07-2022, 05:32 AM
Thank you so much. It worked. Cheers...