Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Laaibah Malik   on Nov 14 In MS Office Category.

  
Question Answered By: Hariz Burki   on Nov 14

deleting rows  is easy.
to find the syntax, simply record a macro  and delete a row.
Then look at the macro.
It will say something like:

Rows("45:45").Select
Selection.Delete Shift:=xlUp

You can combine that to:

Rows("45:45").Delete Shift:=xlUp

Now, to make it fit your macro,
comment out the line:

Rows(I).EntireRow.Hidden = True
and add the line:

Rows(I).Delete Shift:=xlUp



Now, here's the tricky part.
You're searching from line #1 to the last line...
Say, you get to line 10 and decide to delete it.
10 is gone, line 11 is now 10.
but the next iteration of the loop moves to line 11,
which means that the current line 10 is never processed!

To get around that, we need to reverse the direction of the loop.

Change the line:

For I = 1 to Data_Rowcnt
to:
For I= Data_Rowcnt to 1 step -1

VBA is kind-of funny... you'd think that if you said to go from 10 to 1,
VBA would figure out that it would have to go BACKWARDS, but no..
instead it will start at 10 and go merrily along until it reaches 1..
which is ALMOST never.
If you don't Declare "I" as an Integer or Long data  type,
it defaults to "Variant". I think once you set it to Data_Rowcnt, then
it redefines itself as Integer, which means it's limited to 32,767.
But once beyond that, it switches to Long, so then it can go up to
2,147,483,647.
Then, if you have On Error Resume Next, it's possible it will reset to 0,
and your next loop is 1!!!
If not, then once it reaches 2,147,483,647 then it will kick out an error...
so, it's best to just TELL it what to use as an increment.

BTW.. this is also useful if you're dealing with blocks of data.
Say 4 lines to a record? Like:
Name
Address
City,State,Zip
Phone
then you can use:
For I = 1 to Rowcnt step 5
TName = Cells(I,1)
TAddr = Cells(I+1,1)
TCity = Cells(I+2,1)
TPhon = Cells(I+3,1)
(do something fun here)
Next I

Share: 

 

This Question has 21 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Hide / Show Rows based on Conditions Or get search suggestion and latest updates.


Tagged: