Basic Variable Problem

hdbkiwi

New Member
Joined
Sep 22, 2014
Messages
10
Hi Everyone,

First off, a huge thanks to everyone helping out here - I love this community and have learned so much from reading other threads.
:)
I'm trying to do a very simple for loop that cycles through sheets and executes a long code within that sheet. However, for some reason, the for loop is not working and the code only executes on the first sheet. My code is below:
Code:
Sub RowDel()


x = Worksheets.Count
For J = x To 2 Step -1
    Application.DisplayAlerts = False
    
With Sheets(J) 'example code to execute
Rows("1:1").Delete Shift:=xlUp
Rows("3:3").Delete Shift:=xlUp
End With


Next J
Application.DisplayAlerts = True
End Sub

Any ideas?
 
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Your With block doesn't actually do anything currently as you haven't qualified the Rows property calls:
Rich (BB code):
With Sheets(J) 'example code to execute
.Rows("1:1").Delete Shift:=xlUp
.Rows("3:3").Delete Shift:=xlUp
End With
is what you need.
 
Upvote 0
Oh goodness, so basic, thank you very much! Can't believe I missed that.

It's gone through and is now deleting the first rows which is amazing.

I thought what I would do next is delete out rows that are of a certain colour, but in order to do that I need to reset the used range in each worksheet.

I then go on in my code to write:



Dim rng As Range
Set rng = Sheets(J).UsedRange
rng.Select




RowCount = rng.Rows.Count
For i = RowCount To 2 Step -1
If Cells(i, 2).Interior.ColorIndex = 14 Or Cells(i, 2).Interior.ColorIndex = 43 Then Cells(i, 1).EntireRow.Delete
Next i

--

However, it stops at "rng.Select" with a Run-time error of '1004' Select method of Range class failed.

Any ideas?
 
Upvote 0
You cannot select a range on a sheet without activating that sheet first. However, there is no need to select the range so simply remove that line. And note that you have not qualified your Cells calls with a worksheet...
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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