PC Review


Reply
Thread Tools Rate Thread

Can VBA use array constants

 
 
Walter Briscoe
Guest
Posts: n/a
 
      30th Mar 2009
This question follows from
<http://groups.google.com/g/8077ff07/...18cd6d29d06f79
c>.
In VBA, I want to lookup a column number which refers to a London
Underground line name.

My first version using VLOOKUP was:
Dim colIndex ' Translation of Line to a column number

ActiveCell.Formula = "=VLOOKUP(""" & Line & """," _
& "{""Bakerloo"",5;""Central"",6, ...},2,TRUE)"
colIndex = ActiveCell.Value

My second version using MATCH used this line:
ActiveCell.Formula = "=4+MATCH(""" & Line & """," _
& "{""Bakerloo"",""Central"", ...}, 1)"

(With both those versions both "Central" and "Central line" match.)

My third version using the VBA find function used the following code:
Dim lines As Range ' at module level
Set lines = Range("E1:O1") ' in a public subroutine
colIndex = lines.Find(Line).Column

(With this version, "Central line" (unsurprisingly) does not match.)

Is it possible to use an array constant for the range in such code?
If so, how?

I got a 1004: "Method 'Range' of object '_Global' failed" with
Set lines = Range(Array("Bakerloo", "Central", "Circle", ...))

As it happens, I am happy with the third method.
I am not happy with my ignorance.
--
Walter Briscoe
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      30th Mar 2009
Not sure what you are really trying to do. However if you want to hardcode
an array of constants you could do something like this

Sub test()
Dim sLine As String
Dim aLines As Variant
Const LINES As String = "Bakerloo,Central"
aLines = Split(LINES, ",") ' note lbound is zero

sLine = "Central"
For i = 0 To UBound(aLines)
If UCase$(aLines(i)) = UCase$(sLine) Then
colIndex = i + 1 ' adjust for Lbound zero
Exit For
End If
Next
If colIndex Then
colIndex = colIndex + 4 ' add the 4 (why?)
End If
MsgBox colIndex

End Sub

A simple loop will be faster than the overhead of calling a worksheet
function.

Probably better though to put all your station names in a list in cells,
perhaps in a hidden sheet, and in a named range, eg "myLines" on sheet1

aLines = ThisWorkbook.Worksheets("sheet1").Range("myLines").Value

aLines will now be a two dimensional array, so need to change the code above
slightly from
If UCase$(aLines(i)) = UCase$(sLine) Then
to
If UCase$(aLines(i,1)) = UCase$(sLine) Then

Regards,
Peter T


"Walter Briscoe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This question follows from
> <http://groups.google.com/g/8077ff07/...18cd6d29d06f79
> c>.
> In VBA, I want to lookup a column number which refers to a London
> Underground line name.
>
> My first version using VLOOKUP was:
> Dim colIndex ' Translation of Line to a column number
>
> ActiveCell.Formula = "=VLOOKUP(""" & Line & """," _
> & "{""Bakerloo"",5;""Central"",6, ...},2,TRUE)"
> colIndex = ActiveCell.Value
>
> My second version using MATCH used this line:
> ActiveCell.Formula = "=4+MATCH(""" & Line & """," _
> & "{""Bakerloo"",""Central"", ...}, 1)"
>
> (With both those versions both "Central" and "Central line" match.)
>
> My third version using the VBA find function used the following code:
> Dim lines As Range ' at module level
> Set lines = Range("E1:O1") ' in a public subroutine
> colIndex = lines.Find(Line).Column
>
> (With this version, "Central line" (unsurprisingly) does not match.)
>
> Is it possible to use an array constant for the range in such code?
> If so, how?
>
> I got a 1004: "Method 'Range' of object '_Global' failed" with
> Set lines = Range(Array("Bakerloo", "Central", "Circle", ...))
>
> As it happens, I am happy with the third method.
> I am not happy with my ignorance.
> --
> Walter Briscoe



 
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
Array Constants kittronald Microsoft Excel Worksheet Functions 0 15th Jun 2011 12:04 AM
Array constants different in greek locale vgee Microsoft Excel Crashes 2 10th Oct 2009 08:16 PM
Please help build array with constants suzbee Microsoft Excel Discussion 5 13th Aug 2009 04:57 AM
unions, intersections or array constants Loadmaster Microsoft Excel Worksheet Functions 23 6th May 2009 08:11 PM
Array of String Constants? Joe Cool Microsoft C# .NET 3 4th May 2008 09:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:28 PM.