Array not loading

Charles Bushby

New Member
Joined
Aug 10, 2005
Messages
42
This piece of code is not working properly as the first value loads into the array but no other values load.

I can see that "i" increments to 2 but as soon as it hits the line with "For i = 1 To UBound(aCoupeColAd)" it reverts back to 1 again.

Here is the code if anyone can help work this out for me:

Do
If Not IsEmpty(wShCS.Cells(iRow, iCol + 10)) Then
iCoupeCounter = iCoupeCounter + 1

ReDim Preserve aCoupeColAd(1 To iCoupeCounter)
For i = 1 To UBound(aCoupeColAd) 'FOR SOME REASON ON THE SECOND ITERATION i REVERTS TO 1 AGAIN
aCoupeColAd(i) = iCol + 10
Next i

End If
iCol = iCol + 1
Loop Until iCol = 19

Many thanks in anticipation for any help received.

Charles
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What is the value of UBound(aCoupeColAd) at that point?
 
Upvote 0
Charles

What are you trying to populate the array with?

Perhaps you should be using something other than just i when populating it.

PS Are you sure you need 2 loops?
Code:
Do

    If Not IsEmpty(wShCS.Cells(iRow, iCol + 10)) Then
        iCoupeCounter = iCoupeCounter + 1

        ReDim Preserve aCoupeColAd(1 To iCoupeCounter)

        aCoupeColAd(iCoupeCounter) = iCol + 10

    End If

    iCol = iCol + 1
Loop Until iCol = 19
 
Upvote 0
JoeMo, Norrie and Redbeard,

The UBound(aCoupeColAd) holds the first item to be loaded into the array then as soon as i reverts to 1 the array empties itself until it loops round again and the second value the loads into the array in first position. Thus the value fills each time it loops but then empties itself until at the end I am left with the last value only in the array.

The array is loading with whole numbers which are column numbers.

I do need two loops as I am picking up values from a row of cells (which are in 15 contiguous columns) in the spreadsheet of which some are empty. The aim is to enter into the array the column numbers of cells that are not empty.

Charles
 
Upvote 0
Charles

What are the 2 loops looping through exactly?

What would the resultant array contain?

Is it the column numbers of the non-empty cells in a specific row?
 
Upvote 0
Norrie,

On reflection maybe I don't need the two loops as I might simply need one loop to go through 15 columns of one row and checks if each cell is empty or not. If not empty then:

1) it adds to the iCoupeCounter in order to increase the array size
2) adds the column number of the filled cell to the array

Not quite sure how to do that!

I will then subsequently need to refer to the iCoupeCounter and the column number in the next part of the code.

Charles
 
Upvote 0
Charles

The code I posted kind of does that, I think.

It loops through the row irow and if it finds a cell that's empty redimensions aCoupeColAd and puts icol+10 in the new item of the array.

Mind you, I've got no idea what irow and icol are, or why you are adding 10 to icol.:)
 
Upvote 0
Norrie,

That works perfectly so thank you very much for your help.

irow and icol are variables to hold row and column numbers

Charles
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,085
Members
452,378
Latest member
Hoodzy01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top