Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro definition help
Sub DeleteHeadings()
Dim rng As Range Dim target As String target = InputBox("Enter Search Term") If target = "" Then Exit Sub Do If Not rng Is Nothing Then rng.Resize(8, 1).EntireRow.Delete End If Set rng = Nothing Set rng = Cells.Find(What:=target, _ After:=ActiveCell, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) Loop While Not rng Is Nothing End Sub Can someone explain me in simple words what is this macro performing step by step. I would really appreciate that. Thanks. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Macro definition help
I can try.
Dim rng as Range declares a variable that may refer to a cell, or group of cells or other Excel "Range" Dim target As String declares a variable that may contain a text value target = InputBox("Enter Search Term") requesting you provide a term/phrase/value you want to search for later If target="" Then Exit Sub if you didn't enter anything for a search term in the InputBox statement, then get out. the Do .... Loop While Not rng Is Nothing Is Nothing is a condition examining the variable rng and asking "did this get assigned to some real world range in my workbook). If rng does not refer to anything, it will be "Is Nothing", so Not ... Is Nothing is saying "do this while rng is referring to some real-world range". Inside of the loop, rng actually starts off as Is Nothing so that If Not rng Is Nothing Then rng.Resize(8,1).EntireRow.Delete End if never gets executed the first time thru the loop, but then is continually executed until there is no match found for the phrase entered earlier. But when rng does refer to (a cell) the .Resize(8,1) then makes it refer to that cell and the 7 cells below it, then by referring to .EntireRow it is now referring to the row the match was found in along with the next 7 rows, and the .Delete does exactly that: deletes the 8 rows! Set rng = Nothing sets rng back to nothingness so that if no match is found in the next statement, the loop will end. The next statement is a simple .Find command that looks for the phrase you entered. If it is found, then rng will be set to the cell/range where it was found, and the loop will continue and that cell/range and the 7 rows below it will be deleted. Repeat until no more matches are found. "andresg1975" wrote: Sub DeleteHeadings() Dim rng As Range Dim target As String target = InputBox("Enter Search Term") If target = "" Then Exit Sub Do If Not rng Is Nothing Then rng.Resize(8, 1).EntireRow.Delete End If Set rng = Nothing Set rng = Cells.Find(What:=target, _ After:=ActiveCell, _ LookIn:=xlFormulas, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) Loop While Not rng Is Nothing End Sub Can someone explain me in simple words what is this macro performing step by step. I would really appreciate that. Thanks. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
name definition | Excel Programming | |||
Macro to Uncheck Save Query Definition | Excel Programming | |||
Range Definition in Macro ? | Excel Programming | |||
Syntax problem in defining OnAction macro definition held in Add-in! | Excel Programming | |||
Remove Query Definition with Macro or VBA | Excel Programming |