Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 419
Default Using a 2 dimensional string array variable like a lookup function

Hello all,

How would I use a 2 dimensional string array like a vlookup/hlookup?

I'm thinking that my array will be something like this:

dim pstrDestinationPath(0 to [n], 0 to 1) as string

so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.

what I'm thinking of right now is just using a For loop to loop through each
element of the "first column" of the array, comparing it to a string. Then
using the For Loop control counter (after the match is found) as the index
for the "2nd column" of the array to return the matching path element.

Is this the correct way of doing this, or is there some other more straight
forward way?

Thanks for any help anyone can provide,

Conan Kelly


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Using a 2 dimensional string array variable like a lookup function

You can use VLOOKUP against an array in code. This would be much more
efficient than looping. For example,

Sub AAA()
Dim V As Variant
Dim Arr() As String
Dim LookupTerm As String

ReDim Arr(1 To 3, 1 To 2)
Arr(1, 1) = "term1"
Arr(1, 2) = "path1"
Arr(2, 1) = "term2"
Arr(2, 2) = "path2"
Arr(3, 1) = "term3"
Arr(3, 2) = "path3"

LookupTerm = "term1"
V = Application.VLookup(LookupTerm, Arr, 2)
If IsError(V) = True Then
Debug.Print "Term not found"
Else
Debug.Print "Path: " & V
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Conan Kelly" wrote in message
...
Hello all,

How would I use a 2 dimensional string array like a vlookup/hlookup?

I'm thinking that my array will be something like this:

dim pstrDestinationPath(0 to [n], 0 to 1) as string

so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.

what I'm thinking of right now is just using a For loop to loop through
each element of the "first column" of the array, comparing it to a string.
Then using the For Loop control counter (after the match is found) as the
index for the "2nd column" of the array to return the matching path
element.

Is this the correct way of doing this, or is there some other more
straight forward way?

Thanks for any help anyone can provide,

Conan Kelly


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 419
Default Using a 2 dimensional string array variable like a lookup function

Chip,

Thanks for info......much appreciated.

Thanks again,

Conan




"Chip Pearson" wrote in message
...
You can use VLOOKUP against an array in code. This would be much more
efficient than looping. For example,

Sub AAA()
Dim V As Variant
Dim Arr() As String
Dim LookupTerm As String

ReDim Arr(1 To 3, 1 To 2)
Arr(1, 1) = "term1"
Arr(1, 2) = "path1"
Arr(2, 1) = "term2"
Arr(2, 2) = "path2"
Arr(3, 1) = "term3"
Arr(3, 2) = "path3"

LookupTerm = "term1"
V = Application.VLookup(LookupTerm, Arr, 2)
If IsError(V) = True Then
Debug.Print "Term not found"
Else
Debug.Print "Path: " & V
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Conan Kelly" wrote in message
...
Hello all,

How would I use a 2 dimensional string array like a vlookup/hlookup?

I'm thinking that my array will be something like this:

dim pstrDestinationPath(0 to [n], 0 to 1) as string

so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.

what I'm thinking of right now is just using a For loop to loop through
each element of the "first column" of the array, comparing it to a
string. Then using the For Loop control counter (after the match is
found) as the index for the "2nd column" of the array to return the
matching path element.

Is this the correct way of doing this, or is there some other more
straight forward way?

Thanks for any help anyone can provide,

Conan Kelly




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 256
Default Using a 2 dimensional string array variable like a lookupfunction

Does this cause a cast from string array to a range? Or is
Application.Vlookup different from
Application.WorksheetFunction.Vlookup as far as argument types?


On Nov 29, 3:44 pm, "Chip Pearson" wrote:
You can use VLOOKUP against an array in code. This would be much more
efficient than looping. For example,

Sub AAA()
Dim V As Variant
Dim Arr() As String
Dim LookupTerm As String

ReDim Arr(1 To 3, 1 To 2)
Arr(1, 1) = "term1"
Arr(1, 2) = "path1"
Arr(2, 1) = "term2"
Arr(2, 2) = "path2"
Arr(3, 1) = "term3"
Arr(3, 2) = "path3"

LookupTerm = "term1"
V = Application.VLookup(LookupTerm, Arr, 2)
If IsError(V) = True Then
Debug.Print "Term not found"
Else
Debug.Print "Path: " & V
End If
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)

"Conan Kelly" wrote in message

...



Hello all,


How would I use a 2 dimensional string array like a vlookup/hlookup?


I'm thinking that my array will be something like this:


dim pstrDestinationPath(0 to [n], 0 to 1) as string


so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.


what I'm thinking of right now is just using a For loop to loop through
each element of the "first column" of the array, comparing it to a string.
Then using the For Loop control counter (after the match is found) as the
index for the "2nd column" of the array to return the matching path
element.


Is this the correct way of doing this, or is there some other more
straight forward way?


Thanks for any help anyone can provide,


Conan Kelly- Hide quoted text -


- Show quoted text -


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Using a 2 dimensional string array variable like a lookup function

