+ Reply to Thread
Results 1 to 9 of 9

VBA - Delete sheets in activeworkbook

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    VBA - Delete sheets in activeworkbook

    Hello All, i have also posted this at MrExcel because i kind of need a solution fairly quick and i hoped to better my chance by posting at 2 sites.

    http://www.mrexcel.com/forum/excel-q...ml#post3801354
    I am working on a macro which creates a new workbook by copying activeworkbook and while doing so delete some sheets.

    For some reason, i can see that it deletes it, but then it creates it again. In question is highlighted in red font. Can someone point to me what i am doing wrong?

     some Codes to create new workbook then comes below
    'name new WB
        Set newWBK = Workbooks.Open(vpath1 & vpath2 & ".xlsm")
    'Make the new workbook active
        newWBK.Activate
        
    Application.DisplayAlerts = False
    If ComboBox3.Value = "Master" Then Sheet7.Delete
    Application.DisplayAlerts = True
    
    ActiveSheet.Range("Q1").Value = MyProduct
    ActiveSheet.Range("R1").Value = MyApplication
    ActiveSheet.Range("S1").Value = MySCN
    ActiveSheet.Range("T1").Value = MyEvent
    
    
    FriendlyName1 = "Link to Documents with SCN"
    MyPath1 = "https://tarregsp01p/sites/Regulatory/NextDocs%20Document%20Library/Forms/Detailed%20View.aspx?View={0C67A7C1-5BDA-4B11-A42D-4064D61BF7EC}&FilterField1=SCNNumber&FilterValue1=" & MySCN
    FriendlyName1 = "Link to RegApp with product"
    MyPath2 = "https://tarregsp01p/sites/Regulatory/_layouts/FormServer.aspx?XmlLocation=/sites/Regulatory/RegApp/Submission/Submission-" & MyProduct & "-" & MySCN & ".xml&DefaultItemOpen=1"
    
    
    ActiveSheet.Range("A1").Formula = "=Hyperlink(""" & MyPath1 & """, """ & "Link to Documents with - " & MySCN & """)"
    ActiveSheet.Range("C1").Formula = "=Hyperlink(""" & MyPath2 & """, """ & "Link to RegApp for - " & MySCN & " " & MyEvent & """)"
    
    With Range("a1").Font
        .Bold = True
    End With
    
    With Range("c1").Font
        .Bold = True
    End With
        
    newWBK.Save
    newWBK.Activate
    
    'Close the template without saving
    ThisWorkbook.Close False
    
    
    End Sub
    Last edited by honger; 05-07-2014 at 10:37 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,982

    Re: VBA - Delete sheets in activeworkbook

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    http://www.mrexcel.com/forum/excel-q...eworkbook.html

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA - Delete sheets in activeworkbook

    Below is the entire code i have.

    It is supposed to copy the open active workbook and saveas in a different location. While doing so i want some sheets to be deleted according to value chosen in combobox3. Everything works but it wont delete that sheet while creating the new workbook. It does delete it, but then it gets created again . I think i know this is happening because when i got rid of the Application.Displayalerts, diaglog box asked to confirm the delete and it deleted the sheet for a second but it recreated it

    Private Sub CommandButton1_Click()
    
    Dim newWBK As Workbook
    
    'Grab Data From UserFrom
    MyProduct = UserForm1.ComboBox1.Value
    MyApplication = UserForm1.ComboBox2.Value
    MySCN = UserForm1.TextBox1.Value
    MyEvent = UserForm1.TextBox2.Value
    
        
    MsgProceed = MsgBox("File Name : " & MyProduct & "-" & MyApplication & "-" & MySCN & "-" & MyEvent, vbYesNo, "Create New Submission?")
    If MsgProceed = vbNo Then Exit Sub
    
    Unload UserForm1
    UserForm1.Hide
    
    Dim vFilename As Variant
    Dim vPath As Variant
    
    vpath1 = "\\tarcds01\eCTD_Submission\TRACKERS\" & MyProduct & "\" & MyApplication & "\"
    vpath2 = MyProduct & "-" & MyApplication & "-" & MySCN & "-" & MyEvent
    
    ActiveWorkbook.SaveCopyAs Filename:=vpath1 & vpath2 & ".xlsm"
    
    'name new WB
        Set newWBK = Workbooks.Open(vpath1 & vpath2 & ".xlsm")
    'Make the new workbook active
        newWBK.Activate
        
    Application.DisplayAlerts = False
    If ComboBox3.Value = "Master" Then
    Sheet7.Delete
    End If
    Application.DisplayAlerts = True
    
    ActiveSheet.Range("Q1").Value = MyProduct
    ActiveSheet.Range("R1").Value = MyApplication
    ActiveSheet.Range("S1").Value = MySCN
    ActiveSheet.Range("T1").Value = MyEvent
    
    
    FriendlyName1 = "Link to Documents with SCN"
    MyPath1 = "https://tarregsp01p/sites/Regulatory/NextDocs%20Document%20Library/Forms/Detailed%20View.aspx?View={0C67A7C1-5BDA-4B11-A42D-4064D61BF7EC}&FilterField1=SCNNumber&FilterValue1=" & MySCN
    FriendlyName1 = "Link to RegApp with product"
    MyPath2 = "https://tarregsp01p/sites/Regulatory/_layouts/FormServer.aspx?XmlLocation=/sites/Regulatory/RegApp/Submission/Submission-" & MyProduct & "-" & MySCN & ".xml&DefaultItemOpen=1"
    
    
    ActiveSheet.Range("A1").Formula = "=Hyperlink(""" & MyPath1 & """, """ & "Link to Documents with - " & MySCN & """)"
    ActiveSheet.Range("C1").Formula = "=Hyperlink(""" & MyPath2 & """, """ & "Link to RegApp for - " & MySCN & " " & MyEvent & """)"
    
       
    newWBK.Save
    newWBK.Activate
    
    'Close the template without saving
    ThisWorkbook.Close False
    
    
    End Sub
    Last edited by honger; 05-07-2014 at 10:55 PM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA - Delete sheets in activeworkbook

    Quote Originally Posted by honger View Post
    I am working on a macro which creates a new workbook by copying activeworkbook and while doing so delete some sheets.

    For some reason, i can see that it deletes it, but then it creates it again. In question is highlighted in red font. Can someone point to me what i am doing wrong?
    I don't see any code where you copy the active workbook.

    You open a workbook file and delete Sheet7 in that opened workbook,do other formatting, then save it.

    You then close the workbook that contains the code (not the opened workbook).


    I'm not sure what you want done?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA - Delete sheets in activeworkbook

    sorry i was in rush and i was hoping for a better chance to get a solution... i have modified the first post, does that comply? if not please let me know. thank you

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,982

    Re: VBA - Delete sheets in activeworkbook

    Honger: Yes, it is now in compliance, but please read the link I provided on why it may backfire on you when you post to multiple sites.

  7. #7
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA - Delete sheets in activeworkbook

    Quote Originally Posted by alansidman View Post
    Honger: Yes, it is now in compliance, but please read the link I provided on why it may backfire on you when you post to multiple sites.
    i completely understand, i am very new to VBA and was in need of a solution quick...i am also ancxious to figure out this stuff because this is fascinating to me.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA - Delete sheets in activeworkbook

    You cannot use a worksheet's Code name (Sheet7) to refer to a sheet in another workbook. Sheet code names always refer to the same workbook that contains the code. You can only reference sheets in another workbook by Tab-name or index number.

    This gets the Tab-name of Sheet7 from the original workbook and uses that tab-name to delete the worksheet in the copy.

        Application.DisplayAlerts = False
        If ComboBox3.Value = "Master" Then
            newWBK.Sheets(Sheet7.Name).Delete
        End If
        Application.DisplayAlerts = True

  9. #9
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA - Delete sheets in activeworkbook

    thank you!!! that worked like a charm!!! you guys are great.

    I am not quite done yet and there is another thing i want to do, so i am going to give it a try now!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] ActiveWorkbook.Close closing sheets, but not the window
    By mintymike in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-28-2014, 07:36 AM
  2. Different between ActiveWorkbook.Sheet and ActiveWorkbook.WorkSheets
    By Ranjani in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2009, 12:56 AM
  3. Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  4. Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  5. Replies: 2
    Last Post: 05-07-2005, 12:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1