Deleting a range in all worksheet - what am i doing wrong?

ParanoidAndroid

Board Regular
Joined
Jan 24, 2011
Messages
50
Hi Guys

I would be very thankful if you could tell me what is wrong with the following mothod.

I simply want to delete a range in all worksheets except for the one titled Monkey.

The range will always start at A2 and i want it to delete until the last cell in column J.

It seems to be correct in deleting as far as the last row but not the last column


Dim ws As Worksheet<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim Last_cell_in_Range As Range<o:p></o:p>

For Each ws In ActiveWorkbook.Worksheets
If ws.name <> “Monkey” then
Range("J2", Range("A65536").End(xlUp)).Name = "Last_cell_in_Range"<o:p></o:p>
Range("A2:Last_cell_in_Range").Delete<o:p></o:p>
<o:p> </o:p>
Endif
next
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You need to qualify the range with ws...

an example...untested

Code:
Sub AllWS()
    Dim ws As Worksheet
    Dim Last_cell_in_Range As Range
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> “Monkey” Then
            ws.Range("J2", Range("A65536").End(xlUp)).Name = "Last_cell_in_Range"
            ws.Range("A2:Last_cell_in_Range").Delete
        End If
    Next ws
End Sub
 
Upvote 0
Hello ParanoidAndroid,

Here is another way to do it...
Code:
  Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
      If ws.Name <> “Monkey” Then
         ws.Range("J2", ws.Cells(Rows.Count, "J").End(xlUp)).Delete
      End If
    Next ws
Sincerely,
Leith Ross
 
Upvote 0
just had a play with this and if you have any info in K it moves to A

are you sure you dont want to just clearcontents?
 
Upvote 0
Hi Dryver

I want to clear all contents except that in the first row for each sheet except for one of the sheets,

The contents in columns L and M are to remain in L and M. I dont wish for it to be shifted to columns A.

By All contents...this would be A2 to Column J (not affecting row 1)

This what your asking?
 
Upvote 0
yes what I am saying is if you want the worksheet to stay the same then use .clearcontents instead of .delete
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top