Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Stepping backward through an array

Please help if possible:

I am trying to step backward through an array and my code
looks like the following:

dim lngX as long
For lngX = UBound(arrRows) To 1 Step -1
Rows(arrRows(lngX)).EntireRow.Delete
Next lngX

It seems to me this should work. Does anyone see any
errors?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Stepping backward through an array

I have just tried, loading arrows as follows

Dim arrRows(3)

arrRows(0) = 3
arrRows(1) = 5
arrRows(2) = 7
arrRows(3) = 8

and it worked as I expected.

What are you seeing?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"quartz" wrote in message
...
Please help if possible:

I am trying to step backward through an array and my code
looks like the following:

dim lngX as long
For lngX = UBound(arrRows) To 1 Step -1
Rows(arrRows(lngX)).EntireRow.Delete
Next lngX

It seems to me this should work. Does anyone see any
errors?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Stepping backward through an array

quartz,

For IngX = UBound(arrRows) To 1 Step -1

Should be changed to:

For IngX = UBound(arrRows) To 0 Step -1

or:

For IngX = UBound(arrRows) to LBound(arrRows) Step -1

Remember that arrays are defined starting from 0. (i.e. Dim MyArray(
To 10) as String

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Stepping backward through an array

array sdon't "always" start base 0 or base 1

eg
Dim x(-5 To -2, 5 To 20) As String

Your recommendation to use both UBOUND() and LBOUND() is
the only one that would work in these cases.


The issue with the original code is really more to do
with the values in the array, not the direction through
which it is looped.
Deleting rows should be done from the highest row number
to the lowest to avoid "losing" our initial row indexs
and deleleting the wrong row.
If we want to delete rows 3 and 5. deleting row 3 makes
waht was row 5 now row 4. our code would deleet row 5,
thus corrupting our data. Hence, delete 5 them 3.

The key then to the success of the loop s that the array
must have the data sorted into ascending order.
The loop could as easily be done normally if the data is
sorted descending.

Patrick Molloy
Microsoft Excel MVP




-----Original Message-----
quartz,

For IngX = UBound(arrRows) To 1 Step -1

Should be changed to:

For IngX = UBound(arrRows) To 0 Step -1

or:

For IngX = UBound(arrRows) to LBound(arrRows) Step -1

Remember that arrays are defined starting from 0. (i.e.

Dim MyArray(0
To 10) as String)


---
Message posted from http://www.ExcelForum.com/

.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Stepping backward through an array

Pyball, In VBA arrays can be define with lowerbounds other than zero
using the syntax you supplied.

--

Pyball wrote in message ...

Remember that arrays are defined starting from 0. (i.e. Dim MyArray(0
To 10) as String)



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Stepping backward through an array

Is your array multi-dimensional, perhaps?

If you defined your array something like this:

Dim arrRows As Variant
arrRows = Sheet1.Range("A6:A15").Value

then you will have a 2D array, thus you'd need to change the your code
to this:

Dim lngX As Long
For lngX = UBound(arrRows, 1) To 1 Step -1
Rows(arrRows(lngX, 1)).EntireRow.Delete
Next lngX

Aside: consider qualifying what Rows refers to e.g. Sheet1,
Application.ActiveSheet, etc.

--

"quartz" wrote in message ...
Please help if possible:

I am trying to step backward through an array and my code
looks like the following:

dim lngX as long
For lngX = UBound(arrRows) To 1 Step -1
Rows(arrRows(lngX)).EntireRow.Delete
Next lngX

It seems to me this should work. Does anyone see any
errors?

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
Stepping in to a cell Max Excel Discussion (Misc queries) 3 April 27th 10 02:44 AM
Stepping through Code Jim May Excel Discussion (Misc queries) 9 October 25th 07 01:40 PM
stepping chart Petje Charts and Charting in Excel 2 July 7th 06 10:22 PM
Stepping Through vs. Play CWillis Excel Discussion (Misc queries) 1 June 15th 06 05:34 PM
Stepping through a column using FOR/NEXT Joe Mathis Excel Programming 1 December 3rd 03 06:36 AM


All times are GMT +1. The time now is 06:54 PM.

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"