LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
QB QB is offline
external usenet poster
 
Posts: 57
Default building array

Thank you all for your insight! I have learnt several new things and have
resolved the problem as well!!!

QB





"Dave Peterson" wrote:

Unless you're doing something special (like "Option base 1" at the top of your
module), this line:

ReDim Preserve aNewDbData(1, iNoWks) As Variant
is the equivalent of:
ReDim Preserve aNewDbData(0 to 1, 0 to iNoWks) As Variant

And counting from 0 to 73 gives 74 entries.

I'd use:
ReDim Preserve aNewDbData(0 to 1, 0 to iNoWks - 1) As Variant
(I like being explicit in my bounds.)

And instead of using Cletter(), you can use cells() (and I qualified my ranges
to the activesheet, too):

Option Explicit
Sub testme01()
Dim iStartWkCol As Long
Dim iEndWkCol As Long
Dim aNewDBData() As Variant
Dim iNoWks As Long
Dim iCounter As Long
Dim iArrPosn As Long
Dim wks As Worksheet

Set wks = ActiveSheet

iStartWkCol = 2
iEndWkCol = 74
iNoWks = iEndWkCol - iStartWkCol + 1

'Resize our array to accomodate the data
ReDim Preserve aNewDBData(0 To 1, 0 To iNoWks - 1) As Variant

With wks
'Loop through the Week Cols of the Worksheet
For iCounter = iStartWkCol To iEndWkCol
'Date
iArrPosn = iCounter - iStartWkCol
aNewDBData(0, iArrPosn) = .Cells(2, iCounter).Value

'No Hrs Atl Dispo
aNewDBData(1, iArrPosn) = .Cells(10, iCounter).Value
Next iCounter
End With
End Sub

But there is no reason why you couldn't use different limits:

ReDim Preserve aNewDBData(0 To 1, iStartWkCol To iEndWkCol) As Variant

As in:

Option Explicit
Sub testme01()
Dim iStartWkCol As Long
Dim iEndWkCol As Long
Dim aNewDBData() As Variant
Dim iNoWks As Long
Dim iCounter As Long
Dim iArrPosn As Long
Dim wks As Worksheet

Set wks = ActiveSheet

iStartWkCol = 2
iEndWkCol = 74
iNoWks = iEndWkCol - iStartWkCol + 1

'Resize our array to accomodate the data
ReDim Preserve aNewDBData(0 To 1, iStartWkCol To iEndWkCol) As Variant

With wks
'Loop through the Week Cols of the Worksheet
For iCounter = iStartWkCol To iEndWkCol
'Date
aNewDBData(0, iCounter) = .Cells(2, iCounter).Value

'No Hrs Atl Dispo
aNewDBData(1, iCounter) = .Cells(10, iCounter).Value
Next iCounter
End With
End Sub

I find that this kind of thing makes life easier when debugging. The counter
represents the column number and doesn't need to be adjusted.

To loop through the array, though, I'd use something like:

For iCounter = LBound(aNewDBData, 2) To UBound(aNewDBData, 2)
MsgBox aNewDBData(0, iCounter) & vbLf & aNewDBData(1, iCounter)
Next iCounter

So I'm looping through the elements in the 2nd dimension of that array.

========
In fact, after I've redimmed that array, I could use it in the initial loop:

Option Explicit
Sub testme01()
Dim iStartWkCol As Long
Dim iEndWkCol As Long
Dim aNewDBData() As Variant
Dim iNoWks As Long
Dim iCounter As Long
Dim iArrPosn As Long
Dim wks As Worksheet

Set wks = ActiveSheet

iStartWkCol = 2
iEndWkCol = 74
iNoWks = iEndWkCol - iStartWkCol + 1

'Resize our array to accomodate the data
ReDim Preserve aNewDBData(0 To 1, iStartWkCol To iEndWkCol) As Variant

With wks
'Loop through the Week Cols of the Worksheet
For iCounter = LBound(aNewDBData, 2) To UBound(aNewDBData, 2)
'Date
aNewDBData(0, iCounter) = .Cells(2, iCounter).Value

'No Hrs Atl Dispo
aNewDBData(1, iCounter) = .Cells(10, iCounter).Value
Next iCounter
End With

For iCounter = LBound(aNewDBData, 2) To UBound(aNewDBData, 2)
MsgBox aNewDBData(0, iCounter) & vbLf & aNewDBData(1, iCounter)
Next iCounter

End Sub


QB wrote:

I am trying to build a dynamic array. Sadly, I can't seem to see my error.
I have


iStartWkCol = 2
iEndWkCol = 74
iNoWks = iEndWkCol - iStartWkCol + 1
ReDim Preserve aNewDbData(1, iNoWks) As Variant 'Resize our array to
accomodate the data

For iCounter = iStartWkCol To iEndWkCol 'Loop through the
Week Cols of the Worksheet
iarrPosn = iCounter - iStartWkCol
aNewDbData(0, iarrPosn) = _
Range(CLetter(iCounter) & "2").Value 'Date
aNewDbData(1, iarrPosn) = _
Range(CLetter(iCounter) & "10").Value 'No Hrs Atl Dispo
Next iCounter

My problem is that iNoWks, thus my array size is correctly giving 73, but
when it performs the loop it always run to iCounter = 42 and stops? Does
anyone see what I am doing wrong here?

Thank you for the extra set of eyes!

QB


--

Dave Peterson
.



 
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
Building the PageFields:=Array( John Excel Programming 0 September 30th 08 01:12 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
Building an array of worksheets for printing DRK Excel Programming 5 August 30th 05 01:56 AM
Building an Array Otto Moehrbach[_6_] Excel Programming 5 September 24th 04 01:30 PM
Building/Creating an Array Rich Cooper Excel Programming 8 May 26th 04 06:57 PM


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