Consulting

Results 1 to 4 of 4

Thread: Naming a sheet to the current year

  1. #1
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    2
    Location

    Naming a sheet to the current year

    Hi all,

    Just wondering if anyone can help. at year end im looking to store past data in a different sheet am currently thinking this,

    Private Sub CommandButton1_Click()
    
    Dim yearNum As StringyearNum = ActiveSheet.Range("A1")
    
    MsgBox (yearNum)   
    ''''''in order to check what i get, as yearNum = Year(currDate) was just saying 1988
    
    '''''here i want to try and check that the year doesnt already appear in one of the other sheets, just in case other users hit the button. dont know how to check this though,  hence asking for help here
    
    Sheets("Sheet1").Copy after:=Sheets("Sheet1")
    ActiveSheet.Name = yearNum
    
    '''' ill then activate the first sheet and clear the contents. 
    worksheets.sheet1.activate
    Worksheets = "Sheet1".Range= ("A3:AB1000").ClearContents

    If anyone could help with getting the code better and how to check for a duplicate sheet name would be greatful.

    Thank.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Maybe something like this

    The Button Click event calls this macro in a standard module


    Option Explicit
    
    Sub RenameCurrentSheet()
        Dim yearNum As Long, n As Long
        Dim wsOriginal As Worksheet
        
        Set wsOriginal = ActiveSheet
        
        With wsOriginal
        
            yearNum = CLng(.Range("A1"))
        
            MsgBox "The year used to create a new worksheet is " & yearNum
        
            n = -1
            On Error Resume Next
            n = Worksheets(yearNum).Index
            On Error GoTo 0
            
            If n <> -1 Then
                MsgBox yearNum & " already exists"
                Exit Sub
            End If
            
            .Copy after:=wsOriginal
        End With
        
        ActiveSheet.Name = yearNum
         
        wsOriginal.Range("A3:AB1000").ClearContents
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Dim CurrentSheet As Worksheet
    Set CurrentSheet = ActiveSheet

    You need to set up a True If False test
    On Error Resume Next
    If Not Sheets(CurrentSheet.Range("A1")).Name = CurrentSheet.Range("A1")
    'A Sheet's Name will never Not be the Sheet's Name, so the only way to get here 
    '  is with an error such as the sheet does not exist yet
    '
    'Create your new sheet and name it here
    '
    Errox = o 'Fix this line
    End If
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    2
    Location
    Thanks for the help here.

Posting Permissions

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