Solved

VB Code

Posted on 2011-02-22
6
226 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

707 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

12 Experts available now in Live!

Get 1:1 Help Now