Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Clear UsedRange except specific cells?

I have some code which currently loops through my worksheets (30+) and
'values out' the UsedRange (approx A1:Z50). I'd like to use a
hyperlink in cells A4:C4 (merged cells) to let users navigate back to
a Summary page. However, my code wipes out that formula ...

If there a way to exclude these cells (A4:C4) from the code to value
out the UsedRange?

rgds, ray

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Clear UsedRange except specific cells?

Sub clearexcept()
Set rp = Range("A4:C4")
For Each r In ActiveSheet.UsedRange
If Intersect(r, rp) Is Nothing Then
r.Clear
End If
Next
End Sub

--
Gary''s Student - gsnu200735


"Ray" wrote:

I have some code which currently loops through my worksheets (30+) and
'values out' the UsedRange (approx A1:Z50). I'd like to use a
hyperlink in cells A4:C4 (merged cells) to let users navigate back to
a Summary page. However, my code wipes out that formula ...

If there a way to exclude these cells (A4:C4) from the code to value
out the UsedRange?

rgds, ray


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Clear UsedRange except specific cells?

One way:

Dim ws As Worksheet
Dim rToClear As Range
Dim rMask As Range
For Each ws In ThisWorkbook.Worksheets
With ws
Set rMask = Union(.Rows(5).Resize(.Rows.Count - 4), _
.Columns(4).Resize(4, .Columns.Count - 3))
Set rToClear = Intersect(.UsedRange, rMask)
If Not rToClear Is Nothing Then rToClear.Clear
End With
Next ws



In article . com,
Ray wrote:

I have some code which currently loops through my worksheets (30+) and
'values out' the UsedRange (approx A1:Z50). I'd like to use a
hyperlink in cells A4:C4 (merged cells) to let users navigate back to
a Summary page. However, my code wipes out that formula ...

If there a way to exclude these cells (A4:C4) from the code to value
out the UsedRange?

rgds, ray

  #4   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Clear UsedRange except specific cells?

Hi G.S. -

I tried to implement your suggestion, but the code errored out on the
Intersect line .... here is the error and the code as I'm using it:

RunTime 1004: Method 'Intersect' of Object '_Global' failed


Sub ValueOutDSRs()
Dim sh As Worksheet

Application.Calculation = xlCalculationManual
For Each sh In Sheets(Array("DSR - 152",<a bunch of sheets here,"DSR
- 250", "DSR - 921"))

sh.Activate
Set rp = Range("A4:C4")
For Each r In ActiveSheet.UsedRange
If Intersect(r, rp) Is Nothing Then
r.Formula = r.Value
End If
Next r

Next sh
Application.Calculation = xlCalculationxlAutomatic

End Sub

Any thoughts?


  #5   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Clear UsedRange except specific cells?

A-HA! Figured it out ... the 'set rp...' line should read like this:

Set rp = ActiveSheet.Range("A4:C4")

NOW it works ...!




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Clear UsedRange except specific cells?

That's rather inefficient. Try:

Dim ws As Worksheet
Dim rConvert As Range
Dim rMask As Range
For Each ws In Worksheets(Array(...))
With ws
Set rMask = Union(.Rows(5).Resize(.Rows.Count - 4), _
.Columns(4).Resize(4, .Columns.Count - 3))
Set rConvert = Intersect(.UsedRange, rMask)
If Not rConvert Is Nothing Then _
rConvert.Value = rConvert.Value
End With
Next ws

which converts formulas to values in all the cells in the worksheet
(except those masked) in one step, rather than looping through each cell.

In article .com,
Ray wrote:

I tried to implement your suggestion, but the code errored out on the
Intersect line .... here is the error and the code as I'm using it:

RunTime 1004: Method 'Intersect' of Object '_Global' failed


Sub ValueOutDSRs()
Dim sh As Worksheet

Application.Calculation = xlCalculationManual
For Each sh In Sheets(Array("DSR - 152",<a bunch of sheets here,"DSR
- 250", "DSR - 921"))

sh.Activate
Set rp = Range("A4:C4")
For Each r In ActiveSheet.UsedRange
If Intersect(r, rp) Is Nothing Then
r.Formula = r.Value
End If
Next r

Next sh
Application.Calculation = xlCalculationxlAutomatic

End Sub

Any thoughts?

  #7   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Clear UsedRange except specific cells?

Holy Cow! JEM, you are most certainly right ... your code was MUCH
faster! A very un-scientific test showed a significant difference,
~10secs vs 145secs! I was originally a bit 'scared' of your code, as
I was having a hard time making sense of it .... I prefer to use code
that I can somewhat decipher.

Thanks very much for your input and persistence in getting me to try
it!

Ray


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
clear specific cells in a form vdmbqb Excel Discussion (Misc queries) 1 November 24th 07 08:07 PM
Is there a formula to clear the contents of a specific cell? Bob Smith Excel Worksheet Functions 2 December 9th 06 07:41 PM
How do I clear a column of data without clearing specific cells? EllenSwarts Excel Discussion (Misc queries) 2 April 5th 06 05:07 PM
UsedRange & Formatting of Cells JStone0218 Excel Programming 3 October 26th 03 11:36 PM
Using UsedRange as limits in a For Each loop but for cells on another sheet ? tur13o Excel Programming 2 October 23rd 03 01:18 PM


All times are GMT +1. The time now is 03:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"