Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default simple question

Hi,

This seems like it should be a fairly simple task but i'm new to vba
and can't seem to figure it out. I have 2 cells in excel whose values have
to add up to 1.0. If the user changes the values in any cell and their
total value is not equal to one, a popup window should notify the user.
I've written the fuction in VB but how do i call it when the user changes
the cell values? Many thanks.

TIA
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 618
Default simple question

Hi

put the code in as a worksheet_change event against the sheet itself
(in the project explorer window double click on the sheet name and put the
code there rather than in a module)

also you'll need to use something like:-
If Not Intersect(Target, Range("A2")) Is Nothing Or _
Not Intersect(Target, Range("B2")) Is Nothing Then

at the top of your code to limit the code to run only when one or other of
the two nominated cells are changed.

let us know how you go

Cheers
JulieD

"someguy" wrote in message
7.142...
Hi,

This seems like it should be a fairly simple task but i'm new to vba
and can't seem to figure it out. I have 2 cells in excel whose values
have
to add up to 1.0. If the user changes the values in any cell and their
total value is not equal to one, a popup window should notify the user.
I've written the fuction in VB but how do i call it when the user changes
the cell values? Many thanks.

TIA



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 227
Default simple question

Hi

Place the following in the ThisWorkbook module. I'm assuming the 2 cells are
A1 and C1.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim r As Range
With ActiveSheet
Set r = .Range("A1,C1")
If Application.Sum(r) < 1 Then _
MsgBox "Numbers do not equal 1"
End With
End Sub

--
XL2002
Regards

William



"someguy" wrote in message
7.142...
| Hi,
|
| This seems like it should be a fairly simple task but i'm new to vba
| and can't seem to figure it out. I have 2 cells in excel whose values
have
| to add up to 1.0. If the user changes the values in any cell and their
| total value is not equal to one, a popup window should notify the user.
| I've written the fuction in VB but how do i call it when the user changes
| the cell values? Many thanks.
|
| TIA


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default simple question

a bit better to qualify this on just the cells in question

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim r As Range
If Target.Address = "$A$1" Or Target.Address = "$C$1" Then
Set r = Me.Range("A1,C1")
If Application.Sum(r) < 1 Then
MsgBox "Cell " & Target.Address & _
" has been changed," & vbCrLf & _
"the numbers do not now equal 1"
End If
End If
End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"William" wrote in message
...
Hi

Place the following in the ThisWorkbook module. I'm assuming the 2 cells

are
A1 and C1.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim r As Range
With ActiveSheet
Set r = .Range("A1,C1")
If Application.Sum(r) < 1 Then _
MsgBox "Numbers do not equal 1"
End With
End Sub

--
XL2002
Regards

William



"someguy" wrote in message
7.142...
| Hi,
|
| This seems like it should be a fairly simple task but i'm new to vba
| and can't seem to figure it out. I have 2 cells in excel whose values
have
| to add up to 1.0. If the user changes the values in any cell and their
| total value is not equal to one, a popup window should notify the user.
| I've written the fuction in VB but how do i call it when the user

changes
| the cell values? Many thanks.
|
| TIA




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 227
Default simple question

Hi Bob

The reason I didn't qualify the range is that the OP's post was slightly
unclear as to whether the two cells contained hard values or formulae. The
comment "If the user changes the values in ANY cell..." could imply that the
range in question contained formulae and other cells were being changed.

Notwithstanding, your point is well taken.

--
XL2002
Regards

William



"Bob Phillips" wrote in message
...
| a bit better to qualify this on just the cells in question
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| If Target.Address = "$A$1" Or Target.Address = "$C$1" Then
| Set r = Me.Range("A1,C1")
| If Application.Sum(r) < 1 Then
| MsgBox "Cell " & Target.Address & _
| " has been changed," & vbCrLf & _
| "the numbers do not now equal 1"
| End If
| End If
| End Sub
|
|
|
| --
|
| HTH
|
| RP
| (remove nothere from the email address if mailing direct)
|
|
| "William" wrote in message
| ...
| Hi
|
| Place the following in the ThisWorkbook module. I'm assuming the 2 cells
| are
| A1 and C1.
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| With ActiveSheet
| Set r = .Range("A1,C1")
| If Application.Sum(r) < 1 Then _
| MsgBox "Numbers do not equal 1"
| End With
| End Sub
|
| --
| XL2002
| Regards
|
| William
|
|

|
| "someguy" wrote in message
| 7.142...
| | Hi,
| |
| | This seems like it should be a fairly simple task but i'm new to
vba
| | and can't seem to figure it out. I have 2 cells in excel whose values
| have
| | to add up to 1.0. If the user changes the values in any cell and
their
| | total value is not equal to one, a popup window should notify the
user.
| | I've written the fuction in VB but how do i call it when the user
| changes
| | the cell values? Many thanks.
| |
| | TIA
|
|
|
|




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default simple question

Hi William,

That is a good point. It could be that the two cells are results of other
cells, in which case that would cause a problem with my suggested change.

The OP could also consider conditional formatting I guess.

Regards

Bob


"William" wrote in message
...
Hi Bob

The reason I didn't qualify the range is that the OP's post was slightly
unclear as to whether the two cells contained hard values or formulae. The
comment "If the user changes the values in ANY cell..." could imply that

the
range in question contained formulae and other cells were being changed.

Notwithstanding, your point is well taken.

--
XL2002
Regards

William



