Sort Sheets Alphabetically VBA





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm struggling on how will I sort the sheets after combining all workbooks into 1.



here's my code:



Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim mypath As String
Dim strFilename As String
Dim saveFolder As String

Dim i%, j%

Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False

mypath = "C:Acct. Docs" 'Change to suit
Set wbDst = Workbooks.Add(xlWBATWorksheet)
strFilename = Dir(mypath & "*.xlsx", vbNormal)
Dim fname As String

saveFolder = "C:FORMATTED"

If Len(strFilename) = 0 Then Exit Sub

Do Until strFilename = ""
Set wbSrc = Workbooks.Open(Filename:=mypath & "" & strFilename)
Set wsSrc = wbSrc.Worksheets(1)
wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
wbSrc.Close False

For i = 1 To wbDst.Worksheets.Count - 1
For j = 1 To wbDst.Worksheets.Count
If wbDst.Worksheets(j).Name < wbDst.Worksheets(i + 1).Name Then
wbDst.Worksheets(j).Move Before:=Sheets(i + 1)
End If
Next j
Next i
strFilename = Dir()
Loop


wbDst.Worksheets(1).Delete
fname = Sheets("Sheet1").Range("C5").Text
ActiveWorkbook.SaveAs saveFolder & "" & fname & " Account Documentation.xlsx", FileFormat:=51
wbDst.Close

Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub



I want the sheets to be sorted alphabetically.
thanks in advance!!










