Renaming Sheets in Macro without renaming first four sheets












0















I know how to make marcos but during school he never taught me everything to do with them, mainly with the Dim.
My questions is how to I make a marco that will rename all my sheets expect for the first four.



Sub RenameSheet()

Dim rs As Worksheet

For Each rs In Sheets
rs.Name = rs.Range("D5")
Next rs

End Sub


Works for every sheet but I dont want to be renaming every sheet. My first four are Documentation, Summarry, RONATemplate, KaycanTemplate. Which I want to leave has is. I cant really just put those names in cell D5 to make it work where its a template, and it will mess up my other marcos.










share|improve this question





























    0















    I know how to make marcos but during school he never taught me everything to do with them, mainly with the Dim.
    My questions is how to I make a marco that will rename all my sheets expect for the first four.



    Sub RenameSheet()

    Dim rs As Worksheet

    For Each rs In Sheets
    rs.Name = rs.Range("D5")
    Next rs

    End Sub


    Works for every sheet but I dont want to be renaming every sheet. My first four are Documentation, Summarry, RONATemplate, KaycanTemplate. Which I want to leave has is. I cant really just put those names in cell D5 to make it work where its a template, and it will mess up my other marcos.










    share|improve this question



























      0












      0








      0








      I know how to make marcos but during school he never taught me everything to do with them, mainly with the Dim.
      My questions is how to I make a marco that will rename all my sheets expect for the first four.



      Sub RenameSheet()

      Dim rs As Worksheet

      For Each rs In Sheets
      rs.Name = rs.Range("D5")
      Next rs

      End Sub


      Works for every sheet but I dont want to be renaming every sheet. My first four are Documentation, Summarry, RONATemplate, KaycanTemplate. Which I want to leave has is. I cant really just put those names in cell D5 to make it work where its a template, and it will mess up my other marcos.










      share|improve this question
















      I know how to make marcos but during school he never taught me everything to do with them, mainly with the Dim.
      My questions is how to I make a marco that will rename all my sheets expect for the first four.



      Sub RenameSheet()

      Dim rs As Worksheet

      For Each rs In Sheets
      rs.Name = rs.Range("D5")
      Next rs

      End Sub


      Works for every sheet but I dont want to be renaming every sheet. My first four are Documentation, Summarry, RONATemplate, KaycanTemplate. Which I want to leave has is. I cant really just put those names in cell D5 to make it work where its a template, and it will mess up my other marcos.







      excel excel-vba renaming vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 9 '18 at 18:41









      Community

      11




      11










      asked May 25 '13 at 13:38









      user2420382user2420382

      111




      111
























          3 Answers
          3






          active

          oldest

          votes


















          0














          To change every sheet after the 4th use the index property of sheets in an if statement:



          Sub RenameSheet()

          Dim rs As Worksheet

          For Each rs In Sheets
          If rs.Index > 4 Then
          rs.Name = rs.Range("D5")
          End If
          Next rs

          End Sub





          share|improve this answer































            0














            You could create kind of a blacklist with and check if rs.Name is one of the names you do not want to change or you could access the sheets by index.



            i.e.



            For i = 5 to Worksheets.Count
            Worksheets(i).Name = rs.Range("D5")
            Next





            share|improve this answer

































              0














              First option is to use different kind of loop which iterates based on sheet index/number. Here is the code running only for Worksheets Collection:



              Sub RenameSheet()

              Dim rs As Long

              For rs = 5 To Worksheets.Count
              Worksheets(rs).Name = Worksheets(rs).Range("D5")
              Next rs

              End Sub


              Your loop starts as of 5th of worksheet and runs until the last one.



              The other option is to exclude all sheets with names which you have mentioned in your question. In that situation you could run this macro:



              Sub RenameSheet()

              Dim rs As Worksheet

              For Each rs In Sheets
              if rs.name <> "Summary" And rs.Name <> "RONATemplate" and rs.Name <> "KeycanTemplate" Then
              rs.Name = rs.Range("D5")
              end if
              Next rs

              End Sub


              However, keep in mind that all conditional checks like rs.Name <> "Summary" are case sensitive therefore you need to put appropriate names within code including upper- and lower- cases. Or you could use UCase function to compare to capitalized names like:



              if UCase(rs.Name) <> "SUMMARY" And UCase(rs.Name) <> "RONATEMPLATE" And Ucase(rs.Name) <> "KEYCANTEMPLATE" Then


              I would suggest to use second type of improved procedure. If you change the order of your sheets (e.g. move first sheet into 6th position) you will get unexpected results running first For i=1 loop. There is not such problem running second type of loop/subroutine.






              share|improve this answer


























              • For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                – Doug Glancy
                May 25 '13 at 13:55











              • Right, thanks :)

                – Kazimierz Jawor
                May 25 '13 at 13:58






              • 1





                The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                – Doug Glancy
                May 25 '13 at 14:04













              • that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                – Kazimierz Jawor
                May 25 '13 at 14:18













              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              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%2fstackoverflow.com%2fquestions%2f16750124%2frenaming-sheets-in-macro-without-renaming-first-four-sheets%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              To change every sheet after the 4th use the index property of sheets in an if statement:



              Sub RenameSheet()

              Dim rs As Worksheet

              For Each rs In Sheets
              If rs.Index > 4 Then
              rs.Name = rs.Range("D5")
              End If
              Next rs

              End Sub





              share|improve this answer




























                0














                To change every sheet after the 4th use the index property of sheets in an if statement:



                Sub RenameSheet()

                Dim rs As Worksheet

                For Each rs In Sheets
                If rs.Index > 4 Then
                rs.Name = rs.Range("D5")
                End If
                Next rs

                End Sub





                share|improve this answer


























                  0












                  0








                  0







                  To change every sheet after the 4th use the index property of sheets in an if statement:



                  Sub RenameSheet()

                  Dim rs As Worksheet

                  For Each rs In Sheets
                  If rs.Index > 4 Then
                  rs.Name = rs.Range("D5")
                  End If
                  Next rs

                  End Sub





                  share|improve this answer













                  To change every sheet after the 4th use the index property of sheets in an if statement:



                  Sub RenameSheet()

                  Dim rs As Worksheet

                  For Each rs In Sheets
                  If rs.Index > 4 Then
                  rs.Name = rs.Range("D5")
                  End If
                  Next rs

                  End Sub






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 25 '13 at 13:57









                  Ryan ERyan E

                  4291516




                  4291516

























                      0














                      You could create kind of a blacklist with and check if rs.Name is one of the names you do not want to change or you could access the sheets by index.



                      i.e.



                      For i = 5 to Worksheets.Count
                      Worksheets(i).Name = rs.Range("D5")
                      Next





                      share|improve this answer






























                        0














                        You could create kind of a blacklist with and check if rs.Name is one of the names you do not want to change or you could access the sheets by index.



                        i.e.



                        For i = 5 to Worksheets.Count
                        Worksheets(i).Name = rs.Range("D5")
                        Next





                        share|improve this answer




























                          0












                          0








                          0







                          You could create kind of a blacklist with and check if rs.Name is one of the names you do not want to change or you could access the sheets by index.



                          i.e.



                          For i = 5 to Worksheets.Count
                          Worksheets(i).Name = rs.Range("D5")
                          Next





                          share|improve this answer















                          You could create kind of a blacklist with and check if rs.Name is one of the names you do not want to change or you could access the sheets by index.



                          i.e.



                          For i = 5 to Worksheets.Count
                          Worksheets(i).Name = rs.Range("D5")
                          Next






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 25 '13 at 14:08









                          FallenAngel

                          11.2k86396




                          11.2k86396










                          answered May 25 '13 at 13:52









                          user2420390user2420390

                          1




                          1























                              0














                              First option is to use different kind of loop which iterates based on sheet index/number. Here is the code running only for Worksheets Collection:



                              Sub RenameSheet()

                              Dim rs As Long

                              For rs = 5 To Worksheets.Count
                              Worksheets(rs).Name = Worksheets(rs).Range("D5")
                              Next rs

                              End Sub


                              Your loop starts as of 5th of worksheet and runs until the last one.



                              The other option is to exclude all sheets with names which you have mentioned in your question. In that situation you could run this macro:



                              Sub RenameSheet()

                              Dim rs As Worksheet

                              For Each rs In Sheets
                              if rs.name <> "Summary" And rs.Name <> "RONATemplate" and rs.Name <> "KeycanTemplate" Then
                              rs.Name = rs.Range("D5")
                              end if
                              Next rs

                              End Sub


                              However, keep in mind that all conditional checks like rs.Name <> "Summary" are case sensitive therefore you need to put appropriate names within code including upper- and lower- cases. Or you could use UCase function to compare to capitalized names like:



                              if UCase(rs.Name) <> "SUMMARY" And UCase(rs.Name) <> "RONATEMPLATE" And Ucase(rs.Name) <> "KEYCANTEMPLATE" Then


                              I would suggest to use second type of improved procedure. If you change the order of your sheets (e.g. move first sheet into 6th position) you will get unexpected results running first For i=1 loop. There is not such problem running second type of loop/subroutine.






                              share|improve this answer


























                              • For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                                – Doug Glancy
                                May 25 '13 at 13:55











                              • Right, thanks :)

                                – Kazimierz Jawor
                                May 25 '13 at 13:58






                              • 1





                                The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                                – Doug Glancy
                                May 25 '13 at 14:04













                              • that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                                – Kazimierz Jawor
                                May 25 '13 at 14:18


















                              0














                              First option is to use different kind of loop which iterates based on sheet index/number. Here is the code running only for Worksheets Collection:



                              Sub RenameSheet()

                              Dim rs As Long

                              For rs = 5 To Worksheets.Count
                              Worksheets(rs).Name = Worksheets(rs).Range("D5")
                              Next rs

                              End Sub


                              Your loop starts as of 5th of worksheet and runs until the last one.



                              The other option is to exclude all sheets with names which you have mentioned in your question. In that situation you could run this macro:



                              Sub RenameSheet()

                              Dim rs As Worksheet

                              For Each rs In Sheets
                              if rs.name <> "Summary" And rs.Name <> "RONATemplate" and rs.Name <> "KeycanTemplate" Then
                              rs.Name = rs.Range("D5")
                              end if
                              Next rs

                              End Sub


                              However, keep in mind that all conditional checks like rs.Name <> "Summary" are case sensitive therefore you need to put appropriate names within code including upper- and lower- cases. Or you could use UCase function to compare to capitalized names like:



                              if UCase(rs.Name) <> "SUMMARY" And UCase(rs.Name) <> "RONATEMPLATE" And Ucase(rs.Name) <> "KEYCANTEMPLATE" Then


                              I would suggest to use second type of improved procedure. If you change the order of your sheets (e.g. move first sheet into 6th position) you will get unexpected results running first For i=1 loop. There is not such problem running second type of loop/subroutine.






                              share|improve this answer


























                              • For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                                – Doug Glancy
                                May 25 '13 at 13:55











                              • Right, thanks :)

                                – Kazimierz Jawor
                                May 25 '13 at 13:58






                              • 1





                                The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                                – Doug Glancy
                                May 25 '13 at 14:04













                              • that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                                – Kazimierz Jawor
                                May 25 '13 at 14:18
















                              0












                              0








                              0







                              First option is to use different kind of loop which iterates based on sheet index/number. Here is the code running only for Worksheets Collection:



                              Sub RenameSheet()

                              Dim rs As Long

                              For rs = 5 To Worksheets.Count
                              Worksheets(rs).Name = Worksheets(rs).Range("D5")
                              Next rs

                              End Sub


                              Your loop starts as of 5th of worksheet and runs until the last one.



                              The other option is to exclude all sheets with names which you have mentioned in your question. In that situation you could run this macro:



                              Sub RenameSheet()

                              Dim rs As Worksheet

                              For Each rs In Sheets
                              if rs.name <> "Summary" And rs.Name <> "RONATemplate" and rs.Name <> "KeycanTemplate" Then
                              rs.Name = rs.Range("D5")
                              end if
                              Next rs

                              End Sub


                              However, keep in mind that all conditional checks like rs.Name <> "Summary" are case sensitive therefore you need to put appropriate names within code including upper- and lower- cases. Or you could use UCase function to compare to capitalized names like:



                              if UCase(rs.Name) <> "SUMMARY" And UCase(rs.Name) <> "RONATEMPLATE" And Ucase(rs.Name) <> "KEYCANTEMPLATE" Then


                              I would suggest to use second type of improved procedure. If you change the order of your sheets (e.g. move first sheet into 6th position) you will get unexpected results running first For i=1 loop. There is not such problem running second type of loop/subroutine.






                              share|improve this answer















                              First option is to use different kind of loop which iterates based on sheet index/number. Here is the code running only for Worksheets Collection:



                              Sub RenameSheet()

                              Dim rs As Long

                              For rs = 5 To Worksheets.Count
                              Worksheets(rs).Name = Worksheets(rs).Range("D5")
                              Next rs

                              End Sub


                              Your loop starts as of 5th of worksheet and runs until the last one.



                              The other option is to exclude all sheets with names which you have mentioned in your question. In that situation you could run this macro:



                              Sub RenameSheet()

                              Dim rs As Worksheet

                              For Each rs In Sheets
                              if rs.name <> "Summary" And rs.Name <> "RONATemplate" and rs.Name <> "KeycanTemplate" Then
                              rs.Name = rs.Range("D5")
                              end if
                              Next rs

                              End Sub


                              However, keep in mind that all conditional checks like rs.Name <> "Summary" are case sensitive therefore you need to put appropriate names within code including upper- and lower- cases. Or you could use UCase function to compare to capitalized names like:



                              if UCase(rs.Name) <> "SUMMARY" And UCase(rs.Name) <> "RONATEMPLATE" And Ucase(rs.Name) <> "KEYCANTEMPLATE" Then


                              I would suggest to use second type of improved procedure. If you change the order of your sheets (e.g. move first sheet into 6th position) you will get unexpected results running first For i=1 loop. There is not such problem running second type of loop/subroutine.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 25 '13 at 15:31

























                              answered May 25 '13 at 13:47









                              Kazimierz JaworKazimierz Jawor

                              17k62446




                              17k62446













                              • For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                                – Doug Glancy
                                May 25 '13 at 13:55











                              • Right, thanks :)

                                – Kazimierz Jawor
                                May 25 '13 at 13:58






                              • 1





                                The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                                – Doug Glancy
                                May 25 '13 at 14:04













                              • that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                                – Kazimierz Jawor
                                May 25 '13 at 14:18





















                              • For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                                – Doug Glancy
                                May 25 '13 at 13:55











                              • Right, thanks :)

                                – Kazimierz Jawor
                                May 25 '13 at 13:58






                              • 1





                                The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                                – Doug Glancy
                                May 25 '13 at 14:04













                              • that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                                – Kazimierz Jawor
                                May 25 '13 at 14:18



















                              For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                              – Doug Glancy
                              May 25 '13 at 13:55





                              For rs = 5 In Sheets.Count should be For rs = 5 to Sheets.Count of course.

                              – Doug Glancy
                              May 25 '13 at 13:55













                              Right, thanks :)

                              – Kazimierz Jawor
                              May 25 '13 at 13:58





                              Right, thanks :)

                              – Kazimierz Jawor
                              May 25 '13 at 13:58




                              1




                              1





                              The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                              – Doug Glancy
                              May 25 '13 at 14:04







                              The other thing, I'd point out is if there are any chart Sheets you'll get a 438 runtime error on the first one as chart sheets have no range object.

                              – Doug Glancy
                              May 25 '13 at 14:04















                              that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                              – Kazimierz Jawor
                              May 25 '13 at 14:18







                              that's obviously right therefore 2nd idea is much better also considering other problems (like sheet order reorganisation).

                              – Kazimierz Jawor
                              May 25 '13 at 14:18




















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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%2fstackoverflow.com%2fquestions%2f16750124%2frenaming-sheets-in-macro-without-renaming-first-four-sheets%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              TypeError: fit_transform() missing 1 required positional argument: 'X'