Required Macros to compare the value and need to take unique value and write in another column

dineshkummar

New Member
Joined
Apr 18, 2022
Messages
18
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have two Excel sheets with named :

Patching_MainSheet and ESL_Inventory

In Patching_MainSheet D column have 100 Servernames and ESL_Inventory Sheet A Column have 1845 Servernames and also I column have DataCentre name.
I need Macros to take one servername from Patching_MainSheet of D column and need to compare with 1845 Servernames in A Columns of ESL_Inventory Sheet if it matches, then it have to take one particular value(DataCentre Name) which is present I columns in ESL_Inventory and write down in B column of Patching_MainSheet.
Anyone guide me to prepare macros for this..

Thanks DK
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here (de-sensitized if necessary).
 
Upvote 0
DC NameHost Name
WSAP01
WSAP02
CDCP01
CDCP02
DFCP04
DFCP06
REGP09
EDCP05
GECP07

Host NameDC Name
REGP09London
CDCP02Slovika
WSAP02India
WSAP01Germany
GECP07France
WSAP02UK
DFCP04France
EDCP05Spain
DFCP06India

Pls find the sample sheet..

I have two Excel sheets with named :

Patching_MainSheet and ESL_Inventory

In Patching_MainSheet D column have 100 Servernames and ESL_Inventory Sheet A Column have 1845 Servernames and also I column have DataCentre name.
I need Macros to take one servername from Patching_MainSheet of D column and need to compare with 1845 Servernames in A Columns of ESL_Inventory Sheet if it matches, then it have to take one particular value(DataCentre Name) which is present I columns in ESL_Inventory and write down in B column of Patching_MainSheet.
Anyone guide me to prepare macros for this..
 
Upvote 0
Try:
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object, i As Long
    Set desWS = Sheets("Patching_MainSheet")
    Set srcWS = Sheets("ESL_Inventory Sheet")
    v1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 9).Value
    v2 = desWS.Range("D2", desWS.Range("D" & Rows.Count).End(xlUp)).Resize(, 9).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        If Not dic.exists(v1(i, 1)) Then
            dic.Add v1(i, 1), v1(i, 9)
        End If
    Next i
    For i = LBound(v2) To UBound(v1)
        If dic.exists(v2(i, 1)) Then
            desWS.Range("A" & i + 1) = dic(v2(i, 1))
        End If
    Next i
    Application.ScreenUpdating = True
End Sub[CODE=vba]
[/CODE]
 
Upvote 0
Try:
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object, i As Long
    Set desWS = Sheets("Patching_MainSheet")
    Set srcWS = Sheets("ESL_Inventory Sheet")
    v1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 9).Value
    v2 = desWS.Range("D2", desWS.Range("D" & Rows.Count).End(xlUp)).Resize(, 9).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        If Not dic.exists(v1(i, 1)) Then
            dic.Add v1(i, 1), v1(i, 9)
        End If
    Next i
    For i = LBound(v2) To UBound(v1)
        If dic.exists(v2(i, 1)) Then
            desWS.Range("A" & i + 1) = dic(v2(i, 1))
        End If
    Next i
    Application.ScreenUpdating = True
End Sub[CODE=vba]
[/CODE]
really appreciated for quick response and thanks for the Macros,

when I ran the macros i am getting error Run-time error '9'
Subscript out of range
End Debug
 
Upvote 0
really appreciated for quick response and thanks for the Macros,

when I ran the macros i am getting error Run-time error '9'
Subscript out of range
End Debug
1650372159359.png
 
Upvote 0
Looks like there is a slight typo, try it like
Rich (BB code):
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
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