Solved

VB Code

Posted on 2011-02-22
6
227 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now