Copy and Paste Problem

TexEth

New Member
Joined
Jan 24, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
I will like to copy data from Sheet2 to Sheet1 only if the required conditions on Sheet1 are met.
I will like to copy and paste data to Sheet1 only if Year (date) and Region on both sheets match. Please see below.

Sheet1 (Original)Sheet 2(Source)Sheet1 (copy completed)
YearRegionSalesYearRegionSalesYearRegionSales
2000​
AL
2002​
AL
2000​
AL
2001​
AL
2003​
AL
2000​
2001​
AL
2002​
AL
2004​
AL
2005​
2002​
AL
2003​
AL
2005​
AL
2010​
2003​
AL
2000​
2004​
AL
2002​
AK
2004​
AL
2005​
2005​
AL
2003​
AK
100​
2005​
AL
2010​
2000​
AK
2004​
AK
102​
2000​
AK
2001​
AK
2005​
AK
110​
2001​
AK
2002​
AK
2002​
AR
115​
2002​
AK
2003​
AK
2003​
AR
125​
2003​
AK
100​
2004​
AK
2004​
AR
132​
2004​
AK
102​
2005​
AK
2005​
AR
141​
2005​
AK
110​
2000​
AR
2000​
AR
2001​
AR
2001​
AR
2002​
AR
2002​
AR
115​
2003​
AR
2003​
AR
125​
2004​
AR
2004​
AR
132​
2005​
AR
2005​
AR
141​
 

Attachments

  • Copy&Paste Issue.PNG
    Copy&Paste Issue.PNG
    25.1 KB · Views: 5

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You can also do the same using a formula if you'd like.
VBA Code:
Sub wrjwg()
        Dim wk1 As Worksheet, wk2 As Worksheet
        Set wk1 = Sheets("destination")
        Set wk2 = Sheets("source")
        Dim lr1, lr2 As Long
        Dim k As Long
        Dim dic As Object
        Set dic = CreateObject("scripting.dictionary")
       
        lr1 = wk1.Range("A" & Rows.Count).End(xlUp).Row
        lr2 = wk2.Range("A" & Rows.Count).End(xlUp).Row
                       
        With wk2
            For k = 2 To lr2
                       dic(.Range("A" & k).Value & .Range("B" & k).Value) = .Range("C" & k).Value
            Next k
        End With
       
        With wk1
            For k = 2 To lr1
                    If dic.Exists(.Range("A" & k).Value & .Range("B" & k).Value) Then
                       .Range("C" & k).Value = dic(.Range("A" & k).Value & .Range("B" & k).Value)
                    End If
            Next k
        End With
       
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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