Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 19
Default Multiple If Statements or Macro's in Worksheet:View Code

Hello,

I am trying to sort two different areas in one worksheet. I am trying to
use the following code but it only works for the first area not the second.

Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3")
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3")
Application.EnableEvents = True

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 87
Default Multiple If Statements or Macro's in Worksheet:View Code

If the Target doesn't overlap both ranges, your code exits on the first
test.

You'll have to modify it something like this:

If Not Intersect(Range("h3:n500"), Target) Is Nothing Then
... Do the sort on the first range.
End if

If Not Intersect(Range("p3:t500"), Target) Is Nothing Then
... Do the sort on the second range.
End if

If they are mutually exclusive, you could change it to:

If Not Intersect(Range("h3:n500"), Target) Is Nothing Then
... First sort
Elseif Not Intersect(Range("p3:t500"),Target) Is Nothing Then
... Second sort
End if

Scott

Ben Dummar wrote:
Hello,

I am trying to sort two different areas in one worksheet. I am trying to
use the following code but it only works for the first area not the second.

Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3")
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3")
Application.EnableEvents = True

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,696
Default Multiple If Statements or Macro's in Worksheet:View Code

This may work better...

Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
End Sub

Also, if you still want to sort by the second range even if the first range
is missing, you'll want to create a second Sub to perform the second range
sort, as it currently is ending the entire sub once the first set is blank.

"Ben Dummar" wrote:

Hello,

I am trying to sort two different areas in one worksheet. I am trying to
use the following code but it only works for the first area not the second.


Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3")
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3")
Application.EnableEvents = True

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 19
Default Multiple If Statements or Macro's in Worksheet:View Code

Sean,

Thanks for the response. I tried the code but it stops the editor.

I think one thing I need to clarify is that I want both list's to operate
independantly of each other. If a name is added to one list it re-sorts that
list but doesn't affect the other list and vice-versa.

Ben

"Sean Timmons" wrote:

This may work better...

Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
End Sub

Also, if you still want to sort by the second range even if the first range
is missing, you'll want to create a second Sub to perform the second range
sort, as it currently is ending the entire sub once the first set is blank.

"Ben Dummar" wrote:

Hello,

I am trying to sort two different areas in one worksheet. I am trying to
use the following code but it only works for the first area not the second.


Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3")
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3")
Application.EnableEvents = True

End Sub

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 87
Default Multiple If Statements or Macro's in Worksheet:View Code

Then put each block in an:
If Not Intersect(...) Is Nothing Then
...
End if

Scott

Ben Dummar wrote:
Sean,

Thanks for the response. I tried the code but it stops the editor.

I think one thing I need to clarify is that I want both list's to operate
independantly of each other. If a name is added to one list it re-sorts that
list but doesn't affect the other list and vice-versa.

Ben

"Sean Timmons" wrote:

This may work better...

Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3"), Order1:=xlAscending,
Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom
Application.EnableEvents = True
End Sub

Also, if you still want to sort by the second range even if the first range
is missing, you'll want to create a second Sub to perform the second range
sort, as it currently is ending the entire sub once the first set is blank.

"Ben Dummar" wrote:

Hello,

I am trying to sort two different areas in one worksheet. I am trying to
use the following code but it only works for the first area not the second.


Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Range("h3:n500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("h3:n500").Sort Key1:=Range("h3")
Application.EnableEvents = True
If Intersect(Range("p3:t500"), Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("p3:t500").Sort Key1:=Range("p3")
Application.EnableEvents = True

End Sub


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
Macros - copying macros from one computer to another TT Excel Discussion (Misc queries) 18 December 14th 06 03:24 AM
copy and use vba code Giz Excel Discussion (Misc queries) 9 November 22nd 06 09:57 PM
macro or code to open multiple workbooks Duane Reynolds Excel Discussion (Misc queries) 1 March 14th 06 08:18 AM
How to: Multiple "if" statements? Hosh New Users to Excel 2 June 29th 05 02:20 PM
Make Change Case in Excel a format rather than formula Kevin Excel Worksheet Functions 1 March 18th 05 08:53 PM


All times are GMT +1. The time now is 01:39 PM.

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"