Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Business Applications
Tag Cloud
access acer asus batch bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Solved: Excel Macro -> Delete Multiple Rows

Reply  
Thread Tools
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
18-Nov-2009, 05:46 PM #1
Question Solved: Excel Macro -> Delete Multiple Rows
I am trying to find a way to find a specific text within a worksheet, select that cell and the 9 rows under that row, and delete the 10 rows.
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 09:28 AM #2
I forgot to mention that the code will actually run from a different worksheet.

So, sheet two will actual contain the code to delete the rows from sheet 1.
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 09:50 AM #3
Hey C'man.

try this...

Code:
Sub Macro1()
This = "0.00169909838200311" ' Change this value to whatever you want to search for
Rows(Cells.Find(What:=This).Row & ":" & Cells.Find(What:=This).Row + 9).Delete Shift:=xlUp
End Sub
You can have it on any woorkbnook you want and run it on anyworkbook you want also long as you Activate the workbook first before running it.
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 09:56 AM #4
As usual...

NB. Make backup of stuff before testing code
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 09:58 AM #5
Thanks turbodante. Is there a way to change this portion of the code
Code:
& Cells.Find(What:=This).Row + 9).Delete Shift:=xlUp
to find the next blank cell?

Also, is there not a way to specify the worksheet within the code instead of activating the worksheet?

Something like this
Code:
 wsTest.Rows(Cells.Find(What:=This).Row
Where wsTest is set/assigned to a specific worksheet name.
__________________
Computerman29642
"Why so serious?....let's put a smile on that face" - The Joker

Last edited by computerman29642; 19-Nov-2009 at 10:03 AM..
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 10:05 AM #6
I don't understand.
  • is the code deleting a row short,
  • or do you want to include from the match to down 10 rows, plus additional to next blank
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 10:09 AM #7
The code is falling a row short. There will always be a blank row betwene the rows being deleted, and the rows below the ones being deleted.
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 10:18 AM #8
Just so I understand...

If your match is in row 10, you want to delete rows

10,11,12,13,14,15,16,17,18,19 & 20 that's 11 rows?
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 10:21 AM #9
That is correct. The reason for looking for the blank row is to be sure that all the rows in the correct range are selected.

I really did not want to specify teh number of cells because you never know if that number will increase.
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 10:50 AM #10
turbodante, I believe I figured out the code. Please take a look below:

Code:
Rows(Cells.Find(What:=This).Row & ":" & Cells.Find(What:="", After:=ActiveCell).Row).Select
I used select for testing. What do you think?
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 10:58 AM #11
try this one...

It will delete from the matching row to the farthest blank row on the same column. NB. It may be that you have more than 1 blank, so it finds the blank before the next cell that contains a non-blank.

Code:
Sub Macro4()
Dim Rw1 As Long, Rw2 As Long
Set xSheet = Sheets("Sheet1")
This = "here is what you want to find"  ' change this to what you need to match
On Error Resume Next
   Rw1 = Cells.Find(What:=This).Row
   Rw2 = Cells.Find(What:=This).End(xlDown).End(xlDown).Offset(-1, 0).Row
 
xSheet.Rows(Rw1 & ":" & Rw2).Delete Shift:=xlUp
 
End Sub
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 11:21 AM #12
Nevermind, the code I posted does not work properly after further testing.

turbodante, I am unable to get your new code to work.
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 11:33 AM #13
Quote:
Originally Posted by computerman29642 View Post
...

turbodante, I am unable to get your new code to work.

What errors are you getting? Can you post a sample of your data?
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
19-Nov-2009, 11:37 AM #14
I was finally able to get the code to work, but it is still selecting on row short.
turbodante's Avatar
Senior Member with 744 posts.
 
Join Date: Dec 2008
Location: GMT UK
19-Nov-2009, 11:45 AM #15
Quote:
Originally Posted by computerman29642 View Post
I was finally able to get the code to work, but it is still selecting on row short.

Can you check if the very last blank cell truly blank. ie. is not " ", or a space character?
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:07 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.