Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default clearing array contents

Hello.

I am working with an array to aid in summing the cost of a bill of materials
structure.

The array has an element for each level of the bill of materials, say level
1 to level 8. (sometimes higher, program defins it based on min and max found
in the level structure)

There are times in the summing process when I need to clear out part of the
array contents, say from level 5 to level 8. I can do that by using a
counter and a for loop, and clearing each one.

My question is, is there a one program line method to clear a group of the
array elements, something that would assign levArray(lev5, lev6, lev7, lev8)
all to 0 in one step?

Thanks.
Mark
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default clearing array contents

Highly doubtful.

--
Regards,
Tom Ogilvy


"mark" wrote in message
...
Hello.

I am working with an array to aid in summing the cost of a bill of

materials
structure.

The array has an element for each level of the bill of materials, say

level
1 to level 8. (sometimes higher, program defins it based on min and max

found
in the level structure)

There are times in the summing process when I need to clear out part of

the
array contents, say from level 5 to level 8. I can do that by using a
counter and a for loop, and clearing each one.

My question is, is there a one program line method to clear a group of the
array elements, something that would assign levArray(lev5, lev6, lev7,

lev8)
all to 0 in one step?

Thanks.
Mark



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default clearing array contents

Highly doubtful.


okay, thanks.

I could do it by tracking the levels in a group of spreadsheet cells,
instead of an array in VB... that way, I could clear whatever portion of the
group of cells I wanted.

I was just looking for a more efficient programming method (not looping
through if it wasn't needed)...

But what I have runs quickly anyway, so it will be fine to the user.

Thanks Tom.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default clearing array contents

What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine. Then, whereever you need
to do the clearing, just put a one line call to that routine. If/when
you find a better way to do what you want to do all you will have to do
is update that one routine.

Depending on what you are doing, you could set up the array as a
dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)

Is it faster/cleaner than looping? Dunno. But, then, I wouldn't care
as long as the code was in the above mentioned subroutine where its
intent was obvious and its impact was isolated.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Highly doubtful.



okay, thanks.

I could do it by tracking the levels in a group of spreadsheet cells,
instead of an array in VB... that way, I could clear whatever portion of the
group of cells I wanted.

I was just looking for a more efficient programming method (not looping
through if it wasn't needed)...

But what I have runs quickly anyway, so it will be fine to the user.

Thanks Tom.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default clearing array contents

What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default clearing array contents

My answer was to a question about an array like this: (Your example, not
mine)
levArray(lev5, lev6, lev7, lev8)

representing some ill defined hierarchy. If you only have a one dimensional
array, then disregard what I said.

But you can't redim a part of an array in a single dimension to clear it.
However, looping through an array is extremely fast, so you may be attacking
something that isn't a problem at all.


--
Regards,
Tom Ogilvy

"mark" wrote in message
...
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it

flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper

part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default clearing array contents

Yes, when you use redim preserve you have to keep the lower bound
unchanged. But, since in your case you anyway want to throw away only
the values in the upper region of the array this technique should work.

However, and as Tom has already pointed out, looping through an array
is very fast. I suspect for even a large 1D array it is much faster to
loop and clear the contents than use redim's.

But that's not the point either. The primary purpose of good code is
to first and foremost be correct and then be easy to understand and
maintain while meeting the implicit or explicit performance
requirements.

As I pointed out, I routinely use a separate procedure to write highly
modular code. When warranted, I also express a short and obvious idea
that requires multiple lines of code by putting those statements on a
single line.

In the code below *each* line contains a complete intent.

Sub testIt3()
Dim x() As Double, lb As Integer, ub As Integer, _
i As Integer
lb = 2: ub = 10: ReDim x(lb To ub)

For i = lb To ub: x(i) = i: Next i
ReDim Preserve x(lb To ub - 4): ReDim Preserve x(lb To ub)
For i = lb To ub: Debug.Print x(i): Next i

End Sub

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.

Reply
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
Clearing Contents AMANDA Excel Discussion (Misc queries) 2 July 6th 09 05:34 PM
Clearing contents not formatting JSnow Excel Discussion (Misc queries) 3 August 13th 08 04:26 PM
clearing the contents of specified cells Jimmy Pop Excel Programming 5 May 27th 05 01:17 AM
Clearing Contents Sean Excel Programming 5 May 6th 05 05:16 PM
Clearing Contents but not Formulas Louise Excel Worksheet Functions 6 January 27th 05 05:04 PM


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