ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Clear Contents of a Selected Range (https://www.excelbanter.com/excel-programming/374849-clear-contents-selected-range.html)

Connie

Clear Contents of a Selected Range
 
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


Bob Phillips

Clear Contents of a Selected Range
 
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" wrote in message
oups.com...
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





All times are GMT +1. The time now is 11:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com