PC Review


Reply
Thread Tools Rate Thread

Dynamic Array Lbound not working when only one value in array

 
 
dean.brunne@lion-nathan.com.au
Guest
Posts: n/a
 
      23rd May 2007
Hi-The code below is to use a dyanmic array to loop through the
values. It works with more than one value in the array but does not
work with only one value in the array. I end up with a type mismatch.
How do I fix?

Array Value =23000-7000

CODE:

Sub SetupCostCentres()

Dim MyArray As Variant
Dim rng As Range
Dim RangeValues As Variant
Dim i As Long
Dim j As Long
Dim Sh As String

Application.EnableEvents = False

Set rng = Worksheets("CC Info").Range("A2:A100")

ArrayCount = Excel.WorksheetFunction.CountA(rng)

If ArrayCount = 0 Then
MsgBox "No Cost Centres Entered"
Exit Sub

Else

ReDim MyArray(1 To ArrayCount, 1)

RangeValues = Worksheets("Cc Info").Range("A2:A" & ArrayCount + 1)

For i = LBound(RangeValues, 1) To UBound(RangeValues, 1)
'MISMATCHES HERE RANGEVALUES TO "23000-7000" EXPECTING NUMBER
For j = LBound(RangeValues, 2) To UBound(RangeValues, 2)
MyArray(i, j) = RangeValues(i, j)

 
Reply With Quote
 
 
 
 
NickHK
Guest
Posts: n/a
 
      23rd May 2007
Dean,
Firstly, I do not understand your logic of using COUNTA. That counts the
non-blank cells. How do know the non-blanks are at the top of given range ?
May you should Sort the column first, or use .End(xlDown) instead.

Also, you can assign the range values to an array directly:
Dim MyArray As Variant

With Worksheets("CC Info")
MyArray = .Range(.Range("A2"),.Range("A2").End(xlDown))
End With

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi-The code below is to use a dyanmic array to loop through the
> values. It works with more than one value in the array but does not
> work with only one value in the array. I end up with a type mismatch.
> How do I fix?
>
> Array Value =23000-7000
>
> CODE:
>
> Sub SetupCostCentres()
>
> Dim MyArray As Variant
> Dim rng As Range
> Dim RangeValues As Variant
> Dim i As Long
> Dim j As Long
> Dim Sh As String
>
> Application.EnableEvents = False
>
> Set rng = Worksheets("CC Info").Range("A2:A100")
>
> ArrayCount = Excel.WorksheetFunction.CountA(rng)
>
> If ArrayCount = 0 Then
> MsgBox "No Cost Centres Entered"
> Exit Sub
>
> Else
>
> ReDim MyArray(1 To ArrayCount, 1)
>
> RangeValues = Worksheets("Cc Info").Range("A2:A" & ArrayCount + 1)
>
> For i = LBound(RangeValues, 1) To UBound(RangeValues, 1)
> 'MISMATCHES HERE RANGEVALUES TO "23000-7000" EXPECTING NUMBER
> For j = LBound(RangeValues, 2) To UBound(RangeValues, 2)
> MyArray(i, j) = RangeValues(i, j)
>



 
Reply With Quote
 
dean.brunne@lion-nathan.com.au
Guest
Posts: n/a
 
      24th May 2007
On May 23, 2:43 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> Dean,
> Firstly, I do not understand your logic of using COUNTA. That counts the
> non-blank cells. How do know the non-blanks are at the top of given range ?
> May you should Sort the column first, or use .End(xlDown) instead.
>
> Also, you can assign the range values to an array directly:
> Dim MyArray As Variant
>
> With Worksheets("CC Info")
> MyArray = .Range(.Range("A2"),.Range("A2").End(xlDown))
> End With
>
> NickHK
>
> <dean.bru...@lion-nathan.com.au> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Hi-The code below is to use a dyanmic array to loop through the
> > values. It works with more than one value in the array but does not
> > work with only one value in the array. I end up with a type mismatch.
> > How do I fix?

>
> > Array Value =23000-7000

>
> > CODE:

>
> > Sub SetupCostCentres()

>
> > Dim MyArray As Variant
> > Dim rng As Range
> > Dim RangeValues As Variant
> > Dim i As Long
> > Dim j As Long
> > Dim Sh As String

