Google Answers Logo
View Question
 
Q: Excel function to populate a table from data in an existing table ( Answered 5 out of 5 stars,   6 Comments )
Question  
Subject: Excel function to populate a table from data in an existing table
Category: Miscellaneous
Asked by: ptkelly-ga
List Price: $10.00
Posted: 29 Jul 2004 08:08 PDT
Expires: 28 Aug 2004 08:08 PDT
Question ID: 380772
Have a table that i use to track all of my stock positions, both long
(i.e. positive values) and shorts (i.e. negative values), and
performance results.  How do I elegantly create a 2 dynamic tables
pulling data from my master position sheet separating my long and
short positions in order to view & evaluate them independantly?

Have tried all the varieties of lookup & array functions i know and
can't come up with something that is fairly automated

Request for Question Clarification by maniac-ga on 29 Jul 2004 17:27 PDT
Hello Ptkelly,

There are a few ways that your problem can be solved:
 - a macro can certainly generate / update the tables from your master
position sheet. It could even run from a button (or everytime you open
the sheet) if you like.
 - you could sort the data in the master sheet based on "value" to
give you three regions of data (long, no position, short). This would
leave the data in the master position sheet but allow you to view the
longs (and shorts) in groups.
 - you can filter the data in the master position sheet show it only
shows the long or short positions. Then you can go back to showing all
the data if desired. This does not actually make the dynamic tables -
just shows you the long / short records in the original table. This is
perhaps the easiest method to describe.
 - you can generate a "long" and "short" table and do a simple update
after each update to the master table. It is a little less automatic
than the first method but should still look OK.

Which way do you want your problem solved so I can prepare a proper answer?
  --Maniac

Clarification of Question by ptkelly-ga on 29 Jul 2004 18:34 PDT
sounds like the macro way you described would help me the best, from a
button would be ideal.  please let me know if there's anything
additional you'd need from me.

thanks!
Answer  
Subject: Re: Excel function to populate a table from data in an existing table
Answered By: maniac-ga on 30 Jul 2004 16:36 PDT
Rated:5 out of 5 stars
 
Hello Ptkelly,

A copy of the macro follows at the end, but I will describe the steps
taken to create it if you want to make your own (to do this or a
similar task).

Recording the Macro
===================

[0] Before you start... I started w/ a new spreadsheet w/ 3 sheets. In
your case, you could do this with a copy of your "master position
sheet" as long as you have the extra sheets for the long / short
tables. Use
  Insert -> Worksheet
and drag/drop the sheet tabs at the bottom to order the sheets them
the way you like.

[1] I entered some dummy data into the first sheet as follows:

Symbol	How much	Value
AAA	100	10
BBB	200	20
AAA	100	15
CCC	100	-10
DDD	100	-20
EEE	200	30
(seven rows, three column)

If your existing data has some other layout, make the changes I note
in the various steps that follow (or in the macro at the end).

[2] Select the upper left cell use:
  Tools -> Macro -> Record New Macro
a dialog box appears, I changed the name to LS, and then selected OK.
I got a floating toolbar w/ a "stop" button and "relative reference"
button. I moved the toolbar out of the way (but where I can still see
it).

[3] Then I used
  Data -> Filter -> Autofilter
and saw the three headings replaced by a set of pop up buttons. Each
should have a little up/down arrow icon on the right. I'll represent
that as ^v in the text below.

[4] Select
  Value^v -> Custom Filter  [or whatever your value column title is]
(be sure to click on ^v in the Value cell to get the pop up menu) and
another dialog box should appear. Change Equals to "Is Greater Than"
and set the value to zero. Select OK and you should see just your long
positions. On my system, the ^v changed from black to a blue color.

[5] Select
  Edit -> Go To -> Special -> Current Region
and then OK. At this point the upper left region will be selected with
all of your long positions included.

[6] Select
  Edit -> Copy
Go to the second sheet, select the upper right cell and select
  Edit -> Paste
The cells representing your long position should now be on the second sheet.

[7] Go back to the first sheet and repeat steps 4-6 with the following differences:
 - [4] Select "Is Less Than" as the filter method
 - [5] Don't forget to reselect the current region
 - [6] paste into the third sheet

[8] Go back to the first sheet and select
  Data -> Filter -> Auto Filter
to turn off the filtering and select the upper left cell. Finally
select the stop button to stop recording the macro. It took about 10
steps to get to this point and the macro is complete.

Copy the Macro
==============

As an alternative to recording the macro you can copy the text of the
macro at the end and then:

[1] Select
  Tools -> Macro -> Visual Basic Editor
a couple windows will pop up.

[2] Select
  Insert -> Module
and a window will pop up.

[3] Paste the macro into the window. Change the 
  Field:=3
to whatever column number has your value data in the two places it
appears. Change the sheet numbers (1, 2, 3) to the sheets in your
workbook if you did not want the sheets ordered as Master Data, Long,
Short.

