Pasting Data Based On Drop Down

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
Hi all,

So I have an Excel Worksheet with a data validation drop down. I am trying to have data pasted based on said drop down. The data validation drop down is constantly changing.

Based on the table provided, I need Sheet 2 to populate based on a data validation drop down provided in cell A1 and A2. The thing is, the Data Validation drop down on sheet 2 is changing numbers. So one time it might be 1 and the next time it is used, it might be 500. The numbers in this case are 1 and 45.

Sheet 1
ABCD
1NumberAnimalFoodDrink
21CatDryMilk
345DogWetWater

<tbody>
</tbody>







Sheet 2
ABCD
1"Drop Down Here" Auto PopAuto PopAuto Pop
2"Drop Down Here"Auto PopAuto PopAuto Pop

<tbody>
</tbody>

So If 45 Is chosen on Sheet 2, A1, then The B-D columns from sheet 1 will be pasted into sheet 2's columns. Same goes for 1.

All help is greatly appreciated. I can try to explain more, if it is needed. Thanks in advance.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this:
Copy the code then right click sheet2 tab > select View Code > paste the code.

Code:
[FONT=lucida console][COLOR=Royalblue]Private[/COLOR] [COLOR=Royalblue]Sub[/COLOR] Worksheet_Change([COLOR=Royalblue]ByVal[/COLOR] Target [COLOR=Royalblue]As[/COLOR] Range)

[COLOR=Royalblue]If[/COLOR] Target.Cells.CountLarge <> [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
    [COLOR=Royalblue]If[/COLOR] [COLOR=Royalblue]Not[/COLOR] Intersect(Target, Range([COLOR=brown]"A:A"[/COLOR])) [COLOR=Royalblue]Is[/COLOR] [COLOR=Royalblue]Nothing[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]With[/COLOR] Sheets([COLOR=brown]"Sheet1"[/COLOR])
        
        [COLOR=Royalblue]Dim[/COLOR] res
        res = Application.Match(Target.Value, .Range([COLOR=brown]"A1"[/COLOR], .Cells(Rows.Count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp)), [COLOR=Royalblue]False[/COLOR])
            [COLOR=Royalblue]If[/COLOR] IsNumeric(res) [COLOR=Royalblue]Then[/COLOR]
            Application.EnableEvents = [COLOR=Royalblue]False[/COLOR]
            Target.Offset(, [COLOR=crimson]1[/COLOR]).Resize(, [COLOR=crimson]3[/COLOR]).Value = .Range([COLOR=brown]"B"[/COLOR] & res & [COLOR=brown]":D"[/COLOR] & res).Value
            Application.EnableEvents = [COLOR=Royalblue]True[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
        
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR]
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Try this:
Copy the code then right click sheet2 tab > select View Code > paste the code.

Code:
[FONT=lucida console][COLOR=Royalblue]Private[/COLOR] [COLOR=Royalblue]Sub[/COLOR] Worksheet_Change([COLOR=Royalblue]ByVal[/COLOR] Target [COLOR=Royalblue]As[/COLOR] Range)

[COLOR=Royalblue]If[/COLOR] Target.Cells.CountLarge <> [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]Then[/COLOR] [COLOR=Royalblue]Exit[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
    [COLOR=Royalblue]If[/COLOR] [COLOR=Royalblue]Not[/COLOR] Intersect(Target, Range([COLOR=brown]"A:A"[/COLOR])) [COLOR=Royalblue]Is[/COLOR] [COLOR=Royalblue]Nothing[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]With[/COLOR] Sheets([COLOR=brown]"Sheet1"[/COLOR])
        
        [COLOR=Royalblue]Dim[/COLOR] res
        res = Application.Match(Target.Value, .Range([COLOR=brown]"A1"[/COLOR], .Cells(Rows.Count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp)), [COLOR=Royalblue]False[/COLOR])
            [COLOR=Royalblue]If[/COLOR] IsNumeric(res) [COLOR=Royalblue]Then[/COLOR]
            Application.EnableEvents = [COLOR=Royalblue]False[/COLOR]
            Target.Offset(, [COLOR=crimson]1[/COLOR]).Resize(, [COLOR=crimson]3[/COLOR]).Value = .Range([COLOR=brown]"B"[/COLOR] & res & [COLOR=brown]":D"[/COLOR] & res).Value
            Application.EnableEvents = [COLOR=Royalblue]True[/COLOR]
            [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
        
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR]
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
This worked for the example sheet I provided. How would you change the code to where it grabs only specific cells (i.e, B and D but not C)?
 
Upvote 0
This worked for the example sheet I provided. How would you change the code to where it grabs only specific cells (i.e, B and D but not C)?
Change this:
Target.Offset(, 1).Resize(, 3).Value = .Range("B" & res & ":D" & res).Value

to this:
Target.Offset(, 1).Value = .Range("B" & res).Value
Target.Offset(, 2).Value = .Range("D" & res).Value
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

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