+ Reply to Thread
Results 1 to 6 of 6

Make Codename macro shorter & possibly more efficient

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    188

    Make Codename macro shorter & possibly more efficient

    This code runs fine. However it is long and repetitive.
    I'm believing there's a way to loop something like this to make it shorter but I don't know how just yet. How do I set

    Sub renameLogTabs()
    
        Sheet2.Name = Range("B2").Value
        Sheet3.Name = Range("B3").Value
        Sheet4.Name = Range("B4").Value
        Sheet5.Name = Range("B5").Value
        Sheet6.Name = Range("B6").Value
        Sheet7.Name = Range("B7").Value
        Sheet8.Name = Range("B8").Value
        Sheet9.Name = Range("B9").Value
        Sheet10.Name = Range("B10").Value
        Sheet11.Name = Range("B11").Value
        Sheet12.Name = Range("B12").Value
        Sheet13.Name = Range("B13").Value
        Sheet14.Name = Range("B14").Value
        Sheet15.Name = Range("B15").Value
        Sheet16.Name = Range("B16").Value
        Sheet17.Name = Range("B17").Value
        Sheet18.Name = Range("B18").Value
        Sheet19.Name = Range("B19").Value
        Sheet20.Name = Range("B20").Value
        Sheet21.Name = Range("B21").Value
        Sheet22.Name = Range("B22").Value
        Sheet23.Name = Range("B23").Value
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,699

    Re: Make Codename macro shorter & possibly more efficient

    Sub Maybe()
    Dim i As Long
    With ActiveSheet
        For i = 2 To 23
            Sheets("Sheet" & i).Name = .Cells(i, 2).Value
        Next i
    End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    274

    Re: Make Codename macro shorter & possibly more efficient

    Run the macro in this test file. It doesn't care what the sheets are currently named or how many there are. It just renames all the sheets except the first one to the values you have starting in B2.
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,788

    Re: Make Codename macro shorter & possibly more efficient

    How about
    Sub Dbroek()
       Dim Ws As Worksheet
       Dim i As Long
       
       For Each Ws In Worksheets
          If InStr(1, Ws.CodeName, "Sheet") > 0 Then
             i = Replace(Ws.CodeName, "Sheet", "")
             Select Case i
                Case 2 To 23
                   Ws.Name = Range("B" & i).Value
             End Select
          End If
       Next Ws
    End Sub

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Make Codename macro shorter & possibly more efficient

    Sub test()
        Dim i As Long
        With ThisWorkbook
            For i = 2 To 23
                .Sheets(.Sheets(.VBProject.VBComponents("sheet" & i).Properties("name").Value).Name).Name = Cells(i, 2)
            Next
        End With
    End Sub
    P.S
    You need to;
    Go to
    File --> Options --> Trust Center --> Trust Center Settings --> Macro Settings.
    Check Trust Access to the VBA project object model.
    Last edited by jindon; 08-14-2021 at 11:12 AM.

  6. #6
    Forum Contributor
    Join Date
    05-25-2019
    Location
    Sioux Falls, South Dakota
    MS-Off Ver
    Microsoft 365
    Posts
    188

    Re: Make Codename macro shorter & possibly more efficient

    Gurus, thank you for your generous offers. Unfortunately, I've come to learn that the code I've originally written does not work for what I intended after all. So, therefore none of you offers will have any impact on my problem. And, I am so sorry for posting my question before learning of this problem.

    I will post the file for your review for you to see why but I fully understand if you decide to move on and not attempt any further suggestions.
    Anyways, my original code does not work because it becomes corrupt when try to sort cells A1:E17 in ABC order or if I clear any of the values in cell B2:B17. The B-column cells are what I'm using to try and rename the worksheets in a reliable manner. I might still come up with an alternative option... but so far, am awfully stymied.

    Again, I am gracious for your generosity. Thank you much for sharing. This is very educational for me.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Make VBA Macro more efficient
    By boubou114 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2015, 10:32 AM
  2. Replies: 2
    Last Post: 08-12-2014, 12:56 PM
  3. [SOLVED] Make a macro more efficient/faster
    By jhonneyboy in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-25-2014, 05:38 PM
  4. Make Copy/Paste Values Macro more efficient when calculating
    By Phily915 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2014, 04:28 PM
  5. Macro takes 20 seconds to run, can anyone please help to make it more efficient?
    By Adrian1981 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2013, 11:24 PM
  6. Macro takes 20 seconds to run, can anyone please help to make it more efficient?
    By Adrian1981 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2013, 06:44 PM
  7. How to make systematic repetitive macro shorter?
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-05-2011, 01:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1