How to pass values from one sheet to another sheet if condition met?

Rampage598

New Member
Joined
Mar 11, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
This is my scenario
1648009337930.png

I want to pass the values from sheet2 to sheet1 according to ID...in here from sheet2 id= 1 data=a will go into the sheet1 id=1 data column....
This is my expected output
1648009415843.png

Is there any possible way to do this in excel using macros?
Can anyone help me with this?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
kindly try below code (My working code from another thread I'm working on...)
also, you may want to search first for other thread post here for a similar problem :)

Code:
Sub A_PROB104_MREXCEL_Rampage598()

    Dim Row4Sheet2 As Integer, LRow As Long, iCTR As Integer, WSCopy As Worksheet
    Set WSCopy = ActiveSheet
    ActiveSheet.Select
    'Count Row even with Empty Cells
    LRow = ActiveSheet.UsedRange.rows.Count + ActiveSheet.UsedRange.rows(1).Row

    iCTR = 2
    Row4Sheet2 = 2
    Do While iCTR < LRow
        If Not IsEmpty(Cells(iCTR, 2)) Then
            WSCopy.Range("A" & iCTR & ":B" & iCTR).Copy Worksheets("PAGE02").Range("A" & Row4Sheet2)
            Row4Sheet2 = Row4Sheet2 + 1
        End If
        iCTR = iCTR + 1
    Loop

End Sub
 
Upvote 0
Try:
VBA Code:
Sub CompareID()
    Application.ScreenUpdating = False
    Dim i As Long, srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object
    Set srcWS = Sheets("Sheet2")
    Set desWS = Sheets("Sheet1")
    v1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    v2 = desWS.Range("A2", desWS.Range("A" & Rows.Count).End(xlUp)).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, 2)
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            desWS.Range("B" & i + 1) = dic(v2(i, 1))
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,571
Members
452,652
Latest member
eduedu

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