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
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Number[/TD]
[TD]Animal[/TD]
[TD]Food[/TD]
[TD]Drink
[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]Cat[/TD]
[TD]Dry[/TD]
[TD]Milk
[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]45[/TD]
[TD]Dog[/TD]
[TD]Wet[/TD]
[TD]Water[/TD]
[/TR]
</tbody>[/TABLE]







Sheet 2
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]"Drop Down Here" [/TD]
[TD]Auto Pop[/TD]
[TD]Auto Pop[/TD]
[TD]Auto Pop
[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]"Drop Down Here"[/TD]
[TD]Auto Pop[/TD]
[TD]Auto Pop[/TD]
[TD]Auto Pop[/TD]
[/TR]
</tbody>[/TABLE]

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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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