How does one use Excel VBA to merge data between workbooks & Import new recently added data?

GPSmith

New Member
Joined
Jan 1, 2019
Messages
1
Hi All

I am very green to VBA so apologies for the massive gaps in my knowledge or if this has already been answered but I am struggling to find a code that covers what i would like to do.

So i have to two workbooks, WB1 has a list of my projects (address and job database which contains the sheet "Job_list") the other (WB2) also lists these projects but is used for a different purpose, currently defined as "Sheet15". Currently i manually enter new rows in the second workbook containing sheet15 and physically copy and paste the new projects names and numbers over from the "job_list". Simple enough but wondered if this could be automated with VBA.

I need to bring in the project number I allocate (column A in WB1) and paste it in column B in WB2. As well as and the Name (column F in WB1) and paste into column C in WB2.

Can the data set be compared and rows inserted to suit the new data? Is there a way to only check data added since the last time the macro was run?

I have had a go at the code based on what i have found but all help would be appreciated as i am getting an runtime error 9 for the "else" part and I am just a tad overwhelmed by how to solve it. If i need to post somewhere else then guidance would be appreciated.
Thanks in advance.

Code:
Option Explicit
 
Sub InsertJobs()
 
    Dim wbkA As Variant
    Dim varSheetA As Variant
    Dim varSheetB As Variant
    Dim strRangeToCheck As String
    Dim iRow As Long
    Dim iCol As Long
      
    Set wbkA = Workbooks.Open(Filename:="P:\Address_&_Job_Database.xls")
    Set varSheetA = wbkA.Worksheets("Job_List") ' or whatever sheet you need
 
    strRangeToCheck = "A1:G500"
    ' If you know the data will only be in a smaller range, reduce the size of the ranges above.
    Debug.Print Now
    varSheetA.Range (strRangeToCheck)
    varSheetB = Worksheets(Sheet15).Range(strRangeToCheck) ' or whatever your other sheet is.
    Debug.Print Now
 
    For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
        For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
            If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
                ' Cells are identical.
                ' Do nothing.
            Else
                ' Cells are different.
                Sheets(varSheetA).Range("A" & "F").Copy Destination:=Sheets("January_2019").Range("B" & "C")
 
    Dim i As Long, r1 As Range, r2 As Range
    For i = 4 To 500
      Set r1 = Range("A" & i)
      Set r2 = Range("B" & i & ":C" & i)
    Next i
            End If
        Next iCol
    Next iRow
 
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,214,419
Messages
6,119,389
Members
448,891
Latest member
tpierce

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