Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default General question about using "worksheet" when select from the VBA Editor?

question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
..selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default General question about using "worksheet" when select from the VBA Editor?

Hi Susan,

See Chip Pearsons notes on Event procedures at:

http://www.cpearson.com/excel/events.htm

---
Regards,
Norman



"Susan Hayes" wrote in message
...
question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
.selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default General question about using "worksheet" when select from the VBA Editor?

Susan,

I am not really sure what you are asking, but a few thoughts.

Worksheet_SelectionChange is triggered when a cell is selected. If you want
to catch when a cell is changed, then you need the Worksheet_Change event.

In itself this has nothing to do with IF/Else, that just becomes a control
mechanism within the code, albeit there are others. Often you only want to
test for changes within a certain area of the worksheet and you can use the
intersect method for this

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then

which is only true if the changed cell is in the range A1:H10.

If you want to test for it has been changed to a particular value, you can
use If/ElseIf/Else/Endif

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
If .Value =2 Then
.Value = 100
ElseIf .Value = 3 Then
.Value = 1000
Else
.Value = "Error"
End If
End With
End If

which should be quite simple to follow. But you can also use Case statements

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
Select Case .Value
Case 2: .Value = 100
Case 3: .Value = 1000
Case Else: .Value = "Error"
End Select
End With
End If

which is more elegant IMO.

Another thing to be wary of is stopping your changes re-firing the event.
This is done by adding
Application.EnableEvents = False
at the start, and re-set at the end.

Putting all this together, here is a simple example that checks a range for
a value (planetary in this case, and sets the cell colour accordingly)

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
On Error GoTo ws_exit
If Not Intersect(Target, Range("A3:H9")) Is Nothing Then
With Target
Select Case LCase(Target.Value)
Case "mars": .Interior.ColorIndex = 3 'Red
Case "moon": .Interior.ColorIndex = 5 'Blue
Case "sun": .Interior.ColorIndex = 6 'Yellow
Case "saturn": .Interior.ColorIndex = 10 'Green
Case "venus": .Interior.ColorIndex = 45 'orange
End Select
End With
End If


ws_exit:
Application.EnableEvents = True

End Sub
--

HTH

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


"Susan Hayes" wrote in message
...
question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
.selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default General question about using "worksheet" when select from the

dear bob,

i read you informative reply to susain's query about tracking changes in an
execl file.

Actually i am working on a project where i have to maintain audit trail on a
web-based excel . Do you think that this intersect method will be helpful in
web based project's environment ?

i'll be greatfull if you can take out some time and reply to this query .
others are also equally welcome to give their opinions so that i can proceed
with my project.

thanks in advance
with kind regards

anugrah atreya
Softsolvers Technologies


"Bob Phillips" wrote:

Susan,

I am not really sure what you are asking, but a few thoughts.

Worksheet_SelectionChange is triggered when a cell is selected. If you want
to catch when a cell is changed, then you need the Worksheet_Change event.

In itself this has nothing to do with IF/Else, that just becomes a control
mechanism within the code, albeit there are others. Often you only want to
test for changes within a certain area of the worksheet and you can use the
intersect method for this

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then

which is only true if the changed cell is in the range A1:H10.

If you want to test for it has been changed to a particular value, you can
use If/ElseIf/Else/Endif

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
If .Value =2 Then
.Value = 100
ElseIf .Value = 3 Then
.Value = 1000
Else
.Value = "Error"
End If
End With
End If

which should be quite simple to follow. But you can also use Case statements

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
Select Case .Value
Case 2: .Value = 100
Case 3: .Value = 1000
Case Else: .Value = "Error"
End Select
End With
End If

which is more elegant IMO.

Another thing to be wary of is stopping your changes re-firing the event.
This is done by adding
Application.EnableEvents = False
at the start, and re-set at the end.

Putting all this together, here is a simple example that checks a range for
a value (planetary in this case, and sets the cell colour accordingly)

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
On Error GoTo ws_exit
If Not Intersect(Target, Range("A3:H9")) Is Nothing Then
With Target
Select Case LCase(Target.Value)
Case "mars": .Interior.ColorIndex = 3 'Red
Case "moon": .Interior.ColorIndex = 5 'Blue
Case "sun": .Interior.ColorIndex = 6 'Yellow
Case "saturn": .Interior.ColorIndex = 10 'Green
Case "venus": .Interior.ColorIndex = 45 'orange
End Select
End With
End If


ws_exit:
Application.EnableEvents = True

End Sub
--

HTH

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


"Susan Hayes" wrote in message
...
question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
.selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default General question about using "worksheet" when select from the

dear bob,

i read you informative reply to susain's query about tracking changes in an
execl file.

Actually i am working on a project where i have to maintain audit trail on a
web-based excel . Do you think that this intersect method will be helpful in
web based project's environment ?

i'll be greatfull if you can take out some time and reply to this query .
others are also equally welcome to give their opinions so that i can proceed
with my project.

thanks in advance
with kind regards

anugrah atreya
Softsolvers Technologies


"Bob Phillips" wrote:

Susan,

I am not really sure what you are asking, but a few thoughts.

Worksheet_SelectionChange is triggered when a cell is selected. If you want
to catch when a cell is changed, then you need the Worksheet_Change event.

In itself this has nothing to do with IF/Else, that just becomes a control
mechanism within the code, albeit there are others. Often you only want to
test for changes within a certain area of the worksheet and you can use the
intersect method for this

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then

