Macro to delete specific rows in different worksheets

T

tim.carne

Hi All,

I've been looking around and have found plenty of examples of how to
delete rows of cells with blank entries, delete every nth row, etc,
but haven't found anything along the lines of what I'm wishing to do.

I have several worksheets, and I'm attempting to create a macro which
deletes (but doesn't delete the entire column, thus shifting every
other column left one..) a range in these worksheets.

I.e. I'm attempting to delete, in a worksheet "Data" every cell which
has an entry in it, in columns A and B - the reason for this, is that
I have a another column C which stores a formula working off the 2
cells in these columns, and is linked to a different worksheet which
will be the "end" product - i.e. presenting the calculated values.

Would anyone be able to point me in the right direction?

Cheers
Tim.
 
G

Guest

Tim, you might be chasing a shadow with the delete and shift method. Excel
only supports the shifting of an entire column or row, not partials.
However, you could use the cut and paste methods to move specific ranges. As
an example, if you find and empty cell and want to shift all data from right
to left.

'Establish right boundry
lastCol = Cells(Columns.Count, 1).End(xlToLeft).Column
'insert code to find empty cells and for first empty
EmptyCell = CellFound.Address
Range(Cells(0, Range(EmptyCell).Offset(0, 1).Col), Cells(0, lastCol).Cut
Range(EmptyCell)

This would have to be in a loop to execute for each emty cell found.
 
G

Guest

Tim, disregard the first posting i made. I re-read yours and see that i was
assuming you wanted to find empty cells. Now i see that you want to delete
cells with data, without shifting the rows or columns. This can be done by
simply setting a range within column A or B or both and using the
ClearContents command.
The method used for selecting the range to be deleted will depend on whether
your data is contiguous, whether you know the exact cell references or
whether you have to establish the range based on certain criteria and then
offset from key reference points to the end of the range. I can't offer any
code for that without knowing how your data is constructed in Columns A and B
and if it is consistent throughout all worksheets.
 
G

Guest

If you only want to delete the cells that contain data in columns A and B then:

Sub delAandBdata()
Dim lastRow, dRng
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("$A$2:$B" & lastRow).ClearContents
End Sub

The
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top