PC Review


Reply
Thread Tools Rate Thread

Clear Contents of a Selected Range

 
 
Connie
Guest
Posts: n/a
 
      11th Oct 2006
I'm using the following code to clear the contents of a selected range
on the "Compiled Totals" and "Upload Data" sheets. The code is called
from a command button which is on another sheet in the workbook:

Private Sub Clear_Contents_Click()

'Clear "Compiled Totals" and "Upload Data" sheets
Set sh = Worksheets("Compiled Totals")
Set rng = GetRealLastCell(sh)
Sheets("Compiled Totals").Range("$A$9:" + rng.Address).Select
Selection.ClearContents
Set sh = Worksheets("Upload Data")
Set rng = GetRealLastCell(sh)
Sheets("Upload Data").Range("$A$2:" + rng.Address).Select
Selection.ClearContents

End Sub

When I run the code, I get a 1004 error. What's wrong with my syntax?
Any help would be appreciated.

Connie

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      11th Oct 2006
You seem to be creating a multi-row range and trying to use that as the last
row. Try this

Private Sub Clear_Contents_Click()
With Worksheets("Compiled Totals")
.Range("$A$9:A" + .Cells(.Rows.Count,
"A").End(xlUp).Row).ClearContents
End With
With Worksheets("Upload Data")
.Range("$A$2:A" + .Cells(.Rows.Count,
"A").End(xlUp).Row).ClearContents
End With

End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Connie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm using the following code to clear the contents of a selected range
> on the "Compiled Totals" and "Upload Data" sheets. The code is called
> from a command button which is on another sheet in the workbook:
>
> Private Sub Clear_Contents_Click()
>
> 'Clear "Compiled Totals" and "Upload Data" sheets
> Set sh = Worksheets("Compiled Totals")
> Set rng = GetRealLastCell(sh)
> Sheets("Compiled Totals").Range("$A$9:" + rng.Address).Select
> Selection.ClearContents
> Set sh = Worksheets("Upload Data")
> Set rng = GetRealLastCell(sh)
> Sheets("Upload Data").Range("$A$2:" + rng.Address).Select
> Selection.ClearContents
>
> End Sub
>
> When I run the code, I get a 1004 error. What's wrong with my syntax?
> Any help would be appreciated.
>
> Connie
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear contents of a 3-d (three-dimensional) range excel@shoenfeltconsulting.com Microsoft Excel Programming 1 27th Aug 2010 08:13 PM
duplicate data in range clear contents baha17@gmail.com Microsoft Excel Programming 1 18th Nov 2009 03:18 PM
Iterate named range & clear contents Paul Wagstaff Microsoft Excel Discussion 6 11th Aug 2007 03:27 PM
Macro to clear range contents when cell contents are changed by us =?Utf-8?B?U3RldmUgRQ==?= Microsoft Excel Programming 12 22nd Feb 2007 09:09 PM
Automatically clear values from a range of selected cells =?Utf-8?B?Sm9obiBEYXZpZXM=?= Microsoft Excel Misc 1 28th Jun 2005 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:30 PM.