Something strange (at least for me).
When I record a macro that selects all the cells in a workbook and clear all content the code looks like this:
This works fine. However when I have the same code inside a commandobutton it fails and gives me the error code "application-defined or object defined error". Anyone that knows why and how this can be done correct? I want this to be done within the commandbutton code.Sub Macro14() Sheets("Values to EIS").Select Cells.Select Selection.ClearContents End Sub
The code that fails looks like:
Private Sub CommandButton2_Click() Sheets("Values to EIS").Select Cells.Select Selection.ClearContents End Sub
I would do this :
Private Sub Commandbutton1_Click() Dim sht As Worksheet Set sht = Worksheets("YourSheetName") sht.Cells.ClearContents End Sub
This would work anytime, anywhere in your workbook.
Thanks,
Vikas
As you aren't reusing the variable, you can just use:
or even:Sub Macro14() Sheets("Values to EIS").Cells.ClearContents End SubSub Macro14() Sheets("Values to EIS").Usedrange.ClearContents End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks