Solved

VB Code

Posted on 2011-02-22
6
233 Views
Last Modified: 2012-05-11
Basically I was planning to hide sheet3 on my workbook, but when the command button is clicked on sheet1; the hidden sheet (sheet 3) would open in a brand new/separate worksheet.

So I was thinking the code could copy the format in the hidden sheet (sheet3) and then paste this into a brand new occurence of excel.
0
Comment
Question by:Gazza83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34956573
Try this simple code

Private Sub CommandButton1_Click()
    Dim wb1 As Workbook, wb2 As Workbook
    
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks.Add
    
    wb1.Sheets("Sheet3").Copy After:=wb2.Sheets(wb2.Sheets.Count)
    wb2.Sheets(wb2.Sheets.Count).Visible = True
End Sub

Open in new window


Sid
0
 

Author Comment

by:Gazza83
ID: 34957240
in excel 2011 it is creating the new file but as opposed to just sheet 3 being in the new workbook, there are now four sheets within the new workbook "Sheet3 (2)", sheet3,sheet2, sheet1

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34957260
Try this

Private Sub CommandButton1_Click()
    Dim wb1 As Workbook, wb2 As Workbook
    
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks.Add
    
    For i = 1 To wb2.Sheets.Count
        Sheets(i).Name = "Sid" & i
    Next
    
    wb1.Sheets("Sheet3").Copy After:=wb2.Sheets(wb2.Sheets.Count)
    wb2.Sheets(wb2.Sheets.Count).Visible = True
    
    For i = wb2.Sheets.Count To 1 Step -1
        Application.DisplayAlerts = False
        On Error Resume Next
        If InStr(1, Sheets(i).Name, "sid", vbTextCompare) Then Sheets(i).Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    Next
    
    wb2.Sheets(1).Name = "Sheet3"
End Sub

Open in new window


Sid
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34957265
By mistake I kept the Application.DisplayAlerts in the loop. Please use this code.

Private Sub CommandButton1_Click()
    Dim wb1 As Workbook, wb2 As Workbook
    
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks.Add
    
    For i = 1 To wb2.Sheets.Count
        Sheets(i).Name = "Sid" & i
    Next
    
    wb1.Sheets("Sheet3").Copy After:=wb2.Sheets(wb2.Sheets.Count)
    wb2.Sheets(wb2.Sheets.Count).Visible = True
    
    Application.DisplayAlerts = False
    
    For i = wb2.Sheets.Count To 1 Step -1
        On Error Resume Next
        If InStr(1, Sheets(i).Name, "sid", vbTextCompare) Then Sheets(i).Delete
        On Error GoTo 0
    Next
    
    Application.DisplayAlerts = True
    
    wb2.Sheets(1).Name = "Sheet3"
End Sub

Open in new window

0
 

Author Closing Comment

by:Gazza83
ID: 34957283
Brilliant, thanks!
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34957293
Glad to be of help. Though it worked, but I saw a small error in the above code.

Please change line 8

Sheets(i).Name = "Sid" & i

to

wb2.Sheets(i).Name = "Sid" & i

and line 18

        If InStr(1, Sheets(i).Name, "sid", vbTextCompare) Then Sheets(i).Delete

to

        If InStr(1, wb2.Sheets(i).Name, "sid", vbTextCompare) Then wb2.Sheets(i).Delete


Sid
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question