EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by DanCLi » Fri, 23 May 1997 04:00:00



I am trying to code a proceedure by which my program can take user input
from a user form text box.....Use this information to search a
worksheet.....find a matching entry....then delete the row of information
associated with it.  I understand the concept, but the scripting is
eluding me....

I appreciate any help which is offered and any questions asked....


 
 
 

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by Jorma Heikkine » Sun, 25 May 1997 04:00:00



> I am trying to code a proceedure by which my program can take user input
> from a user form text box.....Use this information to search a
> worksheet.....find a matching entry....then delete the row of information
> associated with it.  I understand the concept, but the scripting is
> eluding me....

DanCLink

That is a tall order!

Perhaps this idea will help.

Jorma U. Heikkinen

Quote:> Sometimes it is necessary to extrapolate and even speculate, based on minimal amounts of data.

Sub Test()
Dim MyRange As Range, Shiitti As Worksheet, Found As Boolean
Dim Celli As Range
On Error GoTo Trappi
For Each Shiitti In ActiveWorkbook.Worksheets
  Do
    Found = False
    'Focus your search, for example:
    Shiitti.Cells.SpecialCells(xlTextValues).Select
    'xlNumbers, xlLogical etc. etc.
    For Each Celli In Selection
      If Celli = "UserInput" Then
        Found = True
        Rows(Celli.Row).Select
        Selection.Delete Shift:=xlUp
      End If
    Next
  Loop While Found
Next
Exit Sub
Trappi:
Exit Sub
End Sub

 
 
 

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by Jorma Heikkine » Sun, 25 May 1997 04:00:00


BTW again

My posting was just an idea, not a well-thought solution. For example, I
did not test the need for the loop, just thought that perhaps the
deletion of a row could mix up the selection. Come to think of it, I
also believe that on error one should resume next, not exit the sub.

Jorma U. Heikkinen

Quote:> A key idea in management is the never-ending improvement of all processes.

 
 
 

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by CMeach » Mon, 26 May 1997 04:00:00


     There is actually a fairly simple method of doing what you ask.

    RowToDelete=Application.WorksheetFunction.Match (TextToMatch,_
          Worksheets("FirstSheet").Range("A1:A1000"),0)

    Worksheets("FirstSheet").Rows(RowToDelete).EntireRow.Delete

     If you have Option Explicit turned on you will have to Dim
RowToDelete as an integer before using it.  As well you have to make sure
that you substitute the name of the worksheet you are using for
FirstSheet.  You can make the range "A1:A1000" anything you would like but
I suggest keeping it in a single column.

     Not to confuse the issue but if you are calling the macro directly
from the affected worksheet there is an even easier way to do this.  As
follows...

    RowToDelete=Application.WorksheetFunction.Match (TextToMatch,_
          ActiveSheet.Range("A1:A1000"),0)

    ActiveSheet.Rows(RowToDelete).EntireRow.Delete

    Continuing to prove there is more than one way to skin a cat (not to
offend cat lovers!)  you can also try the following.  It is essentially
the same as above but skips a step.

     ActiveSheet.Rows(Application.WorksheetFunction.Match (TextToMatch,_
          ActiveSheet.Range("A1:A1000"),0)).EntireRow.Delete

    All this does is eliminate the step of separately figuring which row
to delete.

    Almost any function or formula that you can use in an Excel
Spreadsheet can be used in VBA5.  Application.WorksheetFunction can be a
very powerful tool as witnessed by the brevity of the solution above as
compared to some of the others.  Any macro that accomplishes its job is a
good one but usually the smaller the better.

 
 
 

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by Jorma Heikkine » Thu, 29 May 1997 04:00:00



>  Almost any function or formula that you can use in an Excel Spreadsheet can be used in VBA5.

True, but only if the number of elements, for example the size of a VBA
array, is 5461 or less.

Jorma U. Heikkinen

 
 
 

EXCEL'97 VBA- Find and Delete Rows based on data in spreadsheet

Post by Harri Kinnune » Fri, 30 May 1997 04:00:00



> > I am trying to code a proceedure by which my program can take user
> input
> > from a user form text box.....Use this information to search a
> > worksheet.....find a matching entry....then delete the row of
> information
> > associated with it.  I understand the concept, but the scripting is
> > eluding me....

> DanCLink

Assuming the information to search for is in a specific column, the this
should delete all rows countaining the searched value..

TempSheet.Rows(1).Insert xlDown
TempSheet.UsedRange.AutoFilter DateColumn, AsOfDate
TempSheet.UsedRange.Delete xlUp

-Harri

.