Avatar of jfrank85
jfrank85
 asked on

remove workbook from sort vba

hi,

I have a macro that sorts the worksheets within excel for me. I have it excluding any worksheet that starts with "filtered from". I also want to add into the code to exclude any workbook called "RUN". How can I include that in the below?


Private Sub Sort_Active_Book()
Dim I As Integer
Dim J As Integer
Dim iAnswer As VbMsgBoxResult
'
' Prompt the user as which direction they wish to
' sort the worksheets.
'
iAnswer = vbYes
'iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
  & "Clicking No will sort in Descending Order", _
  vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For I = 1 To Sheets.Count
    For J = 1 To Sheets.Count - 1
        
        ' Exclude certain sheets from Sorting
        If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 Then
        
            ' If the answer is Yes, then sort in ascending order.
            '
            If iAnswer = vbYes Then
                If UCase$(Sheets(J).Name) > UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If

            ' If the answer is No, then sort in descending order.
            '
            ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(J).Name) < UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If
            End If
        End If
    Next J
Next I
End Sub

Open in new window

Microsoft OfficeMicrosoft ExcelVB ScriptVBA

Avatar of undefined
Last Comment
jfrank85

8/22/2022 - Mon
Saurabh Singh Teotia

Change the line no-17 in your code which is this...

If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 Then

Open in new window


To this...

  If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 And InStr(1, LCase(Sheets(J).Name), "Run") = 0 Then

Open in new window


Saurabh...
Rgonzo1971

Hi,

pls try
Private Sub Sort_Active_Book()
Dim I As Integer
Dim J As Integer
Dim iAnswer As VbMsgBoxResult
'
' Prompt the user as which direction they wish to
' sort the worksheets.
'
iAnswer = vbYes
'iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
  & "Clicking No will sort in Descending Order", _
  vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For I = 1 To Sheets.Count
    For J = 1 To Sheets.Count - 1
        
        ' Exclude certain sheets from Sorting
        If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 Or _
            LCase(Sheets(J).Name) <> "run" Then
        
            ' If the answer is Yes, then sort in ascending order.
            '
            If iAnswer = vbYes Then
                If UCase$(Sheets(J).Name) > UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If

            ' If the answer is No, then sort in descending order.
            '
            ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(J).Name) < UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If
            End If
        End If
    Next J
Next I
End Sub

Open in new window

Regards
Saurabh Singh Teotia

And my bad since you have it lowercase it will be like this...

 If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 And InStr(1, LCase(Sheets(J).Name), "run") = 0 Then

Open in new window


or you can ignore the cases like this is even more efficient way...

  If InStr(1, Sheets(J).Name, "filtered from", vbTextCompare) = 0 And InStr(1, Sheets(J).Name, "Run", vbTextCompare) = 0 Then

Open in new window


Saurabh...
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rory Archibald

Since you said workbook:

Private Sub Sort_Active_Book()
Dim I As Integer
Dim J As Integer
Dim iAnswer As VbMsgBoxResult
'
' Prompt the user as which direction they wish to
' sort the worksheets.
'

If UCase$(Left$(Activeworkbook.name, 3)) = "RUN" then exit sub
iAnswer = vbYes
'iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
  & "Clicking No will sort in Descending Order", _
  vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For I = 1 To Sheets.Count
    For J = 1 To Sheets.Count - 1
        
        ' Exclude certain sheets from Sorting
        If InStr(1, LCase(Sheets(J).Name), "filtered from") = 0 Then
        
            ' If the answer is Yes, then sort in ascending order.
            '
            If iAnswer = vbYes Then
                If UCase$(Sheets(J).Name) > UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If

            ' If the answer is No, then sort in descending order.
            '
            ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(J).Name) < UCase$(Sheets(J + 1).Name) Then
                   Sheets(J).Move after:=Sheets(J + 1)
                End If
            End If
        End If
    Next J
Next I
End Sub

Open in new window

jfrank85

ASKER
Rgonzo1971, Saurabh Singh Teotia - tried your suggestions and didn't ignore those sheets from the sort.

Rory, sorry i meant to say worksheet: "RUN"

so I have three worksheets that I don't want included in the sort

"RUN"
"Filtered from Prev Year"
"Filtered from Current Year"
ASKER CERTIFIED SOLUTION
Saurabh Singh Teotia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jfrank85

ASKER
works great, thanks!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.