Retrieving data from one workbook to another

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
During my search, I came across this script and I want the bigger brains help me to amend it for my use. I will copy the data from one workbook that's macro free and then paste it into another workbook that's is macro enabled. I don't know if I have to open it before I copy or there is a way I can copy without opening. Anything flexible will go a long way to help me.

Code:
Sub WorkbookLoop()
    Dim i As Integer
    Dim j As Integer
    Dim finalRow As Integer
    Dim masterWb As Worksheet
    Dim templateWb As Workbook
    Dim sPath As String
    Dim sFileName As String
    
    Set masterWb = ThisWorkbook.Sheets(1)
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row

    Workbooks.Open masterWb.Range("E1").Value & Range("C1").Value
    Set templateWb = ActiveWorkbook
    
    Application.DisplayAlerts = False
    On Error GoTo MissWB
    For i = 4 To finalRow
        sPath = masterWb.Range("B" & i).Value
        sFileName = masterWb.Range("A" & i).Value
        
        If ThisWorkbook.Sheets(1).Range("G" & i) <> "File Available. Data Copied" Then
            Workbooks.Open Filename:=sPath & sFileName
            
            For j = i To finalRow
            
                If Cells(j, 2).Value = sPath And Cells(j, 1).Value = sFileName Then
                    Range(masterWb.Range("D" & j).Value).Copy _
                        Destination:=templateWb.Sheets(masterWb.Range("E" & j).Value).Range(masterWb.Range("F" & j).Value)
                    master.Range("G" & j).Value = "File Available. Data Copied"
                    
                End If
                
            Next j
            Workbooks(sFileName).Close SaveChanges:=False
            
        End If
        
NextWB:
    Next i
    
    On Error GoTo 0
    
    MsgBox "Done."
    
    Set masterWb = Nothing
    Application.DisplayAlerts = True
    Exit Sub

    
MissWB:
    masterWb.Range("G" & i).Value = "File Not Available"
    Resume NextWB
End Sub
 
Upvote 0
Thanks Dave.
I am looking at the script. I don't really understand it that much. I will be glad you can explain the logic behind it for me here:

Thanks again
Kelly
 
Upvote 0
Thanks Dave.
I am looking at the script. I don't really understand it that much. I will be glad you can explain the logic behind it for me here:

Thanks again
Kelly

DatabaseToDataEntry code returns a contiguous range of values from a worksheet (database) to non- contiguous range of cells (input form)

DatabaseToDataEntry Code has two parameters

- InputFormRange As Range The range of cells in your data entry entry form

- Target As Range The Range to be returned in your database (only the first cell needs to be found in the range)

to return required database record to your data entry form

DatabaseToDataEntry DataEntryRange, Foundcell

Where DataEntryRange is the range of input cells & FoundCell is the required cell in your database.

Dave
 
Upvote 0
Hello Dave,
I ran the script and had an error on the line :
Code:
Set Foundcell = Worksheets("DATABASE").Range("B2")
Says run time error 9
Subscript out of range.

Also I am finding it difficult to understand the line:
Code:
Set DataEntryRange = ThisWorkbook.Worksheets("Entry Sheet").Range("C6,C8,F6,C10,G8,H12,D12")

What does this do?:

Code:
Range("C6,C8,F6,C10,G8,H12,D12")

Regards
Kelly
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top