Checking worksheet names and renaming if duplicate found

  • Hi everyone! I do not have much experience in excel and would appreciated any help with my situation. My problem in excel right now is checking all the worksheet names and if one exists, then rename the current worksheet to WorksheetName1, with the next one being WorksheetName2, WorksheetName3 and so on.

    For example, right now, I name my worksheets as "Imported" using the following:

    Code
    Range("O1").Select
        Selection.Formula = "Imported"
        Selection.Copy
        Selection.PasteSpecial Paste:=xlValues
        Application.CutCopyMode = False
        Selection.Columns.AutoFit
        ActiveSheet.Name = Range("O1").Value
        Range("O1").Value = ""



    But I want to modify the above so it checks worksheet names already in the excel workbook and if another worksheet is already named "Imported", then the current worksheet will be renamed "Imported1".

    Also, my codes following that also functions based on the worksheet names. For example, in another Sub, I add another worksheet named "Graphs" and have it behind "Imported". But how do I make sure the data used to make "Graphs1" will come from "Imported1" (if "Imported" already exists). Code below:



    I know the code is probably really inefficient and looks horrible, but I just need to get it done. Inefficiency is the least of my concerns :)


    P.S. Extremely noob question: How do I automatically format the codes so it indents properly? And when an error occurs, how do I check which line is the problem? Thanks in advance!

  • Re: Checking worksheet names and renaming if duplicate found


    Hi VBAno0b,
    Nice to meet you.


    There is Smart Indent belowpage.
    http://www.oaltd.co.uk/Indenter/IndentFrm.htm
    Regards, junho

  • Re: Checking worksheet names and renaming if duplicate found


    VBANoOb,


    Cross posting without providing a link to the other post is a violation of the Forum Rules you agreed to abide by. We prefer you do not cross post, but if you do, you are required to provide a link.


    You cross posted at Excel Help Forum: Checking worksheet names and renaming if duplicate found


    To understand why cross posting is not acceptable read in full Message to Cross Posters which you can access via my signature.
    Future violations of this rule will result in your thread being deleted or locked without notice.

  • Re: Checking worksheet names and renaming if duplicate found


    Sorry, didn't realize it was the same forum..

    Thanks for responding junho lee, however, i get an error on this line:

    Code
    For Each ws In ThisWorkbook.Worksheets
  • Re: Checking worksheet names and renaming if duplicate found


    I have changed the code drastically now. I'm not sure if the code will check for the largest number of Imported (ie. find Imported2 instead of Imported1).. But it now looks like this:



    However, I get an error on this line:

    Code
    Range("O1").Select.Formula = "=Imported" & counter



    I don't understand why this won't run?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!