+ Reply to Thread
Results 1 to 3 of 3

Help combining two Macros, one that formats & another that splits the sheet into multiple

Hybrid View

taylorsm Help combining two Macros,... 09-20-2016, 10:52 AM
JBeaucaire Re: Help combining two... 09-20-2016, 05:11 PM
molonlabe Re: Help combining two... 09-21-2016, 11:23 AM
  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Help combining two Macros, one that formats & another that splits the sheet into multiple

    I think I could figure out how to do it, but I imagine it would be dirty and the splitting macro is already very consuming so I don't want to add any unnecessary power drains. Is there a way to clean and combine the two? Or should I just leave them separate. Not really a big, one button vs two if that is the case. Also, I am tested the macros again to day and the EDNotepadSplit seems to be taking an excessively long time than it used too. I haven't adjusted the code at any point though.

    Sub EDNotepadFormat()
    Rows("1:4").Delete
    Columns("c").ColumnWidth = 125
    Columns("d:l").Delete
    Columns("b").ColumnWidth = 30
    Columns("a").NumberFormat = "mm/dd/yy hh:mm:ss am/pm"
    Columns("a").ColumnWidth = 25
    Range("a1:IV65536").Select
        Selection.UnMerge
        Range("E9").Select
    With ActiveSheet.PageSetup
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
        End With
    Cells.Select
    Selection.Rows.AutoFit
    ActiveSheet.Cells.Borders.LineStyle = xlLineStyleNone
    End Sub

    Option Explicit
    
    Sub EDNotepadSplit()
        Dim myAreas As Areas, I As Long, ii As Long, n As Long, rng As Range
        Application.ScreenUpdating = False
        Set myAreas = ActiveSheet.Columns(1).SpecialCells(2).Areas
        For I = 1 To myAreas.Count
            If myAreas(I)(1).Font.Bold Then
                Set rng = myAreas(I).Resize(myAreas(I).Count + 1)
                ii = 1
                Do While I + ii <= myAreas.Count
                    If myAreas(I + ii)(1).Font.Bold Then Exit Do
                    Set rng = Union(rng, myAreas(I + ii))
                    ii = ii + 1
                Loop
                n = n + 1: If "Sheet" & n = myAreas.Parent.Parent.Name Then n = n + 1
                DeleteSheet "Sheet" & n
                myAreas.Parent.Parent.Copy after:=Sheets(Sheets.Count)
                With ActiveSheet
                    .Name = "Sheet" & n: .Cells.ClearContents
                    rng.EntireRow.Copy .Cells(1)
                End With
                I = I + ii - 1: Set rng = Nothing
            End If
        Next
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub DeleteSheet(ByVal wsName As String)
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets(wsName).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    End Sub
    Last edited by taylorsm; 09-20-2016 at 10:56 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help combining two Macros, one that formats & another that splits the sheet into multi

    1) A third macro that calls the two connected to a single button:
    Sub Master()
        Call EDNotepadFormat
        Call EDNotepadSplit
    End Sub
    2) Your second macro is destroying and creating a sheet for each copy. I would think simply using the existing sheets would be faster.
                n = n + 1: If "Sheet" & n = myAreas.Parent.Parent.Name Then n = n + 1
                DeleteSheet "Sheet" & n
                myAreas.Parent.Parent.Copy after:=Sheets(Sheets.Count)
                With ActiveSheet
                    .Name = "Sheet" & n: .Cells.ClearContents
                    rng.EntireRow.Copy .Cells(1)
                End With
    Maybe this:
    n = n + 1: If "Sheet" & n = myAreas.Parent.Parent.Name Then n = n + 1
    If Evaluate("ISREF(Sheet" & n & "!A1)") Then
        With Sheets("Sheet" & n)
            .UsedRange.ClearContents
            rng.EntireRow.Copy .Range("A1")
        End With
    Else
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet" & n
        rng.EntireRow.Copy .Range("A1")
    End If
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-06-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    19

    Re: Help combining two Macros, one that formats & another that splits the sheet into multi

    Wow, that is really simple. Sorry that would have been an easy google.

    With your other, I copied and pasted over the code you cited and I get a "compile error: invalid or unqualified reference" and then it highlights .Range("A1")

    Option Explicit
    
    Sub NotepadSplit()
        Dim myAreas As Areas, I As Long, ii As Long, n As Long, rng As Range
        Application.ScreenUpdating = False
        Set myAreas = ActiveSheet.Columns(1).SpecialCells(2).Areas
        For I = 1 To myAreas.Count
            If myAreas(I)(1).Font.Bold Then
                Set rng = myAreas(I).Resize(myAreas(I).Count + 1)
                ii = 1
                Do While I + ii <= myAreas.Count
                    If myAreas(I + ii)(1).Font.Bold Then Exit Do
                    Set rng = Union(rng, myAreas(I + ii))
                    ii = ii + 1
                Loop
                n = n + 1: If "Sheet" & n = myAreas.Parent.Parent.Name Then n = n + 1
    If Evaluate("ISREF(Sheet" & n & "!A1)") Then
        With Sheets("Sheet" & n)
            .UsedRange.ClearContents
            rng.EntireRow.Copy .Range("A1")
        End With
    Else
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet" & n
        rng.EntireRow.Copy .Range("A1")
    End If
                I = I + ii - 1: Set rng = Nothing
            End If
        Next
        myAreas.Parent.Parent.Select
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub DeleteSheet(ByVal wsName As String)
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets(wsName).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    End Sub

+ 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] Combining multiple macros into one
    By the tax man in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2014, 08:19 PM
  2. [SOLVED] Combining Multiple Macros into One
    By pinstripe05 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-07-2013, 03:06 AM
  3. Combining multiple macros for entire workbook
    By sonexus1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2013, 05:50 PM
  4. [SOLVED] Combining Sheets with a Row in between and using one buttion for multiple Macros
    By compgeek1979 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-24-2012, 11:08 AM
  5. Combining multiple macros into one
    By strder1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-11-2012, 08:51 AM
  6. Combining multiple macros into one subroutine
    By Quentyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2011, 05:48 PM
  7. Combining Multiple Macros
    By ZEvans12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2011, 01:39 PM
  8. Combining Multiple Macros
    By Brenda42 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2011, 09:50 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