ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why I got this 1004 runtime error (https://www.excelbanter.com/excel-programming/304112-re-why-i-got-1004-runtime-error.html)

Rob Bovey

Why I got this 1004 runtime error
 

You probably got the error because your worksheet wasn't active. You
can't select cells on a worksheet that isn't active. Change your macro to
the following and it will work regardless of whether the worksheet is active
or not:

Sub ClrSht(ShtNm)
With Sheets(ShtNm)
.Cells.ClearContents
End With
End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"hcova" wrote in message
...
I am trying to write a procedure that clear all in a sheet before write

something on it. I wrote the following code:
---------------------------
Option Explicit

Const MySht As String = "Sheet1" ' this is a valid sheet name in the

workbook

Sub ClrSht(ShtNm)
With Sheets(ShtNm)
.Cells.Select ' <== Here I receive the error !!!
Selection.ClearContents
End With
End Sub

Sub CallClrSht()
ClrSht(MySht)
End Sub
------------------------------

Anybody can explain me why I am getting a runtime error?
Regards





All times are GMT +1. The time now is 08:25 PM.

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