LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default ReDim not working as expected. Array expert needed.

Greetings! This one has me totally baffled.

In Sub ArrayStudies1, MyArray is dimensioned as an array of type Integer.
MyArray is then ReDim'ed, Ranges A1 through A3 are set to integers,
successive array locations are set to these ranges, and the first array
location is successfully displayed.

Sub ArrayStudies2 differs from Sub ArrayStudies1 only in that the array
MyArray is set to a 3 cell range having integers. In this case, upon
execution, I get the message " "Run-time error '13': Type mismatch".

Sub ArrayStudies3 differs from Sub ArrayStudies2 only in that MyArray is
declared as a dynamic array of type Variant rather than as type Integer. And
this worked perfectly with no error messages. Why can't MyArray be declared
as an array of type Integer in Sub ArrayStudies2? (At the top of the module
containing these three subs is: Option Base 1)

Sub ArrayStudies1()

' example patterned after Excel VBA Help on ReDim statement:
' This example uses the ReDim statement to allocate and reallocate storage
space for dynamic-array variables. It assumes the Option Base is 1.

Dim MyArray() As Integer ' Declare dynamic array of type Integer.
ReDim MyArray(3) ' Allocate 3 elements.

Range("A1") = 1
Range("A2") = 2
Range("A3") = 3

MyArray(1) = Range("A1")
MyArray(2) = Range("A2")
MyArray(3) = Range("A3")

' The following instruction works fine:
MsgBox "MyArray(1) = " & MyArray(1)

End Sub

-------------------------------------------------------------------------------------------------

Sub ArrayStudies2()

Dim MyArray() As Integer ' Declare dynamic array of type Integer.

ReDim MyArray(3) ' Allocate 3 elements.

Range("A1") = 1
Range("A2") = 2
Range("A3") = 3

' The follwg instr gives: "Run-time error '13': Type mismatch"
MyArray = Range("A1:A3") ' Initialize array.

MsgBox "MyArray(1, 1) = " & MyArray(1, 1)

End Sub

-------------------------------------------------------------------------------------------------

Sub ArrayStudies3()

Dim MyArray() ' Declare dynamic array of type Variant.
ReDim MyArray(3) ' Allocate 3 elements.

Range("A1") = 1
Range("A2") = 2
Range("A3") = 3

' The follwg instr works fine
MyArray = Range("A1:A3") ' Initialize array.

MsgBox "MyArray(1, 1) = " & MyArray(1, 1)

End Sub

May you have a most blessed day!

Sincerely,

Michael Fitzpatrick
 
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
Expert-ish Help needed Kendra Excel Worksheet Functions 1 November 5th 08 02:16 AM
ReDim Preserve on 2D Array not working bony_tony Excel Programming 5 December 9th 07 03:21 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R[_2_] Excel Programming 3 November 13th 07 04:08 PM
ReDim Object array as parameter of Variant array Peter T Excel Programming 4 May 10th 05 02:11 PM
Expert help needed Michael168[_16_] Excel Programming 1 October 2nd 03 08:20 PM


All times are GMT +1. The time now is 08:27 AM.

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"