Please Help...

G

Guest

Hi,

I had a table of data and would like to go the following... please help...
A B C D
1 Andy 78 98 85
2 Jane 45 65 96
3 David 12 32 41
4 Sam 23 74 52

If I type in "Andy" in Cell 1, I like Cell 2 to display 78, Cell 3 to
display 98 and Cell 4 to display 85 automatically.

Please help. Thank you.
 
B

Bob Phillips

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1:A4" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Select Case .Value
Case "Andy"
.Offset(0, 1).Value = 78
.Offset(0, 2).Value = 98
.Offset(0, 3).Value = 85
Case "Jane"
.Offset(0, 1).Value = 45
.Offset(0, 2).Value = 65
.Offset(0, 3).Value = 96
Case "David"
.Offset(0, 1).Value = 12
.Offset(0, 2).Value = 32
.Offset(0, 3).Value = 41
Case "Sam"
.Offset(0, 1).Value = 23
.Offset(0, 2).Value = 74
.Offset(0, 3).Value = 52
End Select
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
R

Rick Rothstein \(MVP - VB\)

newbie said:
Hi,

I had a table of data and would like to go the following... please help...
A B C D
1 Andy 78 98 85
2 Jane 45 65 96
3 David 12 32 41
4 Sam 23 74 52

If I type in "Andy" in Cell 1, I like Cell 2 to display 78, Cell 3 to
display 98 and Cell 4 to display 85 automatically.

Let us assume that Cell 1 is A10, Cell 2 is B10, Cell 3 is C10 and Cell 4 is
D10, then put this formula in B10 (Cell 2)...

=VLOOKUP($A$10,$A$1:$D$4,COLUMN(B2),FALSE)

and copy it across to D10 (Cell 4). Type the name (Andy for you example)
into A10 (Cell 1). Adjust the $A$10 reference if you chose to type the name
in a different cell.

Rick
 
G

Guest

Thanks alot! It work like magic... Would really appreciate if you could
explain how it work. I tried to read the "Help" provided by Microsoft but
couldn't really understand.

Thanks again...
 
R

Rick Rothstein \(MVP - VB\)

Okay, let's look at the syntax for the VLOOKUP function from the help
files...

VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)

The first argument (lookup_value) is the item you want to find. You called
that Cell 1 and I said to assume Cell 1 was A10. Whatever is typed into Cell
1 (A10) is what will be looked up by the function.

The second item (table_array) is the range for the table of values you want
to look into in order to find whatever is in the first argument. For your
given data, that was A1:D4.

At this point, there are two things to note. First, the 'lookup_value'
(first argument) will be searched for only in the first column of the range
given for the table in 'table_array' (second argument). Second, the cell
reference in the first argument and the range reference in the second
argument were all given as absolute references (the $ preceding the row and
column designators). Why? Because I knew that after constructing the
formula, it was going to be copied to other cells. Since the location of the
'lookup_value' and the table's location was fixed, I did not want the
references to be incremented when copied. The absolute references guaranteed
that wouldn't happen.

Okay, now, the third argument (col_index) is the number of the column you
want to return your value from. The column is numbered within the table, not
by the actual column on the spreadsheet. By that, I mean if your table were
located at F10:J13, column "F" would be the table's first column, column "G"
its second column, etc. So, the column we want to look up the value in, for
the formula located in A11 (Cell 2), was 2. When we copy the formula across
to Cell 3 and Cell 4, the column numbers from the table for them will be 3
and 4. You are probably wondering why I didn't simply put 2 in for the third
argument (for the formula located in Cell 2). Well, because, when you copied
that formula across to the other cells, the 2 (a constant number) would not
be incremented. I needed something that would increment when copied. So, I
called upon the Column function to do that. Unsurprisingly, the Column
function returns the column number for reference cell given as it argument
(or the first cell of a range reference). So, I used that in order to get a
reference that would increment when copied. Column(B2) returns 2 because the
"B" in B2 is column number 2 in the spreadsheet (not because it also happens
to be the second column in the table). When the formula is copied across,
the B2 reference will become C2 and D2 respectively in next cells copied to
(and the column function will return 3 and 4 for them, thus pulling data
from the next columns in the table). Just one more mention on this... if the
table had been located in, say, F10:J13, then Column(F11) would not have
been sufficient for the column number. Remember, the column number for the
VLOOKUP function is the column's number within the table itself, not the
spreadsheet in general. Column(F11) would return, obviously, 11 and not the
2 we needed. For this situation, we would have had to use COLUMN(F11)-9 for
the third argument in order to feed it the 2 necessary for the second
(table) column lookup.