which is only true if the changed cell is in the range A1:H10.

If you want to test for it has been changed to a particular value, you can
use If/ElseIf/Else/Endif

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
If .Value =2 Then
.Value = 100
ElseIf .Value = 3 Then
.Value = 1000
Else
.Value = "Error"
End If
End With
End If

which should be quite simple to follow. But you can also use Case statements

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
Select Case .Value
Case 2: .Value = 100
Case 3: .Value = 1000
Case Else: .Value = "Error"
End Select
End With
End If

which is more elegant IMO.

Another thing to be wary of is stopping your changes re-firing the event.
This is done by adding
Application.EnableEvents = False
at the start, and re-set at the end.

Putting all this together, here is a simple example that checks a range for
a value (planetary in this case, and sets the cell colour accordingly)

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
On Error GoTo ws_exit
If Not Intersect(Target, Range("A3:H9")) Is Nothing Then
With Target
Select Case LCase(Target.Value)
Case "mars": .Interior.ColorIndex = 3 'Red
Case "moon": .Interior.ColorIndex = 5 'Blue
Case "sun": .Interior.ColorIndex = 6 'Yellow
Case "saturn": .Interior.ColorIndex = 10 'Green
Case "venus": .Interior.ColorIndex = 45 'orange
End Select
End With
End If


ws_exit:
Application.EnableEvents = True

End Sub
--

HTH

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


"Susan Hayes" wrote in message
...
question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
.selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default General question about using "worksheet" when select from the

dear bob,

i read you informative reply to susain's query about tracking changes in an
execl file.

Actually i am working on a project where i have to maintain audit trail on a
web-based excel . Do you think that this intersect method will be helpful in
web based project's environment ?

i'll be greatfull if you can take out some time and reply to this query .
others are also equally welcome to give their opinions so that i can proceed
with my project.

thanks in advance
with kind regards

anugrah atreya
Softsolvers Technologies
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default General question about using "worksheet" when select from the

dear bob ,

i read your informative articale.
can you please advice that if i can use this same approach to generate audit
trail of changes made to any workbook in web-based excel environment.

thanks in advance.
with kind regards
anugrah atreya

"Bob Phillips" wrote:

Susan,

I am not really sure what you are asking, but a few thoughts.

Worksheet_SelectionChange is triggered when a cell is selected. If you want
to catch when a cell is changed, then you need the Worksheet_Change event.

In itself this has nothing to do with IF/Else, that just becomes a control
mechanism within the code, albeit there are others. Often you only want to
test for changes within a certain area of the worksheet and you can use the
intersect method for this

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then

which is only true if the changed cell is in the range A1:H10.

If you want to test for it has been changed to a particular value, you can
use If/ElseIf/Else/Endif

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
If .Value =2 Then
.Value = 100
ElseIf .Value = 3 Then
.Value = 1000
Else
.Value = "Error"
End If
End With
End If

which should be quite simple to follow. But you can also use Case statements

If Not Intersect(Target, Me.Range("A1:H10")) Is Nothing Then
With Target
Select Case .Value
Case 2: .Value = 100
Case 3: .Value = 1000
Case Else: .Value = "Error"
End Select
End With
End If

which is more elegant IMO.

Another thing to be wary of is stopping your changes re-firing the event.
This is done by adding
Application.EnableEvents = False
at the start, and re-set at the end.

Putting all this together, here is a simple example that checks a range for
a value (planetary in this case, and sets the cell colour accordingly)

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
On Error GoTo ws_exit
If Not Intersect(Target, Range("A3:H9")) Is Nothing Then
With Target
Select Case LCase(Target.Value)
Case "mars": .Interior.ColorIndex = 3 'Red
Case "moon": .Interior.ColorIndex = 5 'Blue
Case "sun": .Interior.ColorIndex = 6 'Yellow
Case "saturn": .Interior.ColorIndex = 10 'Green
Case "venus": .Interior.ColorIndex = 45 'orange
End Select
End With
End If


ws_exit:
Application.EnableEvents = True

End Sub
--

HTH

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


"Susan Hayes" wrote in message
...
question about Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
This probably seems basic to you, but Im trying to create a
spreadsheet that would need to be updated as the user
changes/manipulates the worksheet. The only way I see to change
things as the user manipulates is to use the if else statement. Is
there a more general statement that can be used. I have tried
.selection. The for, while, case select dont work as I intended

When something is changed/moved/manipulated on that sheet does what
you have coded run from the very top all the time?

This is difficult to explain, but any advice is appreciated

Thanks




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
Format changes from "General" to "Text" GARY Excel Discussion (Misc queries) 2 March 1st 09 10:18 AM
how do i reset the default from "general" to "number" Fog Excel Discussion (Misc queries) 5 February 4th 08 09:34 PM
"general" cells turned into "accounting" - why and how to reverse Kolhoz Excel Worksheet Functions 1 January 17th 07 06:20 AM
"Control" plus "click" doesn't allow me to select multiple cells Ken Cooke New Users to Excel 0 September 25th 06 04:46 PM
Using "Cells" to write "Range("A:A,H:H").Select" Trip Ives[_2_] Excel Programming 3 June 5th 04 03:13 PM


All times are GMT +1. The time now is 06:21 AM.

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

About Us

"It's about Microsoft Excel"