+ Reply to Thread
Results 1 to 6 of 6

VBA code to ungroup worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    09-14-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    7

    VBA code to ungroup worksheets

    Hi All,

    Excuse my VBA, its a bit rusty. But i wrote a code, to copy worksheets from one workbook, to another workbook, that gets sent out to a mailing list. The issue is after doing so, the original workbook, groups all the tabs. I would like your help to amend the code to ungroup the tabs in the original workbook.

    I think the issue is in the initial sheets(array()) line which initially groups it

    Thanks


    Here is the code I have:

    Sub ExportNewFile()
    
    On Error GoTo errorHandler
    ' '*****Part 1: Define location to save the exported files***
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
    Dim NewName As String
    
        thisAppwin = Application.ActiveWorkbook.Name
        strpath = Application.ActiveWorkbook.Path & "\"
        cdir = Left(strpath & thisAppwin, Len(strpath) + Len(thisAppwin) - 5)
        NewName = cdir & "_EXPORT" ' & thisAppwin
        
        
        Sheets(Array("DATA", "UK", "EU7", "EU28", "GLOBAL" _
            , "PRINT")).Select
      
        Sheets(Array("DATA", "UK", "EU7", "EU28", "GLOBAL" _
            , "PRINT")).Copy
        
    
            ActiveWorkbook.SaveAs Filename:= _
            NewName & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
           
    
        Sheets("PRINT").Activate
        ActiveSheet.Buttons.Delete
        Sheets("DATA").Activate
        ActiveSheet.Buttons.Delete
        
       Range("I1").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
       Range("A1").Select
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        ActiveWorkbook.Save
       MsgBox ("Your Excel file to send out is in the same folder; " & NewName), vbOKOnly
     Exit Sub
    
    errorHandler:
       MsgBox ("There has been an error. Please contact IT for support")
       
    End Sub
    Last edited by Oilking; 02-27-2014 at 07:27 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to ungroup worksheets

    Hi,

    Selecting a sheet that hasn't been grouped will remove the grouping. e.g. Sheet1.Select
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    09-14-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA code to ungroup worksheets

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Selecting a sheet that hasn't been grouped will remove the grouping. e.g. Sheet1.Select

    1. The problem is that all the worksheets on the original workbook are grouped, so i cannot select a worksheet.

    2. For another workbook i have where i do have a non grouped worksheet, selecting it is fine, it ungroups it, but via VBA i have an issue as activating the original workbook due to the filename being dynamic becomes troublesome. Below is the code i used, to activate the original workbook but doesnt seem to work

    Dim w As workbook 
    Set w=ActiveWorkbook 
     ....
    
    w.Activate
    Would you be kind to tell me what code will solve this?

    Regards
    Last edited by Oilking; 02-27-2014 at 07:38 AM.

  4. #4
    Registered User
    Join Date
    09-14-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA code to ungroup worksheets

    The code attached doesnt seem to activate the original workbook

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: VBA code to ungroup worksheets

    You may omit select:
    Sub ExportNewFile()
    
    On Error GoTo errorHandler
    ' '*****Part 1: Define location to save the exported files***
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
    Dim NewName As String
    
        thisAppwin = Application.ActiveWorkbook.Name
        strpath = Application.ActiveWorkbook.Path & "\"
        cdir = Left(strpath & thisAppwin, Len(strpath) + Len(thisAppwin) - 5)
        NewName = cdir & "_EXPORT" ' & thisAppwin
        
          
        Sheets(Array("DATA", "UK", "EU7", "EU28", "GLOBAL" _
            , "PRINT")).Copy
        
    
            ActiveWorkbook.SaveAs Filename:= _
            NewName & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
           
    
        Sheets("PRINT").Activate
        ActiveSheet.Buttons.Delete
        Sheets("DATA").Activate
        ActiveSheet.Buttons.Delete
        
       Range("I1").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
       Range("A1").Select
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        ActiveWorkbook.Save
       MsgBox ("Your Excel file to send out is in the same folder; " & NewName), vbOKOnly
     Exit Sub
    
    errorHandler:
       MsgBox ("There has been an error. Please contact IT for support")
       
    End Sub
    You may also simply use:
    Sheets("DATA").Select
    to deselect all other sheets.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  6. #6
    Registered User
    Join Date
    09-14-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA code to ungroup worksheets

    This works now. Thank you all for your help.

    Quote Originally Posted by Izandol View Post
    You may omit select:
    Sub ExportNewFile()
    
    On Error GoTo errorHandler
    ' '*****Part 1: Define location to save the exported files***
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
    Dim NewName As String
    
        thisAppwin = Application.ActiveWorkbook.Name
        strpath = Application.ActiveWorkbook.Path & "\"
        cdir = Left(strpath & thisAppwin, Len(strpath) + Len(thisAppwin) - 5)
        NewName = cdir & "_EXPORT" ' & thisAppwin
        
          
        Sheets(Array("DATA", "UK", "EU7", "EU28", "GLOBAL" _
            , "PRINT")).Copy
        
    
            ActiveWorkbook.SaveAs Filename:= _
            NewName & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
           
    
        Sheets("PRINT").Activate
        ActiveSheet.Buttons.Delete
        Sheets("DATA").Activate
        ActiveSheet.Buttons.Delete
        
       Range("I1").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
       Range("A1").Select
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        ActiveWorkbook.Save
       MsgBox ("Your Excel file to send out is in the same folder; " & NewName), vbOKOnly
     Exit Sub
    
    errorHandler:
       MsgBox ("There has been an error. Please contact IT for support")
       
    End Sub
    You may also simply use:
    Sheets("DATA").Select
    to deselect all other sheets.

+ 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. Ungroup selected worksheets after Before_Print event
    By clng in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-27-2010, 11:53 PM
  2. Group/Ungroup
    By rz6657 in forum Excel General
    Replies: 1
    Last Post: 03-26-2009, 02:55 AM
  3. Group and Ungroup Macro
    By caliskier in forum Excel General
    Replies: 2
    Last Post: 09-20-2007, 02:31 PM
  4. Help with Group/Ungroup
    By deacs in forum Excel General
    Replies: 3
    Last Post: 01-30-2006, 12:30 PM
  5. [SOLVED] how do you ungroup a spreadsheet
    By Sparkle in forum Excel General
    Replies: 1
    Last Post: 12-16-2005, 01:00 PM

Tags for this Thread

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