VBA Code: Look for specific values and copy/paste

Boudifct

New Member
Joined
Apr 9, 2008
Messages
12
My question is the following:
I have 2 excel worksheet.
In the 1 worksheet there is an empty template (with 4 rows and 5 columns).
In the 2 worksheet there are 20 templates with values in the 4 rows and 5 columns. Each template has a different name.
In the 1st worksheet there is also a list with the 20 different template's names.
What I would like to do is that when I select one name, Excel will look for the corresponding one in the 2nd worksheet and automatically display the related values in the empty template.
Maybe it might help you with a little illustration of the problem:
Worksheet "empty template":

"PICK THE NAME OF THE TEMPLATE" (in a cell)
B C D E F G
2
3
4
5


Worksheet "templates":

3 NAME OF TEMPLATE 1
B C D E
5 10% 13%
6 20%
7 35% 14%
8 20%

3 NAME OF TEMPLATE 2
F G I J
5 14%
6 20% 32%
7
8 18%


3 NAME OF TEMPLATE 3
K L M N
5 14%
6
7 20%
8

So let's assume that I choose "NAME OF TEMPLATE 3" then automatically, the value 14% and 20% will be displayed in the "empty template"

Hope I was clear enough and that you can help me on this one. Thanks a million
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Peter,

I have tried and it did work, it indicates me that there is a mistake in the formula here (in bold):

=INDEX($A$1:$N$5,ROWS(C$9:C9),MATCH($A$9,$A$1:$N$1,0)-1+COLUMNS($C9:C9))

Moreover, since I have around 20 templates it it a bit different than your example. To be more specific the 20 templates are in the following area (A3:AL76).

It is why my first thought was for a VBA Code instead of a regular formula
 
Upvote 0
It is pretty unclear just how big each template is and what rows/columns they are in. Even your small example says each one is 4 rows and 5 columns but the samples look more like 5 rows and 4 columns (and what happened to column H in Template 2?).

So, what I suggest is that you set up a new test workbook with sheets and code just like below and see if that does the sort of thing you want. If so, see if you can modify the code to suit your actual data.

First, my 'templates' sheet (colours just to show the area of each of my templates).

Excel Workbook
ABCDEFGHIJKLMN
1
2
3Template 1Template 2Template 3
4
510%13%14%14%
620%20%32%
735%14%20%
820%18%
928%15%25%15%
10
templates



Now my 'empty template' sheet. The template name is chosen in cell E1 by Data Validation drawn from a list in column K.

Excel Workbook
CDEFGHIJKL
1Pick Template -->Template 2Template 1
2Template 2
314%Template 3
420%32%
5
618%
715%25%15%
8
empty template
#VALUE!



To implement ...

1. Right click the sheet name tab of the 'empty template' sheet and choose 'View Code'.

2. Copy the code below and paste into the main right hand pane that opens at step 1.

3. Close the VB window and try changing cell E1 to one of the other template names.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Range("E1")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> Sheets("templates").Rows(3)<br>            Range("E3").Resize(5, 4).Value = .Find(What:=Target.Value, _<br>                After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, _<br>                MatchCase:=False, SearchFormat:=False).Offset(2).Resize(5, 4).Value<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,432
Members
448,961
Latest member
nzskater

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