+ Reply to Thread
Results 1 to 4 of 4

Why does this produce an error?

  1. #1
    Registered User
    Join Date
    05-11-2006
    Location
    Manchester
    Posts
    5

    Why does this produce an error?

    Public Sub initialize()
    Dim NumberOfColumns As Integer
    NumberOfColumns = 13
    Dim p(NumberOfColumns) As Integer


    end Sub


    Seems that Excel doesn't like using Variables to intialize arrays. How should this be done?

  2. #2

    Re: Why does this produce an error?

    Hi
    You need

    Public Sub initialize()
    Dim NumberOfColumns As Integer
    Dim p( ) as integer
    NumberOfColumns = 13
    ReDim p(1 to NumberOfColumns)


    end Sub

    So you decare the array first with p( ) if you don't yet know the index
    range. Then you must ReDim it once the index range is known.

    regards
    Paul


  3. #3
    Andy Pope
    Guest

    Re: Why does this produce an error?

    Hi,

    And as the warning message says you can use a Const.

    Public Sub initialize()
    Const NumberOfColumns = 13
    Dim p(NumberOfColumns) As Integer
    End Sub

    Or use the ReDim

    Public Sub initialize()
    Dim NumberOfColumns As Integer
    NumberOfColumns = 13
    ReDim p(NumberOfColumns) As Integer
    End Sub

    Cheers
    Andy

    zaphod2003 wrote:
    > Public Sub initialize()
    > Dim NumberOfColumns As Integer
    > NumberOfColumns = 13
    > Dim p(NumberOfColumns) As Integer
    >
    >
    > end Sub
    >
    >
    > Seems that Excel doesn't like using Variables to intialize arrays. How
    > should this be done?
    >
    >


    --

    Andy Pope, Microsoft MVP - Excel
    http://www.andypope.info

  4. #4
    Registered User
    Join Date
    05-11-2006
    Location
    Manchester
    Posts
    5

    Thanks Guys

    Thanks for your help much appreciated

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1