Need VBA suggestions to import and update excel data

SchellElec

New Member
Joined
Jun 29, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
We keep track of our job history in an excel file, using data that we export from Quickbooks. I'm given a file weekly to append new rows and update existing rows, and we want to keep all the data, even if it is no longer on the weekly update file.

I'd like to know if a macro can achieve the following:
Run a macro from the summary file.
Select the weekly update file to be used.
VBA Code:
Set WB2 = Workbooks.Open(Application.GetOpenFilename(Title:="Select the new file with the new info", MultiSelect:=False))
seems to work well.
For each row in the update file, search if the job already exists in summary file. If so, update that row. If not, add row to bottom.
When finished, sort the sheet so that jobs are in alphanumeric order.

For example, the summary file is formatted like this:
Job#ReceiptsCOGSGross ProfitTotal ExpenseNet Income
J3662-055211.005021.36189.6488.97100.67
J3663-27C602.64280.77321.8722.77299.10
J3708-11C783.20500.82282.3848.96233.42

A weekly update file may look like this:
Job#ReceiptCOGSGross ProfitTotal ExpenseNet Income
J3663-27C602.64315.36287.2838.77248.51
J3668-17C495.80312.14183.6612.67170.99
J3709-15C1199.20737.00462.2039.76422.44

And my desired output for the summary file would look like this:
Job#ReceiptsCOGSGross ProfitTotal ExpenseNet Income
J3662-055211.005021.36189.6488.97100.67
J3663-27C602.64315.36287.2838.77248.51
J3668-17C495.80312.14183.6612.67170.99
J3708-11C783.20500.82282.3848.96233.42
J3709-15C1199.20737.00462.2039.76422.44
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The easiest way to do this may not be VBA but Power Query. Look at this video on how to set it up.
 
Upvote 0
@SchellElec. welcome to MrExcel.
Try this code:
You need to amend the sheet reference to suit.
VBA Code:
Sub SchellElec2()
Dim c As Range, a As Range, b As Range
Dim n As Long
Dim wb1 As Workbook, wb2 As Workbook
Application.ScreenUpdating = False


Set wb1 = ThisWorkbook
With wb1.Sheets("Sheet1")  'change to suit
    n = .Range("A" & Rows.Count).End(xlUp).Row
Set a = .Range("A1:A" & n)
End With


Set wb2 = Workbooks.Open(Application.GetOpenFilename(Title:="Select the new file with the new info", MultiSelect:=False))

With wb2.Sheets("Sheet1") ''change to suit
    Set f = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With a
    For Each c In f
        res = Application.Match(c.Value, a, 0)
            If IsNumeric(res) Then
                .Cells(res, 1).Resize(, 6).Value = c.Resize(, 6).Value
            Else
                n = n + 1
                .Cells(n).Resize(, 6).Value = c.Resize(, 6).Value
            End If
    Next
End With

Set a = a.Resize(n, 6)
With a
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
End With
Application.ScreenUpdating = True

End Sub
 
Upvote 1
Solution
@SchellElec. welcome to MrExcel.
Try this code:
You need to amend the sheet reference to suit.
VBA Code:
Sub SchellElec2()
Dim c As Range, a As Range, b As Range
Dim n As Long
Dim wb1 As Workbook, wb2 As Workbook
Application.ScreenUpdating = False


Set wb1 = ThisWorkbook
With wb1.Sheets("Sheet1")  'change to suit
    n = .Range("A" & Rows.Count).End(xlUp).Row
Set a = .Range("A1:A" & n)
End With


Set wb2 = Workbooks.Open(Application.GetOpenFilename(Title:="Select the new file with the new info", MultiSelect:=False))

With wb2.Sheets("Sheet1") ''change to suit
    Set f = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With a
    For Each c In f
        res = Application.Match(c.Value, a, 0)
            If IsNumeric(res) Then
                .Cells(res, 1).Resize(, 6).Value = c.Resize(, 6).Value
            Else
                n = n + 1
                .Cells(n).Resize(, 6).Value = c.Resize(, 6).Value
            End If
    Next
End With

Set a = a.Resize(n, 6)
With a
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
End With
Application.ScreenUpdating = True

End Sub

@Akuini This works beautifully. Thank you so much!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 1

Forum statistics

Threads
1,224,825
Messages
6,181,190
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