VBA Code for VLOOKUP/Data Update

GreatGemini

New Member
Joined
Aug 25, 2016
Messages
4
Hi,
Its 4 part question, just want to start with part 1:
Here is what I badly need:


1. Lookup Pre Production Data's Product ID in Post Production Data
2. Check all Parts IDs data, if any of the Part Code has changed
3. If YES, insert "Upgraded" in Status column and if not changed than insert "Existing" ..... Continue...
3. If YES than in New Data under that part ID change existing value to "Upgraded" otherwise "Existing"
4. If Product ID is not found change part code to "Discontinue"

Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
Pre Production Data:
2
Product IDPart1 IDPart2 IDPart3 IDPart4 IDPar5 IDPart6 IDPart7 IDPart8 IDPart9 IDPart10 IDPart11 IDPart12 IDPart13 IDPart14 IDPart15 IDPart16 IDPart17 ID
3
Multi Deco 12KIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
4
Multi Deco 12KINL-6021INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
5
Multi Deco 1EIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
6
Multi Deco 1EINL-6021INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
7
Multi Deco 12KIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
8
Multi Deco 12KINL-6021INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
9
Old ProductIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
10
11
12
Post Production Data:
13
Product IDPart1 IDPart2 IDPart3 IDPart4 IDPar5 IDPart6 IDPart7 IDPart8 IDPart9 IDPart10 IDPart11 IDPart12 IDPart13 IDPart14 IDPart15 IDPart16 IDPart17 ID
14
Multi Deco 12KIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
15
Multi Deco 12KINL-6020INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
16
Multi Deco 1EIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
17
Multi Deco 1EINL-6020INL-6022INL-6059INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
18
Multi Deco 12KIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
19
Multi Deco 12KINL-6021INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
20
New ProductIDK-6021IDK-6022IDK-6058IDK-6059IDK-6066IDK-6070IDK-6071IDK-6077IDK-6078IDK-6084IDK-6085IDK-6086IDL-2612IDL-2613IDL-2614IDL-2615IDL-2616
21
Multi Deco 1EINL-6021INL-6022INL-6058INL-6059INL-6066INL-6070INL-6071INL-6077INL-6078INL-6084INL-6085INL-6086INO-2612INO-2613INO-2614INO-2615INO-2616
22
23
24
sample result
25
New Data
26
Product IDStatusPart1 IDPart2 IDPart3 IDPart4 IDPar5 IDPart6 IDPart7 IDPart8 IDPart9 IDPart10 IDPart11 IDPart12 IDPart13 IDPart14 IDPart15 IDPart16 IDPart17 ID
27
Multi Deco 12KExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
28
Multi Deco 12KUpgradedUpgradedExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
29
Multi Deco 1EExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
30
Multi Deco 1EUpgradedExistingExistingUpgradedExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
31
Multi Deco 12KExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
32
Multi Deco 12KExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExistingExisting
33
Old ProductDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinueDiscontinue
34
Multi Deco 1ENew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew ProductNew Product

<tbody>
</tbody>
Sheet: Sheet1

<tbody>
</tbody>

Any help would be much appreciated.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You have duplicate Product ID's. I was going to create formulas that gave you what you needed, but the product ID column must contain no duplicate values for that to work. I think if I were to create a macro to do the same, the Product ID's would still have to be changed also.
 
Last edited:
Upvote 0
There are around 650,000 records so I think Macro would be better than formula. Just a thought!
650,000 records?!?!

Do you know what would even better? A database program, since that is what you are really dealing with (something like Access, SQL, MySQL, or Oracle).
You can manipulate Excel to work like a database, but you often will find that the performance will be pretty poor (as that is not really what Excel was designed for).
 
Upvote 0
Try this. You will need to create some named ranges. These are based on your example, but the named ranges can be changed to reflect your true data set.

PreHeaders = C2:S2 (the headers for pre production)
PrePID = B3:B9 (the product ID's for pre production
PostHeaders = C13:S13
PostPID = B14:B21
PreTable = C3:S9 (the pre production table range not including the headers and product ID's)
PostTable = C14:S21
StatusTable = B26:T34 (The entire status table including the headers and product ID's)

We may have to change some logic, but this is a start.


Code:
Sub StatusTableUpdate()
  Dim PreHeaders As Range
  Dim PrePID As Range
  Dim PostHeaders As Range
  Dim PostPID As Range
  Dim PreTable As Range
  Dim PostTable As Range
  Dim StatusTable As Range
  Dim PIDCol As Long
  Dim StatusCol As Long
  Dim HdrsRow As Long
  Dim X As Long
  Dim Y As Long
  Dim FirstCol As Long
  Dim LastCol As Long
  Dim FirstRow As Long
  Dim LastRow As Long
  Dim TL As Range
  Dim PID As String
  Dim PartID As String
  Dim PrePart As String
  Dim PostPart As String
  Dim NewDataStat As String
  
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  
  Set PreHeaders = Range("PreHeaders")
  Set PrePID = Range("PrePID")
  Set PostHeaders = Range("PostHeaders")
  Set PostPID = Range("PostPID")
  Set PreTable = Range("PreTable")
  Set PostTable = Range("PostTable")
  Set StatusTable = Range("StatusTable")
  
  Set TL = StatusTable.Resize(1, 1)
  PIDCol = TL.Column
  StatusCol = PIDCol + 1
  HdrsRow = TL.Row
  FirstCol = PIDCol + 2
  LastCol = StatusTable.Columns.Count + PIDCol - 1
  FirstRow = TL.Row + 1
  LastRow = StatusTable.Rows.Count + HdrsRow - 1
  
  For Y = FirstRow To LastRow
    NewDataStat = "Existing"
    PID = Cells(Y, PIDCol).Value
    For X = FirstCol To LastCol
      
      PartID = Cells(HdrsRow, X).Value
      PrePart = "NA"
      PostPart = "NA"
      On Error Resume Next
      PrePart = Application.Index(PreTable, Application.Match(PID, PrePID, 0), Application.Match(PartID, PreHeaders, 0))
      PostPart = Application.Index(PostTable, Application.Match(PID, PostPID, 0), Application.Match(PartID, PostHeaders, 0))
      On Error GoTo 0
      
      If PrePart = PostPart Then
        Cells(Y, X).Value = "Existing"
      ElseIf PrePart = "NA" Or PostPart = "NA" Then
        Cells(Y, X).Value = "Discontinue"
        NewDataStat = "Discontinue"
      ElseIf PrePart <> PostPart Then
        Cells(Y, X).Value = "Upgraded"
        NewDataStat = "Upgraded"
      End If
    Next X
    
    Cells(Y, StatusCol) = NewDataStat
  Next Y
  
      
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  
  
End Sub
 
Upvote 0
The nice part about using named ranges is that you can change the focus of the range without having to alter the macro. I would definitely consider using dynamic named ranges.

Since your data set is so large, you may want to put each of those tables on a different sheet. Using Workbook level named ranges will aid in this process.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

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