View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Matt Jensen Matt Jensen is offline
external usenet poster
 
Posts: 113
Default Dynamic array problem

Thanks Tushar
I realise the intent may seem questionable but the explanation is long
winded and complicated.
I'll give it a try so you can briefly understand.

A user of the workbook can hit a custom button to email it to another
predefined user.
There is a separate macro (named "Add") that when run, will copy 5 column
values from a row in the "source" worksheet to a blank worksheet named
"Updates" (this worksheet is made "blank" each time the app opens in the
workbook open event). So essentially this updates sheet has "updates" on it,
1 row for each "Add" in the current "session" that the user has had the
workbook open.
*If* this "add" macro has been run, then when the user hits the button to
send an email, in the email body I create an HTML table with 5 headings and
loop thru each and any "added" row on the "updates" sheet.
The fifth column holds 'comments' field values, and sometimes there may be
no comment. So if there is no entries in the comments, no matter how many
"Adds", then referring to the array's 5th column obviously gives an error.

The problem with your latest suggestion which I had already tried is that if
the "updates" worksheet is blank, it throws an error too, in your example
when you define x.
I did read your post, I just didn't see any "need" for XL/VBA to resize my
array that I explicity dimensioned nor did I understand why - I guess I'll
just have to take this fact for granted though.

Thought there'd be a simple answer. Guess I'll have to IF and On Error this
problem away!

Just stressed at the mo - sorry!
Cheers
Matt


"Tushar Mehta" wrote in message
...
Hi Matt,

I guess you didn't get a chance to read my entire post. As I mentioned in
it, XL/VBA resizes the array as needed. So, if you have four columns of
data, the resulting array will have a 2nd dimension of 4.

As far as referring to column 5 goes, I am not sure of the intent. There

is
no column 5 in the worksheet; hence there isn't one in the array.

Consider
using ubound(array,2) rather than a hardcoded value such as 5. If you

need
to create an extra column, consder changing the array size *after* VBA

gives
you the results. Something like:

Dim x()
x = Worksheets(1).UsedRange.Value
ReDim Preserve x(LBound(x, 1) To UBound(x, 1), LBound(x, 2) To

UBound(x,
2) + 1)
.


"Matt Jensen" wrote:

Thanks Tushar

{snip}
Put any value in cells a1,b1,c1,d1 on Sheet1 and the immediate window

will
show UBound(myarray, 2) as 4 even though you defined the array as 5

columns
wide...?? What's with this? How can I avoid an error when referring the

5
column of the array then? Preferably not with an On Error statement
Thanks
Matt

{snip}