Creating a proprer macro in Excel

  • Thread starter Derek Upson - Pioneer
  • Start date
D

Derek Upson - Pioneer

sorry for my ignorance. However, below is what I have so far....where would
you suggest putting the code below and do I need to replace any of the words
in the code below with a reference to my existing code?

Sub ClearContents()
'
' ClearContents Macro
' Clear all Fields in Table
'

'
Range("I6:p6").Select
Selection.ClearContents
Range("I7:p7").Select
Selection.ClearContents
Range("I9:p9").Select
Selection.ClearContents
Range("I10:p10").Select
Selection.ClearContents
Range("I11:p11").Select
Selection.ClearContents
Range("I12:p12").Select
Selection.ClearContents
Range("I13:p13").Select
Selection.ClearContents
Range("U6:X6").Select
Selection.ClearContents
Range("U7:X7").Select
Selection.ClearContents
Range("U52:X52").Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=12
Range("Q55:T55").Select
Selection.ClearContents
End Sub
 
F

FSt1

hi
the select statment is not needed in this case so it can be iliminated. also
the small scroll is not needed. in coding the scroll and select are only
good if you are watching the macro to stuff in design mode. in run mode
usually, things are happening too fast.
Range("I6:p6").ClearContents
Range("I7:p7").ClearContents
Range("I9:p9").ClearContents
Range("I10:p10").ClearContents
Range("I11:p11").ClearContents
Range("I12:p12").ClearContents
Range("I13:p13").ClearContents
Range("U6:X6").ClearContents
Range("U7:X7").ClearContents
Range("U52:X52").ClearContents
Range("Q55:T55").ClearContents

regards
FSt1
 
D

Derek Upson - Pioneer

Thanks FSt1. I will make those changes right now (great suggestions and I
understand them completely). However, what code would I add if I want a
pop-up box to verify the clearcontents before it is actually done. I am
trying to prevent someone from wiping out work accidentally that took 2
hours to input.
 
G

Gord Dibben

The macro recorder adds a great bunch of "Selects" you don't need.


Sub ClearContents()
Dim rcell as Range
msg = "Are you sure you want to clear range?"
response = MsgBox(msg, vbOKCancel)
If response = vbOK Then
MsgBox "You pressed OK. Cells will be cleared"
For Each rcell In _
ActiveSheet.Range("I6:p7, I9:p13, U6:X7, U52:X52, Q55:T55")
rcell.ClearContents
Next
Else
MsgBox "You pressed cancel. Nothing changed"
Exit Sub

End If
End Sub


Gord Dibben MS Excel MVP
 
J

JP

You could condense it down even further like this

Range("I6:p7").ClearContents
Range("I9:p13").ClearContents
Range("U6:X7").ClearContents
Range("U52:X52").ClearContents
Range("Q55:T55").ClearContents


--JP
 
D

Derek Upson - Pioneer

thanks for the first attempt. It looks like this is a problem since some of
the cells are partially merged? it is giving me a problem. Any
suggestions?
 
G

Gord Dibben

What is a "parially" merged cell?

Cells are merged or not.

Suggestions...................GET RID OF MERGED CELLS!

Note the caps which denotes shouting really loudly.


Gord
 
G

Gord Dibben

And even further like this..........


For Each rcell In _
ActiveSheet.Range("I6:p7, I9:p13, U6:X7, U52:X52, Q55:T55")
rcell.ClearContents


Gord
 

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