macro

C

Carl Johnson

I'm new at this so this might seem elementary but if you don't know you ask
those that do.
I've created a macro that is suppose to clear the input data on a worksheet
when a command button is clicked. However when the msgbox appears asking
them if they wish to continue and they click "yes" nothing happens.I've
included the code that I've written. Any help would be appreciated.

Private Sub cmdclear_Click()
MsgBox "Caution: You are about to delete the information on this worksheet"
& vbNewLine & vbNewLine & "Do you wish to continue?", 4, "Clear"
If response = vbYes Then
copyoffatburner.xls = Application.Run("Clear")
Else: End
End If
End Sub
 
J

John Wilson

Carl,

There's nothing wrong with the basic "logic" of your coding.
If the user selects "Yes", the line of code:
copyoffatburner.xls = Application.Run("Clear")
will execute.

Now...if "copyoffatburner.xls" is the file that you're already in,
you don't need to specify it.
If so, also, you don't need to specify "Application.Run"

Private Sub cmdclear_Click()
MsgBox "Caution: You are about to delete the information on this worksheet"
& vbNewLine & vbNewLine & "Do you wish to continue?", 4, "Clear"
If response = vbYes Then
Clear
Else: Exit Sub
End If

Okay, so what's the code in the "Clear" sub???

John
 
D

Dave Peterson

One more take:

Private Sub cmdclear_Click()
dim response as long
response = MsgBox("Caution: You are about to delete the information" & _
" on this worksheet" & vbNewLine & vbNewLine & _
"Do you wish to continue?", 4, "Clear")

If response = vbYes Then
worksheets("sheet1").range("Myrangename").clear '.clearcontents
end if

End Sub

This assumes that you named your range "myrangename" via Insert|Name from Excel.

Your original message box just showed the message and response was never
changed.

(and I like .clearcontents to just empty the cells. Clear will remove
formatting, too.)
 
C

Carl Johnson

To both you and John for responding to my inquiry. I first tried John's but
it still did not run the macro. I then tried yours Dave and while it worked
unfortunately as you said it also deleted the formatting and comments as
well I,ve again included the code as I wrote it, maybe I am overlooking
something. Again your help would be appreciated.
Private Sub cmdclear_Click()
Dim response As Long
response = MsgBox("Caution: You are about to delete the information on this
worksheet." & _
vbNewLine & vbNewLine & "Do you wish to continue?", 4)

If response = vbYes Then
Worksheets("January").Range("Clear").Clear '.clearcontents
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top