VBA: Updating a master sheet based off 2 conditions

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I have a report that I am pulling from our database daily (Sheet 2). I am using this report to update information on a master spreadsheet (Sheet 1). Consider sheet 1 as a running total that is updated daily through a script and sheet 2 is information specific to today that I need to add onto sheet 1 under specific circumstances. There are 2 things that I need from this:

1) For any row on sheet 2 where the contents of the cell D and O on the same row are the same as any row on sheet 1 then do nothing. (Think of columns D and O as a double identifier. Under the first scenario I am copying over anything from sheet 2 to sheet 1 that doesn't already exist. I can tell if it exists based off the double identifier.)
If no row on sheet 1 contains contains this double identifier consider this a new row and copy the entire row from sheet 2 to the bottom of sheet 1.

2) On the same workbook as before, lets say there is a row on sheet where the contents of cells D and O don't exist on any row on sheet 2. Under this scenario I would want to fill the row from column A to column T with a light green color on Sheet 1 AND put today's date into that row on column S on sheet 1.
This is checking to see if there was a change from yesterday when I pulled this from our database to today. I can see that there was a change because on my master sheet 1 there is a value that no longer exists on sheet 2. This is determined by the double identifier above. I want to mark this change by filling the row from A to T with a light green color and by putting today's date into column S.

Thank you in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Are the values in D & O unique when combined?
for if you had England in D2 & UK in O2 could you have that combination again?
 
Upvote 0
How about
Code:
Sub CheckUpdate()

   Dim Cl As Range
   Dim ValU As String
   Dim Itm As Variant
   Dim Sht1 As Worksheet
   Dim Sht2 As Worksheet
   
   Set Sht1 = Sheets("Master")
   Set Sht2 = Sheets("Postcodes")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sht2.Range("D2", Sht2.Range("D" & Rows.Count).End(xlUp))
         ValU = Cl.Value & "|" & Cl.Offset(, 11).Value
         If Not .exists(ValU) Then .Add ValU, Cl.Offset(, -3)
      Next Cl
      For Each Cl In Sht1.Range("D2", Sht1.Range("D" & Rows.Count).End(xlUp))
         ValU = Cl.Value & "|" & Cl.Offset(, 11).Value
         If Not .exists(ValU) Then
            Cl.Offset(, -3).Resize(, 20).Interior.Color = 5296274
            Cl.Offset(, 15).Value = Date
         Else
            .Item(ValU) = vbNullString
         End If
      Next Cl
      For Each Itm In .items
         If Not Itm = vbNullString Then
            Itm.EntireRow.Copy Sht1.Range("A" & Rows.Count).End(xlUp).Offset(1)
         End If
      Next Itm
   End With
         
End Sub
 
Upvote 0
Are the values in D & O unique when combined?
for if you had England in D2 & UK in O2 could you have that combination again?

Sorry for the delayed response here. The combination of D2 and O2 would be a unique value.
 
Upvote 0
How about
Code:
Sub CheckUpdate()

   Dim Cl As Range
   Dim ValU As String
   Dim Itm As Variant
   Dim Sht1 As Worksheet
   Dim Sht2 As Worksheet
   
   Set Sht1 = Sheets("Master")
   Set Sht2 = Sheets("Postcodes")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sht2.Range("D2", Sht2.Range("D" & Rows.Count).End(xlUp))
         ValU = Cl.Value & "|" & Cl.Offset(, 11).Value
         If Not .exists(ValU) Then .Add ValU, Cl.Offset(, -3)
      Next Cl
      For Each Cl In Sht1.Range("D2", Sht1.Range("D" & Rows.Count).End(xlUp))
         ValU = Cl.Value & "|" & Cl.Offset(, 11).Value
         If Not .exists(ValU) Then
            Cl.Offset(, -3).Resize(, 20).Interior.Color = 5296274
            Cl.Offset(, 15).Value = Date
         Else
            .Item(ValU) = vbNullString
         End If
      Next Cl
      For Each Itm In .items
         If Not Itm = vbNullString Then
            Itm.EntireRow.Copy Sht1.Range("A" & Rows.Count).End(xlUp).Offset(1)
         End If
      Next Itm
   End With
         
End Sub

Hi Fluff,

I am running into an issue with this code and am working on trying to fix it.

2) On the same workbook as before, lets say there is a row on sheet where the contents of cells D and O don't exist on any row on sheet 2. Under this scenario I would want to fill the row from column A to column T with a light green color on Sheet 1 AND put today's date into that row on column S on sheet 1.

This is doing as attending. The problem is that I am updating a master spreadsheet daily through this script. What this means is that there could be yesterdays date already in column S. This script is currently changing yesterdays date to the day the script was ran every day.

Is it possible to check to see if that row in column S is empty before updating it to today's date?

Thank you so much!
 
Upvote 0
How about
Code:
If Not .exists(ValU) Then
   Cl.Offset(, -3).Resize(, 20).Interior.Color = 5296274
   If Cl.Offset(, 15) = "" Then Cl.Offset(, 15).Value = Date
Else
 
Upvote 0
Solution

Forum statistics

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