Finally, for the last argument, I used FALSE, which told the VLOOKUP
function that the first column was not in a sorted order. If we used TRUE or
omitted the argument altogether (the default value for this optional
argument is TRUE), then VLOOKUP would have assumed the values in the table's
first column were in ascending sort order. One more thing about this
argument... if FALSE is used, then the search that takes place is for and
exact match to the 'lookup_value' (first argument) and if TRUE is used, the
search will locate exact or approximate matches to the 'lookup_value'.

I think that covers it all... I hope it helped you some.

Rick
 
G

Guest

Thanks alot, Rick. It has been a great help.


Rick Rothstein (MVP - VB) said:
Okay, let's look at the syntax for the VLOOKUP function from the help
files...

VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)

The first argument (lookup_value) is the item you want to find. You called
that Cell 1 and I said to assume Cell 1 was A10. Whatever is typed into Cell
1 (A10) is what will be looked up by the function.

The second item (table_array) is the range for the table of values you want
to look into in order to find whatever is in the first argument. For your
given data, that was A1:D4.

At this point, there are two things to note. First, the 'lookup_value'
(first argument) will be searched for only in the first column of the range
given for the table in 'table_array' (second argument). Second, the cell
reference in the first argument and the range reference in the second
argument were all given as absolute references (the $ preceding the row and
column designators). Why? Because I knew that after constructing the
formula, it was going to be copied to other cells. Since the location of the
'lookup_value' and the table's location was fixed, I did not want the
references to be incremented when copied. The absolute references guaranteed
that wouldn't happen.

Okay, now, the third argument (col_index) is the number of the column you
want to return your value from. The column is numbered within the table, not
by the actual column on the spreadsheet. By that, I mean if your table were
located at F10:J13, column "F" would be the table's first column, column "G"
its second column, etc. So, the column we want to look up the value in, for
the formula located in A11 (Cell 2), was 2. When we copy the formula across
to Cell 3 and Cell 4, the column numbers from the table for them will be 3
and 4. You are probably wondering why I didn't simply put 2 in for the third
argument (for the formula located in Cell 2). Well, because, when you copied
that formula across to the other cells, the 2 (a constant number) would not
be incremented. I needed something that would increment when copied. So, I
called upon the Column function to do that. Unsurprisingly, the Column
function returns the column number for reference cell given as it argument
(or the first cell of a range reference). So, I used that in order to get a
reference that would increment when copied. Column(B2) returns 2 because the
"B" in B2 is column number 2 in the spreadsheet (not because it also happens
to be the second column in the table). When the formula is copied across,
the B2 reference will become C2 and D2 respectively in next cells copied to
(and the column function will return 3 and 4 for them, thus pulling data
from the next columns in the table). Just one more mention on this... if the
table had been located in, say, F10:J13, then Column(F11) would not have
been sufficient for the column number. Remember, the column number for the
VLOOKUP function is the column's number within the table itself, not the
spreadsheet in general. Column(F11) would return, obviously, 11 and not the
2 we needed. For this situation, we would have had to use COLUMN(F11)-9 for
the third argument in order to feed it the 2 necessary for the second
(table) column lookup.

Finally, for the last argument, I used FALSE, which told the VLOOKUP
function that the first column was not in a sorted order. If we used TRUE or
omitted the argument altogether (the default value for this optional
argument is TRUE), then VLOOKUP would have assumed the values in the table's
first column were in ascending sort order. One more thing about this
argument... if FALSE is used, then the search that takes place is for and
exact match to the 'lookup_value' (first argument) and if TRUE is used, the
search will locate exact or approximate matches to the 'lookup_value'.

I think that covers it all... I hope it helped you some.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top