Ignore lines in a Macro

PMRetired2012

Board Regular
Joined
Aug 6, 2019
Messages
123
I think i have a problem but not sure. I am going to put a macro on here for someone to help me with. the problem is: When i am using F8 to step thur the macro and i am watching as i step thur the sheet and that sheet is named workers or Visitors the macro does just like it does on all the other ones. The first line of the macro as i have it written shows to ignoe the sheets Workers, Visitors. What happens it treats those two sheets just like the other sheets and that is: If im looking at one of the other sheets it will do the 2 part sort. A3:AM32 then it will do the second part of the sort A36:A39. then it goes to the bottom part of the macro and sorts the sheets if i am working on the sheets workers and visitors like i want it to do. So what im saying is that i want to macro to do when using the F8 step thru and the active sheet being Workers and Visitors is to not is to jump down and sort the sheet from the bottom part of the macro and to ignore those 2 sheets when my active sheet is one other than those two. I belive the main line of code messed up is the first one it wont ignore those

Sub SORTDATA()
'Sort Data on Seets PreK-Visitors


If ActiveWorkbook.ActiveSheet.Name <> "WORKERS" Or ActiveWorkbook.ActiveSheet.Name <> "VISITORS" Then
Range("A3:AM32").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A3:A32"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A3:AM32")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Second Sort Starts here

Range("A36:AM39").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A36:A39"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A36:AM39")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
If ActiveWorkbook.ActiveSheet.Name = "WORKERS" Or ActiveWorkbook.ActiveSheet.Name = "VISITORS" Then
Range("A3:AM39").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A3:A39"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A3:AM39")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
Range("A3").Select

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
it should be AND not OR.

Code:
[COLOR=#333333]If ActiveSheet.Name <> "WORKERS" [/COLOR][COLOR=#ff0000]And[/COLOR][COLOR=#333333] ActiveSheet.Name <> "VISITORS" Then[/COLOR]
 
Upvote 0
it should be AND not OR.

Code:
[COLOR=#333333]If ActiveSheet.Name <> "WORKERS" [/COLOR][COLOR=#ff0000]And[/COLOR][COLOR=#333333] ActiveSheet.Name <> "VISITORS" Then[/COLOR]

when i change to and it doesnt work at all. it wont highlight anyting when im working with F8. what i want it to do is : if workers or visitors isnt my active sheet that im looking at the fist part of the code works fine but if im looking at workers or visitors the first part of the sortation macro does the same thing on those sheets also and then goes thru the sortation again in the lower part of macro. It sorts the workers and visitors sheets twice is what im trying to say. also the way i think it should work is if im looking at active sheet workers or visitors the macro should skip the first part of code and jump to the second If Statement and do what it is suppost to do.
 
Upvote 0
Code:
[color=darkblue]Sub[/color] SORTDATA()
[color=green]'Sort Data on Seets PreK-Visitors[/color]
    
    [color=darkblue]If[/color] UCase(ActiveSheet.Name) <> "WORKERS" And UCase(ActiveSheet.Name) <> "VISITORS" [color=darkblue]Then[/color] [color=green]'Not "WORKERS" And Not "VISITORS"[/color]
        Range("A3:AM32").Select
        ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A3:A32"), _
                                        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        [color=darkblue]With[/color] ActiveSheet.Sort
            .SetRange Range("A3:AM32")
            .Header = xlGuess
            .MatchCase = [color=darkblue]False[/color]
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        [color=green]'Second Sort Starts here[/color]
        
        Range("A36:AM39").Select
        ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A36:A39"), _
                                        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        [color=darkblue]With[/color] ActiveSheet.Sort
            .SetRange Range("A36:AM39")
            .Header = xlGuess
            .MatchCase = [color=darkblue]False[/color]
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
    [color=darkblue]Else[/color] [color=green]'"WORKERS" or "VISITORS"[/color]
        Range("A3:AM39").Select
        ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A3:A39"), _
                                        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        [color=darkblue]With[/color] ActiveSheet.Sort
            .SetRange Range("A3:AM39")
            .Header = xlGuess
            .MatchCase = [color=darkblue]False[/color]
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    Range("A3").Select
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,224,759
Messages
6,180,814
Members
452,996
Latest member
nelsonsix66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top