View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default VBA macro delete row - Help needed

gemiho wrote:

I wonder if you please could help me with a macro; I just started to use
VBA, so my knowledge is equal zero.
I have a workbook with a few sheets. I am trying to write a macro that
after clicking on a cell it deletes the entire row if the cell in column
A does not contain any text; actually I wanted it to do not delete the
row if the cell in column A contains the text “keepThisRow”, but I do
not know how to do it. I was thinking to use a Form button.

BellowI the code I have, but it does not work at all. thank you in
advance for all help


If you're thinking about using a form specifically because you don't know how
to do it automatically, you can put it in Worksheet_SelectionChange, in the
sheet's object, declared like so:

Private Sub Worksheet_SelectionChange (ByVal Target As Range)

Sub deleteRow_Click()
Dim rng As Range
ActiveSheet.Unprotect Password:="123"


Note that putting the password here lets anyone who can view your code see
it.

On Error GoTo ErrHandler

Set rng =
Worksheets(ActiveSheet).Range("A2:A500").ActiveCel l.Row.Select 'I want
to select a cell in row I want to delete
If Not rng Is Nothing Then
rng.EntireRow.Delete xlUp
End If


Change the above block (from "Set rng =" to "End If") to this:

If Len(Cells(ActiveCell.Row, 1).Value) < 1 Then _
ActiveCell.EntireRow.Delete xlUp

(Note that this will delete the row if cell A contains a formula that
evaluates to an empty string: "".)

If you want to delete the row if column A is *anything* but "keepThisRow",
use this instead:

If Cells(ActiveCell.Row, 1).Value < "keepThisRow" Then _
ActiveCell.EntireRow.Delete xlUp

Exit Sub


By exiting the sub in this manner, you aren't re-protecting the page. If you
*want* it protected afterward, delete the above line.

ErrHandler:

ActiveSheet.Protect Password:="123", DrawingObjects:=True,
Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True,
AllowSorting:=True
End Sub


--
WARNING: Continuous drinking may lead to continuous drinking.