0 votes
in VBA by Beginner (2 points)
Hope someone can help me with this.

I have a workbook with 3 sheets.

One is a sheet where I fill in some information about a project, sheet is called Info

The other 2 sheets are kind of templets and named, Dag vs. 12.1-Styrd Borrning.

These sheets are normally hidden and parts of them are locked with password.

On the Info sheet I have 2 buttons and I want to attach a macro to each one of them.

As example:

It should unhide the template sheet 12.1-Styrd Borrning, unlock  the password, copy the sheet, place it to the right side of the Info tab (that is always as number 4), name the new tab as 12.1-Styrd Borrning 1, lock the password, hide the template sheet again.

Next time I press my button (activate the macro) it should give me a new sheet again, placed as number 4 but this time named 12.1-Styrd Borrning 2, and so on..

On the Info sheet next to the button I also have a box in O14:V14 that should show the number of pages I have of 12.1-Styrd Borrning in the workbook.

Much appreciated if someone could find a few minutes to help me solve this.
by Skilled (279 points)
Can you attach a copy of your file so we don't have to try to recreate it?

1 Answer

0 votes
by Beginner (35 points)

I had to do this exact same thing for a file of mine. Here's the general code.

Sub PutNewSheet

    Dim ShtName as string

    ShtName = "12.1-Styrd Borrning " & (Worksheets.count - 2)

    Worksheets("12.1-Styrd Borrning").Copy After:=Worksheets(3)
    Worksheets("12.1-Styrd Borrning (2)").Name = ShtName
    Worksheets(ShtName).Visible = xlSheetVisible

    Worksheets("Info").TextBox1.Caption = (Worksheets.count - 3) & " Copies of 12.1-Styrd Borrning"

End Sub

You don't have to unhide or unlock the sheet to makea  copy of it. Adjust the code as needed (incase the index numbers are off). But you should get the idea.

Then for the box, since I'm not sure what you mean by the box, you'll need something like the last line, but  it'll be something like "TextBox1" or "Label1" if you used ActiveX controls (which are all I use). If you use Form controls I'm not as sure about those.

Hope this helps!

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.