[4] Select
  Quit and Return to Microsoft Excel.
At this point, you should be ready to run the macro as noted in the
next section. It should also be saved with the worksheet of master
position data. If you want to save the macro in another worksheet, let
me know with a clarification request so I can describe that process.

Updating the Tables
===================

When you update the master position sheet, the other sheets won't be
updated until you rerun the macro. To do that, simply:
  Tools -> Macro
select LS and then
  Run
and the two other sheets will be updated again. On my system, it took
a few seconds for the cursor to stop blinking and the sheets were
updated.

Adding the Button
=================

To add the button, do the following steps.

[0] Make sure the Forms toolbar is shown. If not, try
  Tools -> Customize
and make sure Forms has a checkmark (as a toolbar) before using OK.

[1] The "button" on my system is the fourth toolbar item but its
position will vary based on the width / height of the toolbar. I just
pointed at the choices with the mouse until one said "button" and then
I selected it. Click / drag across the part of the sheet where you
want the button. A dialog box shows up and set the macro name to LS.
Select OK.

[2] At this point, the button can be resized, moved, and you can
change the label. I named mine "update" but you can use whatever word
you want. Click outside the button when you have it the way you want.

[3] Verify it works OK by clicking the button again and checking the
long / short sheets. They should be updated by the macro.

Looking at the Macro
====================

If you are interested, you can review the source code of the macro by using:
  Tools -> Macro
make sure LS is selected, and then
  Edit
to bring up the visual basic editor. It should look something like the
listing below. To return to Excel, use
  Close and Return to Microsoft Excel

That covers everything I can think of. The answer gives you two ways
to get the macro into your worksheet, tells you how to add buttons,
and gives you an idea how to create your own macros using the record
features in Excel.

If any step of this process is unclear or does not work for you -
please use a clarification request. I will be glad to help. Good luck
with your stock picks and I hope will help you.

  --Maniac

Sub LS()
'
' LS Macro
' Macro recorded 7/30/2004 by Maniac
'

'
    Range("A1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=3, Criteria1:=">0", Operator:=xlAnd
    Selection.CurrentRegion.Select
    Selection.Copy
    Sheets(2).Select
    ActiveSheet.Paste
    Sheets(1).Select
    Selection.AutoFilter Field:=3, Criteria1:="<0", Operator:=xlAnd
    Selection.CurrentRegion.Select
    Selection.Copy
    Sheets(3).Select
    ActiveSheet.Paste
    Sheets(1).Select
    Selection.AutoFilter
    Range("A1").Select
End Sub
ptkelly-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Great, thanks for the help.  Very clear and concise answer to my
problem with great directions.  Thanks again!

Comments  
Subject: Re: Excel function to populate a table from data in an existing table
From: gareththewormevans-ga on 29 Jul 2004 08:34 PDT
 
What about a Pivot table?
Subject: Re: Excel function to populate a table from data in an existing table
From: ptkelly-ga on 29 Jul 2004 09:03 PDT
 
extracting the data is the problem.  don't think a pivot table solves that.
Subject: Re: Excel function to populate a table from data in an existing table
From: ptkelly-ga on 29 Jul 2004 09:19 PDT
 
isn't a pivot table just a way to sort things?

one problem for me is my table is not a contiguous list of data, i
have longs (positive values), shorts (negative values), and positions
i no longer have (0 value). so just want to see active positions and
don't want to have any manual sorting to do.

thanks though, let me know if i'm wrong here.

what i was hoping was there was some sort of array function that would
just scan my exisitng position table and return a list of values for
the condition > or < 0 and then i could just do a look up for the
other information.   but standard if function in array returns false
result which leaves a blank result in my table.
Subject: Re: Excel function to populate a table from data in an existing table
From: delard-ga on 29 Jul 2004 10:29 PDT
 
Hi,

I've got a small sheet that demonstrates how I think this can be done
- but its a bit complicated to explain - can I post it to you (but you
would have to put your email address on here?). Alternatively if you
can point me towards somewhere I can post attachments I'll do that...

- Delard
Subject: Re: Excel function to populate a table from data in an existing table
From: just4fun2-ga on 29 Jul 2004 11:35 PDT
 
If you look at the bottom of the excel worksheet you will see Sheet1
Sheet2 Sheet3.  Select sheet2, this will bring up a new worksheet that
is connected to the your current worksheet.  Just referance the data
from sheet1 to sheet2.
Subject: Re: Excel function to populate a table from data in an existing table
From: delard-ga on 30 Jul 2004 03:09 PDT
 
Hi,

I don't think you need to resort to macros or buttons - take a look at
this example :

http://uk.geocities.com/s_ridgway@btinternet.com/longshorttables.xls

This sheet randomly fills in the positions (long or short) for a bunch
of stocks - then splits those into two subtables depending on the
sign. Its just simple formulae - no macros etc.

Hope that helps.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy