How do I use a default value or allow data entry to a cell

M

m4goaldad

H2 has a default value of 10. H2 needs to be changeable by a user and the
amount allowed in H2 is infinite, but if H1 = "no" then H2 must equal 0. If
H1 = "Yes" the H2 must equal the amount entered, or if no amount is entered
by user, it must equal the default value of 10. How in the world do I do
that.
 
M

Mike H

Hi,

Right click your sheet tab, view code and paste this in.


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$H$2" Then Exit Sub
Application.EnableEvents = False
If UCase(Range("$H1").Value) = "NO" Then
Target.Value = 10
End If
Application.EnableEvents = True
End Sub

Mike
 
G

Gary''s Student

Enter the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Set h2 = Range("H2")
Set h1 = Range("H1")
Set t = Target
If Intersect(t, h2) Is Nothing Then Exit Sub
If h1.Value = "Yes" Then Exit Sub
Application.EnableEvents = False
h2.Value = 10
Application.EnableEvents = True
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 

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