VLOOKUP can use either an array or a Range. No casting is done. For
example, you can use an array in a worksheet formula that doesn't reference
any Range at all.

=VLOOKUP(3,{1,"a";2,"b";3,"c"},2,FALSE)

The arguments for any worksheet function callable from VBA (optionally using
WorksheetFunction) has the exact same arguments, and those arguments have
the same meaning, as if the function were called directly from a worksheet
cell.

See http://www.cpearson.com/excel/Callin...ionsInVba.aspx ,
especially the section on error handling.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"ilia" wrote in message
...
Does this cause a cast from string array to a range? Or is
Application.Vlookup different from
Application.WorksheetFunction.Vlookup as far as argument types?


On Nov 29, 3:44 pm, "Chip Pearson" wrote:
You can use VLOOKUP against an array in code. This would be much more
efficient than looping. For example,

Sub AAA()
Dim V As Variant
Dim Arr() As String
Dim LookupTerm As String

ReDim Arr(1 To 3, 1 To 2)
Arr(1, 1) = "term1"
Arr(1, 2) = "path1"
Arr(2, 1) = "term2"
Arr(2, 2) = "path2"
Arr(3, 1) = "term3"
Arr(3, 2) = "path3"

LookupTerm = "term1"
V = Application.VLookup(LookupTerm, Arr, 2)
If IsError(V) = True Then
Debug.Print "Term not found"
Else
Debug.Print "Path: " & V
End If
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)

"Conan Kelly" wrote in message

...



Hello all,


How would I use a 2 dimensional string array like a vlookup/hlookup?


I'm thinking that my array will be something like this:


dim pstrDestinationPath(0 to [n], 0 to 1) as string


so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.


what I'm thinking of right now is just using a For loop to loop through
each element of the "first column" of the array, comparing it to a
string.
Then using the For Loop control counter (after the match is found) as
the
index for the "2nd column" of the array to return the matching path
element.


Is this the correct way of doing this, or is there some other more
straight forward way?


Thanks for any help anyone can provide,


Conan Kelly- Hide quoted text -


- Show quoted text -





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 256
Default Using a 2 dimensional string array variable like a lookupfunction

Thanks Chip.

On Nov 29, 6:24 pm, "Chip Pearson" wrote:
VLOOKUP can use either an array or a Range. No casting is done. For
example, you can use an array in a worksheet formula that doesn't reference
any Range at all.

=VLOOKUP(3,{1,"a";2,"b";3,"c"},2,FALSE)

The arguments for any worksheet function callable from VBA (optionally using
WorksheetFunction) has the exact same arguments, and those arguments have
the same meaning, as if the function were called directly from a worksheet
cell.

Seehttp://www.cpearson.com/excel/CallingWorksheetFunctionsInVba.aspx,
especially the section on error handling.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)

"ilia" wrote in message

...



Does this cause a cast from string array to a range? Or is
Application.Vlookup different from
Application.WorksheetFunction.Vlookup as far as argument types?


On Nov 29, 3:44 pm, "Chip Pearson" wrote:
You can use VLOOKUP against an array in code. This would be much more
efficient than looping. For example,


Sub AAA()
Dim V As Variant
Dim Arr() As String
Dim LookupTerm As String


ReDim Arr(1 To 3, 1 To 2)
Arr(1, 1) = "term1"
Arr(1, 2) = "path1"
Arr(2, 1) = "term2"
Arr(2, 2) = "path2"
Arr(3, 1) = "term3"
Arr(3, 2) = "path3"


LookupTerm = "term1"
V = Application.VLookup(LookupTerm, Arr, 2)
If IsError(V) = True Then
Debug.Print "Term not found"
Else
Debug.Print "Path: " & V
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consultingwww.cpearson.com
(email on the web site)


"Conan Kelly" wrote in message


...


Hello all,


How would I use a 2 dimensional string array like a vlookup/hlookup?


I'm thinking that my array will be something like this:


dim pstrDestinationPath(0 to [n], 0 to 1) as string


so in this 2 x [n] array, I will put a search term (a file name will be
searched for this term) in the "first column" and the corresponding
destination path in the "second column" of the array.


what I'm thinking of right now is just using a For loop to loop through
each element of the "first column" of the array, comparing it to a
string.
Then using the For Loop control counter (after the match is found) as
the
index for the "2nd column" of the array to return the matching path
element.


Is this the correct way of doing this, or is there some other more
straight forward way?


Thanks for any help anyone can provide,


Conan Kelly- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lookup within a 2 dimensional array Edu Excel Worksheet Functions 5 March 31st 10 09:52 PM
Export 1-dimensional array values to a two-dimensional table? Laurie Excel Programming 2 November 8th 07 03:51 PM
Variable Table Array in Lookup Function matt_the_brum Excel Worksheet Functions 6 August 4th 06 05:07 PM
2 dimensional array and freq function? dabith[_10_] Excel Programming 2 October 6th 05 09:56 AM
Filter Function for 2-dimensional array Cool Sport Excel Programming 3 December 20th 04 09:37 AM


All times are GMT +1. The time now is 08:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"