Solved

VB Code

Posted on 2011-02-22
6
234 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

717 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