ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I use a default value or allow data entry to a cell (https://www.excelbanter.com/excel-programming/423332-how-do-i-use-default-value-allow-data-entry-cell.html)

m4goaldad

How do I use a default value or allow data entry to a cell
 
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.


Mike H

How do I use a default value or allow data entry to a cell
 
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

"m4goaldad" wrote:

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.


Gary''s Student

How do I use a default value or allow data entry to a cell
 
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


--
Gary''s Student - gsnu200830


"m4goaldad" wrote:

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.



All times are GMT +1. The time now is 01:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com