>
> > Application.EnableEvents = False

>
> > Set rng = Worksheets("CC Info").Range("A2:A100")

>
> > ArrayCount = Excel.WorksheetFunction.CountA(rng)

>
> > If ArrayCount = 0 Then
> > MsgBox "No Cost Centres Entered"
> > Exit Sub

>
> > Else

>
> > ReDim MyArray(1 To ArrayCount, 1)

>
> > RangeValues = Worksheets("Cc Info").Range("A2:A" & ArrayCount + 1)

>
> > For i = LBound(RangeValues, 1) To UBound(RangeValues, 1)
> > 'MISMATCHES HERE RANGEVALUES TO "23000-7000" EXPECTING NUMBER
> > For j = LBound(RangeValues, 2) To UBound(RangeValues, 2)
> > MyArray(i, j) = RangeValues(i, j)- Hide quoted text -

>
> - Show quoted text -


Nick,

By using the code you have shown do I not have to ReDim for the
variant size of the array or is that implicit in the statement? How
do I structure the LBound and Ubound?

Thanks,

Dean

 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      25th May 2007
You can just use the same LBound/UBound construct on each dimension as you
did before.

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On May 23, 2:43 pm, "NickHK" <TungChe...@Invalid.com> wrote:
> > Dean,
> > Firstly, I do not understand your logic of using COUNTA. That counts the
> > non-blank cells. How do know the non-blanks are at the top of given

range ?
> > May you should Sort the column first, or use .End(xlDown) instead.
> >
> > Also, you can assign the range values to an array directly:
> > Dim MyArray As Variant
> >
> > With Worksheets("CC Info")
> > MyArray = .Range(.Range("A2"),.Range("A2").End(xlDown))
> > End With
> >
> > NickHK
> >
> > <dean.bru...@lion-nathan.com.au> wrote in message
> >
> > news:(E-Mail Removed)...
> >
> >
> >
> > > Hi-The code below is to use a dyanmic array to loop through the
> > > values. It works with more than one value in the array but does not
> > > work with only one value in the array. I end up with a type mismatch.
> > > How do I fix?

> >
> > > Array Value =23000-7000

> >
> > > CODE:

> >
> > > Sub SetupCostCentres()

> >
> > > Dim MyArray As Variant
> > > Dim rng As Range
> > > Dim RangeValues As Variant
> > > Dim i As Long
> > > Dim j As Long
> > > Dim Sh As String

> >
> > > Application.EnableEvents = False

> >
> > > Set rng = Worksheets("CC Info").Range("A2:A100")

> >
> > > ArrayCount = Excel.WorksheetFunction.CountA(rng)

> >
> > > If ArrayCount = 0 Then
> > > MsgBox "No Cost Centres Entered"
> > > Exit Sub

> >
> > > Else

> >
> > > ReDim MyArray(1 To ArrayCount, 1)

> >
> > > RangeValues = Worksheets("Cc Info").Range("A2:A" & ArrayCount + 1)

> >
> > > For i = LBound(RangeValues, 1) To UBound(RangeValues, 1)
> > > 'MISMATCHES HERE RANGEVALUES TO "23000-7000" EXPECTING NUMBER
> > > For j = LBound(RangeValues, 2) To UBound(RangeValues, 2)
> > > MyArray(i, j) = RangeValues(i, j)- Hide quoted text -

> >
> > - Show quoted text -

>
> Nick,
>
> By using the code you have shown do I not have to ReDim for the
> variant size of the array or is that implicit in the statement? How
> do I structure the LBound and Ubound?
>
> Thanks,
>
> Dean
>



 
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
Run-time error '9' when calling LBound on a dynamic array paul.schrum@gmail.com Microsoft Access Form Coding 2 31st Jul 2011 01:09 AM
Speed of fixed array versus dynamic array Sing Microsoft Excel Programming 8 18th Nov 2007 10:19 AM
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 Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
Change LBound of array R Avery Microsoft Excel Programming 4 19th May 2005 04:53 PM
dynamic array of pointers vs dynamic array of objects lemonade Microsoft VC .NET 1 10th Dec 2003 08:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:12 PM.