2

Wondering if anyone can help as I've tried several times to create this functionality within excel.

Summary - The end user will place the applicable rear in a particular cell within designated cell on worksheet and push a macro button to copy last years holiday tracker and create a new worksheet, subsequently renaming the copied worksheet.

Problem - when trying to declare the applicable worksheet variable worksheet name in question I get a type mismatch error.

Appreciate if someone can help?

The below code is stumbling block when declaring the sheet in question.

Sub test()

Dim ws as worksheet
Dim wsn as string

wsn = sheets("procedures").range("a1").value
Set ws = wsn

ws.activate

End sub

1 Answer 1

1

Wow, that's wordy.

will place the applicable rear in a particular cell within designated cell on worksheet

What's that mean?

Are you trying to add a worksheet and name it using the value in cell A1?
This will do the job providing A1 contains text that can be used as a sheet name.

Sub test()

    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets.Add

    ws.Name = Sheets("procedures").Range("a1")

    ws.Activate

End Sub

Edit:
This code will copy the sheet that is named in range A1, give it a new name and set a reference to it.

Sub test()

    Dim ws As Worksheet
    Dim wsn As String

    With ThisWorkbook
        wsn = .Worksheets("procedures").Range("a1")
        'Code to check the name is valid & the worksheet exists.
        '....

        'Unfortunately you can't set a reference while copying,
        'so copy it to before the first sheet and then reference the first sheet.
        .Worksheets(wsn).Copy Before:=.Worksheets(1)
        Set ws = .Worksheets(1)
    End With

    ws.Name = "Some Other Sheet Name"

    ws.Activate

End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

I presume that 'rear' is a typo for 'Year'
Ah yes, I get it now. :)
Correct, massive typo! Lol
All I need the coding to do at this point is select or activate the user defined worksheet and copy it. So yes cell A1 would contain the name of the worksheet in question.
Do you have a sheet called procedures and a text that can be used as a sheet name in range A1 of procedures (error - you typed an invalid name), or is there already a sheet using that name (error - cannot rename sheet)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.