Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I need to loop through an Excel spreadsheet from A1 through A65000 for
specific words and delete out the entire row containing that word. I also need to delete out blank rows. An example is: Range("A1").Select Cells.Find(What:="Payee/", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate Rows("53:53").Select Selection.Delete Shift:=xlUp Range("A1").Select Cells.Find(What:="Program d", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Rows("43:43").Select Selection.Delete Shift:=xlUp Unfortunately, the cell references are not important, even though the macro recorded my keystrokes. The important thing is to delete out the row that contains these specific words and continue the process until the worksheet does not contain any more of these words. Any help will be greatly appreciated. Thankyou. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try this...
Sub test() Call DeleteBlanks Call DeleteUnwanted("This") End Sub Sub DeleteUnwanted(ByVal DeleteWord As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngCurrent As Range Dim rngDelete As Range Dim rngFirst As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngCurrent = rngToSearch.Find(DeleteWord) If Not rngCurrent Is Nothing Then Set rngDelete = rngCurrent Set rngFirst = rngCurrent Do Set rngDelete = Union(rngCurrent, rngDelete) Set rngCurrent = rngToSearch.FindNext(rngCurrent) Loop Until rngCurrent.Address = rngFirst.Address rngDelete.EntireRow.Delete End If End Sub Sub DeleteBlanks() Dim wks As Worksheet Dim rngToSearch As Range Dim rngDelete As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngDelete = rngToSearch.SpecialCells(xlCellTypeBlanks) rngDelete.EntireRow.Delete End Sub -- HTH... Jim Thomlinson "Petra" wrote: I need to loop through an Excel spreadsheet from A1 through A65000 for specific words and delete out the entire row containing that word. I also need to delete out blank rows. An example is: Range("A1").Select Cells.Find(What:="Payee/", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate Rows("53:53").Select Selection.Delete Shift:=xlUp Range("A1").Select Cells.Find(What:="Program d", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Rows("43:43").Select Selection.Delete Shift:=xlUp Unfortunately, the cell references are not important, even though the macro recorded my keystrokes. The important thing is to delete out the row that contains these specific words and continue the process until the worksheet does not contain any more of these words. Any help will be greatly appreciated. Thankyou. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thank you, Jim. I tried all three (DeleteUnwanted did not compile), but
nothing happened. I am a novice (unfortunately) and merely copied and pasted your syntax into a new module, then ran them. Do I need to modify your syntax in any way before compiling? "Jim Thomlinson" wrote: Try this... Sub test() Call DeleteBlanks Call DeleteUnwanted("This") End Sub Sub DeleteUnwanted(ByVal DeleteWord As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngCurrent As Range Dim rngDelete As Range Dim rngFirst As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngCurrent = rngToSearch.Find(DeleteWord) If Not rngCurrent Is Nothing Then Set rngDelete = rngCurrent Set rngFirst = rngCurrent Do Set rngDelete = Union(rngCurrent, rngDelete) Set rngCurrent = rngToSearch.FindNext(rngCurrent) Loop Until rngCurrent.Address = rngFirst.Address rngDelete.EntireRow.Delete End If End Sub Sub DeleteBlanks() Dim wks As Worksheet Dim rngToSearch As Range Dim rngDelete As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngDelete = rngToSearch.SpecialCells(xlCellTypeBlanks) rngDelete.EntireRow.Delete End Sub -- HTH... Jim Thomlinson "Petra" wrote: I need to loop through an Excel spreadsheet from A1 through A65000 for specific words and delete out the entire row containing that word. I also need to delete out blank rows. An example is: Range("A1").Select Cells.Find(What:="Payee/", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate Rows("53:53").Select Selection.Delete Shift:=xlUp Range("A1").Select Cells.Find(What:="Program d", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Rows("43:43").Select Selection.Delete Shift:=xlUp Unfortunately, the cell references are not important, even though the macro recorded my keystrokes. The important thing is to delete out the row that contains these specific words and continue the process until the worksheet does not contain any more of these words. Any help will be greatly appreciated. Thankyou. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Define "did not compile". Did you compile it using Tools - compile in the VB
editor, or did an error get generated at runtime. The code does assume that you have a sheet1. This can be changed by changing the line: Set wks = Sheets("Sheet1") to either Set wks = Sheets("Add sheet name") or Set wks = Activesheet Let me know if this works... -- HTH... Jim Thomlinson "Petra" wrote: Thank you, Jim. I tried all three (DeleteUnwanted did not compile), but nothing happened. I am a novice (unfortunately) and merely copied and pasted your syntax into a new module, then ran them. Do I need to modify your syntax in any way before compiling? "Jim Thomlinson" wrote: Try this... Sub test() Call DeleteBlanks Call DeleteUnwanted("This") End Sub Sub DeleteUnwanted(ByVal DeleteWord As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngCurrent As Range Dim rngDelete As Range Dim rngFirst As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngCurrent = rngToSearch.Find(DeleteWord) If Not rngCurrent Is Nothing Then Set rngDelete = rngCurrent Set rngFirst = rngCurrent Do Set rngDelete = Union(rngCurrent, rngDelete) Set rngCurrent = rngToSearch.FindNext(rngCurrent) Loop Until rngCurrent.Address = rngFirst.Address rngDelete.EntireRow.Delete End If End Sub Sub DeleteBlanks() Dim wks As Worksheet Dim rngToSearch As Range Dim rngDelete As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngDelete = rngToSearch.SpecialCells(xlCellTypeBlanks) rngDelete.EntireRow.Delete End Sub -- HTH... Jim Thomlinson "Petra" wrote: I need to loop through an Excel spreadsheet from A1 through A65000 for specific words and delete out the entire row containing that word. I also need to delete out blank rows. An example is: Range("A1").Select Cells.Find(What:="Payee/", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate Rows("53:53").Select Selection.Delete Shift:=xlUp Range("A1").Select Cells.Find(What:="Program d", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Rows("43:43").Select Selection.Delete Shift:=xlUp Unfortunately, the cell references are not important, even though the macro recorded my keystrokes. The important thing is to delete out the row that contains these specific words and continue the process until the worksheet does not contain any more of these words. Any help will be greatly appreciated. Thankyou. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks Jim. I compiled using Tools and executed using Run, but it did not
perform any action. I did not receive an error but the sub did not appear in the macros list. I'll change the VB to the syntax below and try it out. I appreciate your help. "Jim Thomlinson" wrote: Define "did not compile". Did you compile it using Tools - compile in the VB editor, or did an error get generated at runtime. The code does assume that you have a sheet1. This can be changed by changing the line: Set wks = Sheets("Sheet1") to either Set wks = Sheets("Add sheet name") or Set wks = Activesheet Let me know if this works... -- HTH... Jim Thomlinson "Petra" wrote: Thank you, Jim. I tried all three (DeleteUnwanted did not compile), but nothing happened. I am a novice (unfortunately) and merely copied and pasted your syntax into a new module, then ran them. Do I need to modify your syntax in any way before compiling? "Jim Thomlinson" wrote: Try this... Sub test() Call DeleteBlanks Call DeleteUnwanted("This") End Sub Sub DeleteUnwanted(ByVal DeleteWord As String) Dim wks As Worksheet Dim rngToSearch As Range Dim rngCurrent As Range Dim rngDelete As Range Dim rngFirst As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngCurrent = rngToSearch.Find(DeleteWord) If Not rngCurrent Is Nothing Then Set rngDelete = rngCurrent Set rngFirst = rngCurrent Do Set rngDelete = Union(rngCurrent, rngDelete) Set rngCurrent = rngToSearch.FindNext(rngCurrent) Loop Until rngCurrent.Address = rngFirst.Address rngDelete.EntireRow.Delete End If End Sub Sub DeleteBlanks() Dim wks As Worksheet Dim rngToSearch As Range Dim rngDelete As Range Set wks = Sheets("Sheet1") Set rngToSearch = wks.Range("A1:A65000") Set rngDelete = rngToSearch.SpecialCells(xlCellTypeBlanks) rngDelete.EntireRow.Delete End Sub -- HTH... Jim Thomlinson "Petra" wrote: I need to loop through an Excel spreadsheet from A1 through A65000 for specific words and delete out the entire row containing that word. I also need to delete out blank rows. An example is: Range("A1").Select Cells.Find(What:="Payee/", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate Rows("53:53").Select Selection.Delete Shift:=xlUp Range("A1").Select Cells.Find(What:="Program d", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate Rows("43:43").Select Selection.Delete Shift:=xlUp Unfortunately, the cell references are not important, even though the macro recorded my keystrokes. The important thing is to delete out the row that contains these specific words and continue the process until the worksheet does not contain any more of these words. Any help will be greatly appreciated. Thankyou. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Find loop doesn't loop | Excel Discussion (Misc queries) | |||
Problem adding charts using Do-Loop Until loop | Excel Programming | |||
How do I create a For loop within a For loop? | Excel Programming | |||
Worksheet_Change - loop within a loop | Excel Programming | |||
HELP!!!! Can't stop a loop (NOT an infinite loop) | Excel Programming |