Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA & Arrays

I can't get the following procedure to work... according to the help files,
it should work but doesn't. I get a "Compile error: Can't assign to array"
when vColumns = Array("A", "B", ...). Does anyone know why and how to fix?
Thanks! :-)

Public Sub Suffy()

Dim vColumns(6) As Variant
Dim i As Integer
vColumns = Array("A", "B", "C", "D", "E", "F", "G")

For i = 1 To 7 Step 1
Debug.Print sColumns(i)
Next i

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Excel VBA & Arrays

Robert,

The Array funciton returns a single Variant data type variable that contains
an array. Thus, you need to change

Dim vColumns(6) As Variant
to
Dim vColumns As Variant

Also, the lower bound of the array within the Variant is determined by the
"Option Base" statement (if any) at the top of the module. ("Option Base 0"
or "Option Base 1"). Without an Option Base statement, the lower bound of
vColumns will be 0, not 1, as your code expects. When looping through an
array, it is good standard programming practice to use LBound and UBound to
control the values of the index variable rather then hard-coding the values.
E.g.,

For i = LBound(vColumns) To UBound(vColumns)
Debug.Print vColumns(i)
Next i

Finally, you have a typo
Debug.Print sColumns(i)
should be
Debug.Print vColumns(i)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)





"Robert" wrote in message
...
I can't get the following procedure to work... according to the help files,
it should work but doesn't. I get a "Compile error: Can't assign to array"
when vColumns = Array("A", "B", ...). Does anyone know why and how to fix?
Thanks! :-)

Public Sub Suffy()

Dim vColumns(6) As Variant
Dim i As Integer
vColumns = Array("A", "B", "C", "D", "E", "F", "G")

For i = 1 To 7 Step 1
Debug.Print sColumns(i)
Next i

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Excel VBA & Arrays

First, sometimes you use vColumns and sometimes sColumns. That'll always be a
problem.

Second, this worked for me:

Option Explicit
Public Sub Suffy()

Dim vColumns As Variant
Dim i As Long
vColumns = Array("A", "B", "C", "D", "E", "F", "G")

For i = lbound(vColumns) To ubound(vcolumns) Step 1
Debug.Print vColumns(i)
Next i

End Sub


Robert wrote:

I can't get the following procedure to work... according to the help files,
it should work but doesn't. I get a "Compile error: Can't assign to array"
when vColumns = Array("A", "B", ...). Does anyone know why and how to fix?
Thanks! :-)

Public Sub Suffy()

Dim vColumns(6) As Variant
Dim i As Integer
vColumns = Array("A", "B", "C", "D", "E", "F", "G")

For i = 1 To 7 Step 1
Debug.Print sColumns(i)
Next i

End Sub


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Excel VBA & Arrays

Just to add...
Using a dynamic array instead of a fixed size array allows one
to assign to the array (provided you are using a later version of Excel)...

Public Sub Suffy()
Dim vColumns() As Variant
Dim i As Integer
ReDim vColumns(0 To 6)

vColumns = Array("A", "B", "C", "D", "E", "F", "G")
For i = 0 To 6
Debug.Print vColumns(i)
Next i
End Sub
-----------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

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
Working with ranges in arrays... or an introduction to arrays Glen Excel Programming 5 September 10th 06 08:32 AM
Arrays - declaration, adding values to arrays and calculation Maxi[_2_] Excel Programming 1 August 17th 06 04:13 PM
excel arrays kenrock[_2_] Excel Programming 6 November 12th 05 11:36 AM
arrays in excel Dan Excel Worksheet Functions 9 May 24th 05 07:01 PM
Arrays in Excel VBA Eliezer Excel Programming 4 September 3rd 04 08:37 PM


All times are GMT +1. The time now is 09:40 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"