Match and retrieve values from another workbook

Cortex1000

New Member
Joined
Jul 30, 2022
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
Hello Everyone,

I am struggling creating a VBA code dealing with data retrieval from another opened workbook. I have a main workbook on which the data is manually being fed in from a secondary workbook. At the minute this is a copy and paste job. I am looking to automate this by pressing a button to call a VBA that will sit in Personal.XLSB. I am quite new to VBA and I am learning hitting my head to all of these problems.

I use column E (test5) to match the data between the workbooks.

Unfortunately I am not able to use XL2BB. I hope you can retrieve the table.

Main Workbook (where the info needs to go in)
test1test2test3test4test5test6test7test8test9test10test11test12
info0R2000R22102312345info10info20info30info40info50
R2000R22102312332
info1R2000R22102312342info11info21info31info41info51
R2000R22102312312
R2000R22102312324
info2R2000R22102312350info12info22info32info42info52
R2000R22102312330

Secondary Workbook (where the info is)
test1test2test3test4test5test6test7test8test9test10test11test12
info0R2000R22102312345info10info20info30info40info50
info1R2000R22102312342info11info21info31info41info51
info2R2000R22102312350info12info22info32info42info52
info3R2000R22102312332info13info23info33info43info53
info4R2000R22102312312info14info24info34info44info54
info5R2000R22102312324info15info25info35info45info55
info6R2000R22102312330info16info26info36info46info56

This code works on small scale but the problem is the Main Workbook has thousands of rows that are already been filled in and it just crashes excel.
The code I have works by opening it automatically from desktop which I don't really need now as secondary workbook will be always open when transferring the data.
VBA Code:
Sub WithButton()
Dim KeyCells As Range
Dim Sheet1, Sheet2 As Worksheet
Dim CellChanged As Integer
Dim Path, File As String
Dim LastRow, LastData As Long
Dim Found As Boolean

Application.ScreenUpdating = False
Application.Calculation = xlCalculationAutomatic

On Error GoTo Handle

            Set Sheet1 = ThisWorkbook.Worksheets("record") 'Edit Sheet File1

            If Sheet1.Range("Z1").Value = "" Then
    Sheet1.Range("Z1").Value = 0
    CellChanged = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
End If

If Sheet1.Cells(Rows.Count, "E").End(xlUp).Row > Sheet1.Range("Z1").Value Then
    Path = "C:\Users\user\Desktop\secondwb.xls" 'Edit Path File2
    File = Right$(Path, Len(Path) - InStrRev(Path, "\"))

    CellChanged = Sheet1.Range("Z1").Value + 1
    Workbooks.Open (Path)
            Set Sheet2 = Workbooks(File).Worksheets("data") 'Edit Sheet of File2

               LastRow = Sheet2.Cells(Rows.Count, "E").End(xlUp).Row
    LastData = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row

    For I = 1 To LastRow
        If Sheet1.Range("E" & CellChanged).Value = Sheet2.Range("E" & I) Then
            Sheet1.Range("D" & CellChanged).Value = Sheet2.Range("D" & I).Value
            Sheet1.Range("F" & CellChanged).Value = Sheet2.Range("F" & I).Value
            Sheet1.Range("G" & CellChanged).Value = Sheet2.Range("G" & I).Value
            Sheet1.Range("H" & CellChanged).Value = Sheet2.Range("H" & I).Value
            Sheet1.Range("I" & CellChanged).Value = Sheet2.Range("I" & I).Value
            Sheet1.Range("J" & CellChanged).Value = Sheet2.Range("J" & I).Value
            Found = True
        End If
        If Found = True Or I = LastRow Then
            If CellChanged = LastData Then
                Exit For
            End If
        If Found = True Then
            Found = False
            CellChanged = CellChanged + 1
        Else
            CellChanged = CellChanged + 1
            End If
            I = 0
        End If
    Next I
    Workbooks(File).Close savechanges:=False
                Sheet1.Range("Z1").Value = CellChanged
End If
Exit Sub
Handle:
    MsgBox ("Error")
End Sub


Now the question is.. Is anybody whiling to have a look into this and help me with a shorter option that works with high amount of data? Both workbooks will be opened when retrieving data so I think it wont need a path but I am happy to understand your perspective.

Thank you,
David
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi David

You can make a few space saving changes, resizing contiguous ranges could be quicker then writing line by line.
Change the codename of Sheet1 to sh_Record in the properties panel
Create a reference for the destination workbook.

I haven't tested this, but it should give you a few ideas

VBA Code:
Option Explicit

Sub WithButton()
Dim KeyCells As Range
'Dim Sheet1, Sheet2 As Worksheet ' you can remove the first sheet ref here, see below
Dim sh_Data As Worksheet ' new variable for sheet2
Dim CellChanged As Integer
Dim LastRow, LastData As Long
Dim Found As Boolean
Dim wb2 As Workbook ' added a variable for the workbook you open

Application.ScreenUpdating = False
Application.Calculation = xlCalculationAutomatic

On Error GoTo Handle

' /*
' rename ThisWorkbook.Worksheets("record") as sh_Record
' you can remove everything from /* to */
'Set Sheet1 = ThisWorkbook.Worksheets("record") 'Edit Sheet File1
' */


With sh_Record
    If .Range("Z1").Value = "" Then
        .Range("Z1").Value = 0
        CellChanged = .Cells(Rows.Count, "E").End(xlUp).Row
    End If
    
    If .Cells(Rows.Count, "E").End(xlUp).Row > .Range("Z1").Value Then
        Set wb2 = Workbooks.Open("C:\Users\user\Desktop\secondwb.xls")
        Set sh_Data = wb2.Worksheets("data") 'Edit Sheet of File2
        
        CellChanged = .Range("Z1").Value + 1
        LastRow = sh_Data.Cells(Rows.Count, "E").End(xlUp).Row
        LastData = .Cells(Rows.Count, "E").End(xlUp).Row
    
        For I = 1 To LastRow
            If .Range("E" & CellChanged).Value = sh_Data.Range("E" & I) Then
                .Range("D" & CellChanged).Value = sh_Data.Range("D" & I).Value
                .Range("F" & CellChanged).Resize(, 5).Value = sh_Data.Range("F" & I).Resize(, 5).Value
                Found = True
            End If
            If Found = True Or I = LastRow Then
                If CellChanged = LastData Then
                    Exit For
                End If
            If Found = True Then
                Found = False
                CellChanged = CellChanged + 1
            Else
                CellChanged = CellChanged + 1
                End If
                I = 0
            End If
        Next I
        wb2.Close False
        .Range("Z1").Value = CellChanged
    End If
End With
Exit Sub
Handle:
    MsgBox ("Error")
End Sub
 
Upvote 1
Hello Dave,

Thanks very much for your assistance. I believe I got myself more lost than I was in the first instance. I will keep try to make it work somehow. Thanks again for you help.

Kind Regards
David
 
Upvote 0
Hello Everyone,

Me again :(. I have been working on this for few hours and I still can't make it work. Is someone willing to have a look into this?
Does anyone know a easier way to pull the information with a code similar to this?

Appreciate your interest.
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,510
Members
452,650
Latest member
Tinfish

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