View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Bovey Rob Bovey is offline
external usenet poster
 
Posts: 811
Default 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