Sabtu, 16 Februari 2013

[ExcelVBA] How Do I Delete a Row?

 

In a For loop I examine each cell. I want to delete rows that do not contain a particular string. I didn't know how to delete a row, so I recorded a macro. When I arbitrarily deleted row 21, the recorder gave these statements

Rows("21:21").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp

I assumed that to delete Row J, (J is the counter for the loop) I simply had to replace the first statement with

Rows("J:J").Select

but that gave an "Application-defined or object-defined error". I checked that J had a valid value, 23, in this case.

10 minutes later

In desperation I replaced the Rows statement with

Row(J).Select

and that worked. So now I know how to delete a row, but can anyone explain why the statement that mirrored the one generated by the macro recorder, Rows("J:J").Select, didn't work.

Thanks
Michael Trombetta

__._,_.___
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)
Recent Activity:
----------------------------------
Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: http://www.mousetrax.com/techtrax to enter the ezine, then search the ARCHIVES for EXCEL VBA.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA

----------------------------------
More free tutorials and resources available at:
http://www.mousetrax.com

----------------------------------
.

__,_._,___

Tidak ada komentar:

Posting Komentar