"Bob Phillips" wrote in message
...
| a bit better to qualify this on just the cells in question
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| If Target.Address = "$A$1" Or Target.Address = "$C$1" Then
| Set r = Me.Range("A1,C1")
| If Application.Sum(r) < 1 Then
| MsgBox "Cell " & Target.Address & _
| " has been changed," & vbCrLf & _
| "the numbers do not now equal 1"
| End If
| End If
| End Sub
|
|
|
| --
|
| HTH
|
| RP
| (remove nothere from the email address if mailing direct)
|
|
| "William" wrote in message
| ...
| Hi
|
| Place the following in the ThisWorkbook module. I'm assuming the 2

cells
| are
| A1 and C1.
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| With ActiveSheet
| Set r = .Range("A1,C1")
| If Application.Sum(r) < 1 Then _
| MsgBox "Numbers do not equal 1"
| End With
| End Sub
|
| --
| XL2002
| Regards
|
| William
|
|

|
| "someguy" wrote in message
| 7.142...
| | Hi,
| |
| | This seems like it should be a fairly simple task but i'm new to
vba
| | and can't seem to figure it out. I have 2 cells in excel whose

values
| have
| | to add up to 1.0. If the user changes the values in any cell and
their
| | total value is not equal to one, a popup window should notify the
user.
| | I've written the fuction in VB but how do i call it when the user
| changes
| | the cell values? Many thanks.
| |
| | TIA
|
|
|
|




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 212
Default simple question

Well he can still make it the cells are the result of a calculation:
If the cells in question are say A3 and B3 then

Private Sub Worksheet_Change(ByVal Target As Range)
Static lastTot As Single
Dim nowTot As Single
nowTot = Me.Range("A3").Value + Me.Range("B3").Value
If lastTot = nowTot Then Exit Sub
lastTot = nowTot
If nowTot 1 Then
MsgBox "Total of A3 and B3 is higher than 1"
End If
End Sub

"Bob Phillips" wrote in message
...
Hi William,

That is a good point. It could be that the two cells are results of other
cells, in which case that would cause a problem with my suggested change.

The OP could also consider conditional formatting I guess.

Regards

Bob


"William" wrote in message
...
Hi Bob

The reason I didn't qualify the range is that the OP's post was slightly
unclear as to whether the two cells contained hard values or formulae.
The
comment "If the user changes the values in ANY cell..." could imply that

the
range in question contained formulae and other cells were being changed.

Notwithstanding, your point is well taken.

--
XL2002
Regards

William



"Bob Phillips" wrote in message
...
| a bit better to qualify this on just the cells in question
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| If Target.Address = "$A$1" Or Target.Address = "$C$1" Then
| Set r = Me.Range("A1,C1")
| If Application.Sum(r) < 1 Then
| MsgBox "Cell " & Target.Address & _
| " has been changed," & vbCrLf & _
| "the numbers do not now equal 1"
| End If
| End If
| End Sub
|
|
|
| --
|
| HTH
|
| RP
| (remove nothere from the email address if mailing direct)
|
|
| "William" wrote in message
| ...
| Hi
|
| Place the following in the ThisWorkbook module. I'm assuming the 2

cells
| are
| A1 and C1.
|
| Private Sub Worksheet_Change(ByVal Target As Excel.Range)
| Dim r As Range
| With ActiveSheet
| Set r = .Range("A1,C1")
| If Application.Sum(r) < 1 Then _
| MsgBox "Numbers do not equal 1"
| End With
| End Sub
|
| --
| XL2002
| Regards
|
| William
|
|

|
| "someguy" wrote in message
| 7.142...
| | Hi,
| |
| | This seems like it should be a fairly simple task but i'm new
to
vba
| | and can't seem to figure it out. I have 2 cells in excel whose

values
| have
| | to add up to 1.0. If the user changes the values in any cell and
their
| | total value is not equal to one, a popup window should notify the
user.
| | I've written the fuction in VB but how do i call it when the user
| changes
| | the cell values? Many thanks.
| |
| | TIA
|
|
|
|






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default simple question

"William" wrote in
:

Thanks for all your help guys!...it works....and it seems there are many
ways it can me done :)

Hi

Place the following in the ThisWorkbook module. I'm assuming the 2
cells are A1 and C1.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim r As Range
With ActiveSheet
Set r = .Range("A1,C1")
If Application.Sum(r) < 1 Then _
MsgBox "Numbers do not equal 1"
End With
End Sub


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 339
Default simple question


"someguy" wrote in message
7.142...
Hi,

This seems like it should be a fairly simple task but i'm new to vba
and can't seem to figure it out. I have 2 cells in excel whose values

have
to add up to 1.0. If the user changes the values in any cell and their
total value is not equal to one, a popup window should notify the user.
I've written the fuction in VB but how do i call it when the user changes
the cell values? Many thanks.

TIA


Are you 100% sure you want to use a popup window? Why not use something like
=IF(1=A1+A2, "OK", "SUM < 1")
The problem with a popup is that you potentially could get lots of windows
poping up by just changing the value in one cell
/Fredrik




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF formula-simple question; simple operator Rich D Excel Discussion (Misc queries) 4 December 6th 07 03:36 PM
Simple Simple Excel usage question BookerW Excel Discussion (Misc queries) 1 June 23rd 05 10:06 PM
simple VBA question abxy[_55_] Excel Programming 2 April 16th 04 12:35 AM
simple question, hopefully a simple answer! Matt B Excel Programming 5 January 13th 04 08:43 PM
Simple Question JS[_4_] Excel Programming 1 July 10th 03 06:19 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"