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
Solved

How do you clear contents from the smae multiple ranges on slected sheets in the workbook

Posted on 2011-09-28
10
190 Views
Last Modified: 2012-05-12
I need to clear contents from several indentical cells on multiple sheets within the workbook. I have tried the union command but I am getting anywhere.
0
Comment
Question by:bearblack
  • 4
  • 4
  • 2
10 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 36719457
You can't use Union across worksheets, it will only work if all the ranges are on the same worksheet.

Even then it still has some limitations.

What ranges are you trying to clear?

One way would be to create an array of the addresses of all the ranges and loop through the worksheets.
Dim ws As Worksheet
Dim arrRngAddrs
Dim I As long

  arrRngAddrs = Array("A1:A2", "B1:B40", "G1:L1")

  For I = LBound(arrRngAddrs) To UBound(arrRngAddrs)


       For Each ws In Worksheets

             ws.Range(arrRngAddrs(I)).Clear

       Next ws

  Next I

Open in new window

This will clear the ranges on all worksheets but you can change so it's only specific worksheets.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36719464
Select the sheets before selecting the range, then all sheets will be affected.
0
 
LVL 2

Author Comment

by:bearblack
ID: 36719483
It is only for select worksheets within the workbook

    Range("c7:R7,V7").ClearContents
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Author Comment

by:bearblack
ID: 36719493
I didn't understand the selecting of specific sheets and how the code will only work for those selected
0
 
LVL 33

Expert Comment

by:Norie
ID: 36719522
For only certain worksheets.

For Each ws In Worksheets

       Select Case ws.Name
           Case "SheetNotToClear", "AnotherNotToClear" ' add names of sheets NOT to clear
              ' do nothing
           Case Else
              ws.Range("C7:R7, V7").ClearContents
      End Select
Next ws

Open in new window

You don't need to list all the worksheet names if there's some other way to identify them, eg  names all begin with 'Sales'.
0
 
LVL 2

Author Comment

by:bearblack
ID: 36719525
   Range("c7:R7,V7").Select
    Selection.ClearContents
    Rows("8:8").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete shift:=xlUp

The above is what I am wanting to do on select worksheets
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36719533
Mine was not a code solution so go with imnorie.
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 36719576
With the delete:
Dim ws As Worksheet
For Each ws In Worksheets

       Select Case ws.Name
           Case "SheetNotToClear", "AnotherNotToClear" ' add names of sheets NOT to clear
              ' do nothing
           Case Else
              ws.Range("C7:R7, V7").ClearContents
              ws.Rows("8:" & ws.Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete Shift:=xlUp
      End Select
Next

Open in new window

0
 
LVL 2

Author Comment

by:bearblack
ID: 36719612
Would you mine explaining the case function?
0
 
LVL 33

Expert Comment

by:Norie
ID: 36719687
A select case compares a value against the criteria of each case and when it finds a match the for that that case is executed.

If there is no match and there's an 'Else' case then that code is executed.

I know that probably doesn't make much sense, I'm not very good with explanations.

You don't actually need to use it here, I just find that with this sort of thing it's easier than using some long If statement.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

790 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