share|improve this question





























    0















    I'm struggling on how will I sort the sheets after combining all workbooks into 1.



    here's my code:



    Dim wbDst As Workbook
    Dim wbSrc As Workbook
    Dim wsSrc As Worksheet
    Dim mypath As String
    Dim strFilename As String
    Dim saveFolder As String

    Dim i%, j%

    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.AskToUpdateLinks = False

    mypath = "C:Acct. Docs" 'Change to suit
    Set wbDst = Workbooks.Add(xlWBATWorksheet)
    strFilename = Dir(mypath & "*.xlsx", vbNormal)
    Dim fname As String

    saveFolder = "C:FORMATTED"

    If Len(strFilename) = 0 Then Exit Sub

    Do Until strFilename = ""
    Set wbSrc = Workbooks.Open(Filename:=mypath & "" & strFilename)
    Set wsSrc = wbSrc.Worksheets(1)
    wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
    wbSrc.Close False

    For i = 1 To wbDst.Worksheets.Count - 1
    For j = 1 To wbDst.Worksheets.Count
    If wbDst.Worksheets(j).Name < wbDst.Worksheets(i + 1).Name Then
    wbDst.Worksheets(j).Move Before:=Sheets(i + 1)
    End If
    Next j
    Next i
    strFilename = Dir()
    Loop


    wbDst.Worksheets(1).Delete
    fname = Sheets("Sheet1").Range("C5").Text
    ActiveWorkbook.SaveAs saveFolder & "" & fname & " Account Documentation.xlsx", FileFormat:=51
    wbDst.Close

    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True


    End Sub



    I want the sheets to be sorted alphabetically.
    thanks in advance!!










    share|improve this question

























      0












      0








      0








      I'm struggling on how will I sort the sheets after combining all workbooks into 1.



      here's my code:



      Dim wbDst As Workbook
      Dim wbSrc As Workbook
      Dim wsSrc As Worksheet
      Dim mypath As String
      Dim strFilename As String
      Dim saveFolder As String

      Dim i%, j%

      Application.DisplayAlerts = False
      Application.EnableEvents = False
      Application.ScreenUpdating = False
      Application.AskToUpdateLinks = False

      mypath = "C:Acct. Docs" 'Change to suit
      Set wbDst = Workbooks.Add(xlWBATWorksheet)
      strFilename = Dir(mypath & "*.xlsx", vbNormal)
      Dim fname As String

      saveFolder = "C:FORMATTED"

      If Len(strFilename) = 0 Then Exit Sub

      Do Until strFilename = ""
      Set wbSrc = Workbooks.Open(Filename:=mypath & "" & strFilename)
      Set wsSrc = wbSrc.Worksheets(1)
      wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
      wbSrc.Close False

      For i = 1 To wbDst.Worksheets.Count - 1
      For j = 1 To wbDst.Worksheets.Count
      If wbDst.Worksheets(j).Name < wbDst.Worksheets(i + 1).Name Then
      wbDst.Worksheets(j).Move Before:=Sheets(i + 1)
      End If
      Next j
      Next i
      strFilename = Dir()
      Loop


      wbDst.Worksheets(1).Delete
      fname = Sheets("Sheet1").Range("C5").Text
      ActiveWorkbook.SaveAs saveFolder & "" & fname & " Account Documentation.xlsx", FileFormat:=51
      wbDst.Close

      Application.DisplayAlerts = True
      Application.EnableEvents = True
      Application.ScreenUpdating = True


      End Sub



      I want the sheets to be sorted alphabetically.
      thanks in advance!!










      share|improve this question














      I'm struggling on how will I sort the sheets after combining all workbooks into 1.



      here's my code:



      Dim wbDst As Workbook
      Dim wbSrc As Workbook
      Dim wsSrc As Worksheet
      Dim mypath As String
      Dim strFilename As String
      Dim saveFolder As String

      Dim i%, j%

      Application.DisplayAlerts = False
      Application.EnableEvents = False
      Application.ScreenUpdating = False
      Application.AskToUpdateLinks = False

      mypath = "C:Acct. Docs" 'Change to suit
      Set wbDst = Workbooks.Add(xlWBATWorksheet)
      strFilename = Dir(mypath & "*.xlsx", vbNormal)
      Dim fname As String

      saveFolder = "C:FORMATTED"

      If Len(strFilename) = 0 Then Exit Sub

      Do Until strFilename = ""
      Set wbSrc = Workbooks.Open(Filename:=mypath & "" & strFilename)
      Set wsSrc = wbSrc.Worksheets(1)
      wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
      wbSrc.Close False

      For i = 1 To wbDst.Worksheets.Count - 1
      For j = 1 To wbDst.Worksheets.Count
      If wbDst.Worksheets(j).Name < wbDst.Worksheets(i + 1).Name Then
      wbDst.Worksheets(j).Move Before:=Sheets(i + 1)
      End If
      Next j
      Next i
      strFilename = Dir()
      Loop


      wbDst.Worksheets(1).Delete
      fname = Sheets("Sheet1").Range("C5").Text
      ActiveWorkbook.SaveAs saveFolder & "" & fname & " Account Documentation.xlsx", FileFormat:=51
      wbDst.Close

      Application.DisplayAlerts = True
      Application.EnableEvents = True
      Application.ScreenUpdating = True


      End Sub



      I want the sheets to be sorted alphabetically.
      thanks in advance!!







      microsoft-excel vba macros sorting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 12 at 2:21









      geegee

      304




      304






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Ohh! I already figured it out!! :D
          I just swap the conditions in Looping :D



           For i = 1 To wbDst.Worksheets.Count
          For j = 1 To wbDst.Worksheets.Count - 1
          If wbDst.Worksheets(i).Name < wbDst.Worksheets(j + 1).Name Then
          wbDst.Worksheets(i).Move Before:=Sheets(j + 1)
          End If
          Next j
          Next i





          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404668%2fsort-sheets-alphabetically-vba%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Ohh! I already figured it out!! :D
            I just swap the conditions in Looping :D



             For i = 1 To wbDst.Worksheets.Count
            For j = 1 To wbDst.Worksheets.Count - 1
            If wbDst.Worksheets(i).Name < wbDst.Worksheets(j + 1).Name Then
            wbDst.Worksheets(i).Move Before:=Sheets(j + 1)
            End If
            Next j
            Next i





            share|improve this answer




























              0














              Ohh! I already figured it out!! :D
              I just swap the conditions in Looping :D



               For i = 1 To wbDst.Worksheets.Count
              For j = 1 To wbDst.Worksheets.Count - 1
              If wbDst.Worksheets(i).Name < wbDst.Worksheets(j + 1).Name Then
              wbDst.Worksheets(i).Move Before:=Sheets(j + 1)
              End If
              Next j
              Next i





              share|improve this answer


























                0












                0








                0







                Ohh! I already figured it out!! :D
                I just swap the conditions in Looping :D



                 For i = 1 To wbDst.Worksheets.Count
                For j = 1 To wbDst.Worksheets.Count - 1
                If wbDst.Worksheets(i).Name < wbDst.Worksheets(j + 1).Name Then
                wbDst.Worksheets(i).Move Before:=Sheets(j + 1)
                End If
                Next j
                Next i





                share|improve this answer













                Ohh! I already figured it out!! :D
                I just swap the conditions in Looping :D



                 For i = 1 To wbDst.Worksheets.Count
                For j = 1 To wbDst.Worksheets.Count - 1
                If wbDst.Worksheets(i).Name < wbDst.Worksheets(j + 1).Name Then
                wbDst.Worksheets(i).Move Before:=Sheets(j + 1)
                End If
                Next j
                Next i






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 12 at 3:21









                geegee

                304




                304






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404668%2fsort-sheets-alphabetically-vba%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар