importing data - check if already exist replace or else insert in the bottom

Hawjeen

New Member
Joined
Feb 23, 2018
Messages
23
Hi,

I got a task that are a bit over my current vba skills, and i was hoping that someone could help me out.

i need to import some data regulary and update a database.

what my need is, to check if the data in column A in sheet2 (new data) already is in column A in data (database).

if yes it should check if the value in column sheet2.R=data.R, if that is not the case, i would like it to replace the values from A:R. If it is the same it can just go to next value in sheet2.column A.

if the value in sheet2.column A dosent exist, i want to copy values from A:R and insert in the bottom of my table in sheet.data.

i hope this is understandeable, and that someone can save me from a lot of manual work in the future.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
Code:
Sub UpdateData()

   Dim Cl As Range
   Dim Itm As Variant
   Dim Dws As Worksheet
   Dim Sws As Worksheet
   
   Set Dws = Sheets("Data")
   Set Sws = Sheets("New Data")
   With CreateObject("scripting.dictionary")
      For Each Cl In Sws.Range("A2", Sws.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Array(Cl.Offset(, 17).Value, Cl.Resize(, 18))
      Next Cl
      For Each Cl In Dws.Range("A2", Dws.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            If Not Cl.Offset(, 17).Value = .Item(Cl.Value)(0) Then
               Cl.Resize(, 18).Value = .Item(Cl.Value)(1).Value
            End If
            .Remove Cl.Value
         End If
      Next Cl
      If Not .Count > 0 Then Exit Sub
      For Each Itm In .items
         Dws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 18).Value = Itm(1).Value
      Next Itm
   End With
            
End Sub
 
Upvote 0
Not sure why you are getting that.
Did you copy & paste the code, or type it out?
 
Upvote 0
i copied.
but i simply just deleted the line which triggered the error, and now it seems to run.

but i can see a small problem, it looks like it is pasting all lines from the "new data", even though it is in the database and no differnce in column R
 
Upvote 0
Without that line of code, the entire New Data sheet will be copied over.
Lets try it the other way round
Code:
Sub UpdateData()

   Dim Cl As Range
   Dim Dws As Worksheet
   Dim Sws As Worksheet
   
   Set Dws = Sheets("Data")
   Set Sws = Sheets("New Data")
   With CreateObject("scripting.dictionary")
      For Each Cl In Dws.Range("A2", Dws.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Array(Cl.Offset(, 17).Value, Cl.Resize(, 18))
      Next Cl
      For Each Cl In Sws.Range("A2", Sws.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            If Not Cl.Offset(, 17).Value = .Item(Cl.Value)(0) Then
               .Item(Cl.Value)(1).Value = Cl.Resize(, 18).Value
            End If
         Else
            Dws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 18).Value = Cl.Resize(, 18).Value
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
I have recently started using vba and wanted to know that where and how can i specify the 2 sheets I want to use for this program ?
 
Upvote 0
You do that with the two lines in red
Rich (BB code):
Sub UpdateData()

   Dim Cl As Range
   Dim Dws As Worksheet
   Dim Sws As Worksheet
   
   Set Dws = Sheets("Data")
   Set Sws = Sheets("New Data")
   With CreateObject("scripting.dictionary")
      For Each Cl In Dws.Range("A2", Dws.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Array(Cl.Offset(, 17).Value, Cl.Resize(, 18))
      Next Cl
      For Each Cl In Sws.Range("A2", Sws.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            If Not Cl.Offset(, 17).Value = .Item(Cl.Value)(0) Then
               .Item(Cl.Value)(1).Value = Cl.Resize(, 18).Value
            End If
         Else
            Dws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 18).Value = Cl.Resize(, 18).Value
         End If
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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