PC Review


Reply
Thread Tools Rate Thread

Create function to find a value in an array in seperate worksheet

 
 
jag
Guest
Posts: n/a
 
      30th May 2008
I am looking to create a VB fuction to use a stock reference in a cell
(A) from one workbook, to reference the correct column of information
corresponding to cell A and return the contents of the cell based its
3rd element and the 4th element.
Example:
Sheet1
A
B C
IBM
1/10 112.34

=ATMSTRIKE(A1,B1,C1) would return from the "B" column in Sheet2 "WIB
US 1/10 C110 Equity" by looking up 3 things 1) IBM,
2) the "1/10" as the 3rd element
3) and the smallest absolute value of the difference between
the number in the 4th element and number in C1
which is 112.34

Sheet2
A
B C
dell ibm msft
DLQ US 11 P22 Equity IBM US 6 C75 Equity MSQ US 6 C24
Equity
DLQ US 11 P23 Equity IBM US 6 C80 Equity MSQ US 6 C25
Equity
VPZ US 1 C10 Equity IBM US 7 P90 Equity MSQ US 7
C22.50 Equity
VPZ US 1 C15 Equity IBM US 10 P90 Equity MSQ US 7
C24 Equity
VPZ US 1 C17.50 Equity IBM US 1 P110 Equity MSQ US 1 C40
Equity
WDQ US 1/10 C17.50 Equity IBM US 1 P115 Equity MSQ US 1 C42.50
Equity
WDQ US 1/10 C20 Equity WIB US 1/10 C100 Equity WMF US 1/10
C35 Equity
WDQ US 1/10 C25 Equity WIB US 1/10 C110 Equity WMF US 1/10
C37.50 Equity
WIB US 1/10 C120 Equity WMF US 1/10
C40 Equity
WIB US 1/10 C125 Equity

I would assume you would use multiple functions to create one such as:

Function ExtractElement (txt,n,Seperator)
Dim AllElements As Variant
AllElements = Split(txt,Seperator)
ExtractElement = AllElements(n-1)
End Function

and also Match and Loops, just having hard time putting it all
together

Thanks in advance,
JAG
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      30th May 2008
Function ATMSTRIKE(Company As String, _
Fraction As String, Equity As Single)

With Sheets("Sheet2")
'get company column, search for company name in row 1
Set c = .Rows(1).Find(what:=Company, _
LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
LastRow = .Cells(Rows.Count, c.Column).End(xlUp).Row
RowCount = 2
ATMSTRIKE = ""
Do While RowCount <= LastRow
Element = .Cells(RowCount, c.Column)
'split fields by spaces
SplitElement = Split(Element)
SplitFraction = SplitElement(2)
SplitEquity = SplitElement(3)
'remove first letter and convert to a number
SplitEquity = Val(Mid(SplitEquity, 2))
If SplitFraction = Fraction Then
If BestElement = "" Then
BestEquity = SplitEquity
ATMSTRIKE = Element
Else
If Abs(SplitEquity - Equity) < _
Abs(BestEquity - Equity) Then

BestEquity = SplitEquity
ATMSTRIKE = Element

End If
End If
End If
RowCount = RowCount + 1
Loop
Else
ATMSTRIKE = "Company: " & Company & " Not found"
End If
End With

End Function


"jag" wrote:

> I am looking to create a VB fuction to use a stock reference in a cell
> (A) from one workbook, to reference the correct column of information
> corresponding to cell A and return the contents of the cell based its
> 3rd element and the 4th element.
> Example:
> Sheet1
> A
> B C
> IBM
> 1/10 112.34
>
> =ATMSTRIKE(A1,B1,C1) would return from the "B" column in Sheet2 "WIB
> US 1/10 C110 Equity" by looking up 3 things 1) IBM,
> 2) the "1/10" as the 3rd element
> 3) and the smallest absolute value of the difference between
> the number in the 4th element and number in C1
> which is 112.34
>
> Sheet2
> A
> B C
> dell ibm msft
> DLQ US 11 P22 Equity IBM US 6 C75 Equity MSQ US 6 C24
> Equity
> DLQ US 11 P23 Equity IBM US 6 C80 Equity MSQ US 6 C25
> Equity
> VPZ US 1 C10 Equity IBM US 7 P90 Equity MSQ US 7
> C22.50 Equity
> VPZ US 1 C15 Equity IBM US 10 P90 Equity MSQ US 7
> C24 Equity
> VPZ US 1 C17.50 Equity IBM US 1 P110 Equity MSQ US 1 C40
> Equity
> WDQ US 1/10 C17.50 Equity IBM US 1 P115 Equity MSQ US 1 C42.50
> Equity
> WDQ US 1/10 C20 Equity WIB US 1/10 C100 Equity WMF US 1/10
> C35 Equity
> WDQ US 1/10 C25 Equity WIB US 1/10 C110 Equity WMF US 1/10
> C37.50 Equity
> WIB US 1/10 C120 Equity WMF US 1/10
> C40 Equity
> WIB US 1/10 C125 Equity
>
> I would assume you would use multiple functions to create one such as:
>
> Function ExtractElement (txt,n,Seperator)
> Dim AllElements As Variant
> AllElements = Split(txt,Seperator)
> ExtractElement = AllElements(n-1)
> End Function
>
> and also Match and Loops, just having hard time putting it all
> together
>
> Thanks in advance,
> JAG
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying rows identified by countifs function to seperate worksheet JRD Microsoft Excel Discussion 6 1st Apr 2010 05:17 AM
How do I display the result of a function on seperate worksheet e. Beth Microsoft Excel Worksheet Functions 3 15th Jul 2008 05:30 AM
Worksheet Function to Create Array of Size n with values x Through Dial222 Microsoft Excel Misc 1 6th Dec 2007 11:21 AM
Create seperate excel worksheet for each change in Access Producer =?Utf-8?B?V2h5IG1lPw==?= Microsoft Access Database Table Design 1 24th May 2006 11:29 PM
want to create a template and update in seperate worksheet =?Utf-8?B?bGF0YQ==?= Microsoft Excel New Users 0 23rd Feb 2005 07:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:20 PM.