Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Subscript Out of Range When clearing contents

Posted on 2014-02-28
16
Medium Priority
?
668 Views
Last Modified: 2014-03-08
I have noticed when I occasionally run the below code from the VBA panel I get Subscript Out of Range.  This only seems to happen if I were last in the worksheet called "REPORT-JAN".  If I then manually go to another worksheet and re-run the SUB then I do not have this issue..Any ideas why I get this message...Why should it matter what worksheet I am in when clearing a worksheet ?

Also if I want to clear everything in A-R, then what is the syntax for this...I fear that one day my data may go beyond row 5,000..Should I just find the LastRow or is there an easier way...

With Sheets ("REPORT-JAN")
.RANGE("A1:R5000").Clear Contents
End With
0
Comment
Question by:upobDaPlaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39896515
Check the name of the worksheet (tab name). If it isn't spelled exactly the same you will get that error.

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39896516
I'm sure this is not your problem and only a typing mistake, but this:

Clear Contents

should be:

ClearContents

Kevin
0
 

Author Comment

by:upobDaPlaya
ID: 39896533
I checked and the TabName is spelled correctly and I have ClearContents
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 800 total points
ID: 39896536
Are you working with multiple workbooks? If so, try this:

With ThisWorkbook.Sheets ("REPORT-JAN")
.RANGE("A1:R5000").Clear Contents
End With

Kevin
0
 

Author Comment

by:upobDaPlaya
ID: 39896562
No..I am just working with 1 workbook relative to the code.  However, I did have multiple workbooks open at the time..

Whats the rule when you need to include Workbook...
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39896566
You should always include it. Otherwise you are depending on Excel to determine where the Sheets collection is. I always use fully qualified references to ensure I get exactly what I am looking for.

So my code change did not help?

Kevin
0
 

Author Comment

by:upobDaPlaya
ID: 39896595
It works now, however I was able to get it to work prior to my post, but occasionally it I would get the subscript msg.  Now that I have qualified the WS lets see if I no longer get any errors..I will run it multiple times tomorrow pm and let u know..thx
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 800 total points
ID: 39896632
Further to Kevin's suggestion if you might go beyond 5000 rows of data, I often use the Intersect method to restrict the range to only those rows being used:
With ThisWorkbook.Worksheets("REPORT-JAN")
    Intersect(.UsedRange, .Range("A:R")).ClearContents
End With

Open in new window

Brad
0
 
LVL 33

Assisted Solution

by:Rob Henson
Rob Henson earned 400 total points
ID: 39900195
When clearing the data, is column R the full extent of the data or is there is anything beyond R which you are keeping?

If R is the full extent you can use the "Cells.Select" statement to select everything. I assume therefore that you could use "Cells.ClearContents" to clear everything.

Thanks
Rob H
0
 

Author Comment

by:upobDaPlaya
ID: 39902370
Some very good feedback indeed. I do have data beyond col R, however I only want to delete thru col r, thus it sounds like the Intersect method would be the best ?
0
 
LVL 81

Expert Comment

by:byundt
ID: 39902392
All of the methods suggested will clear the contents of A1:R5000. The Intersect method will go beyond row 5000 should data exist in those rows.

I assume by "delete" that you really mean to clear the contents. Actually deleting those cells (and moving up replacement cells from the bottom of the worksheet) would mess up any formulas that referenced the deleted cells.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39902424
That part of the code is fine. Only clear what you want to clear. If you want to clear from row 1 to the bottom of the worksheet then use:

    .RANGE("A:R").ClearContents

The ClearContents method is not frequently used so any decrease in speed clearing all rows versus the first 5,000 rows (which will be minimal anyway) will not be noticed.

Kevin
0
 

Author Closing Comment

by:upobDaPlaya
ID: 39905341
I appreciated the multiple solutions and the forward thing questions.  Much appreciated.
0
 

Author Comment

by:upobDaPlaya
ID: 39915124
Hi byundt..I am using the intersect method and it works great with the exception of what if I do not want the header row cleared..thus the range starts at A2..how then do I handle the part of your code that has ...... .Range("A:R")).ClearContents
0
 
LVL 81

Expert Comment

by:byundt
ID: 39915240
How about:
    Intersect(.UsedRange, .Range("A2:R" & .Rows.Count)).ClearContents

Open in new window

0
 

Author Comment

by:upobDaPlaya
ID: 39915253
Brilliant! Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question