+ Reply to Thread
Results 1 to 10 of 10

Joining two codes into one

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2015
    Location
    St.Paul, Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    37

    Joining two codes into one

    Hi,

    I've got a code that compiles several sheets into one combined sheet. However, I want to add to my code some adjustments to be made for the newly created combined sheet. The code to compile the data is this:
    Sub Test()
        Dim SH As Worksheet
        Dim J As Integer
        Application.ScreenUpdating = False
            For Each SH In ThisWorkbook.Sheets
                SH.Rows("1:3").EntireRow.Delete
                SH.Columns("A:B").EntireColumn.Delete
                SH.Columns("E:E").EntireColumn.Delete
                SH.Cells.UnMerge
            Next
            
        On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a sheet in first place
        Sheets(1).Name = "Combined"
    
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
    
        ' work through sheets
        For J = 2 To Sheets.Count ' from sheet 2 to last sheet
            Sheets(J).Activate ' make the sheet active
            Range("A1").Select
            Selection.CurrentRegion.Select ' select all cells in this sheets
    
            ' select all lines except title
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
    
            ' copy cells selected in the new sheet on last line
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        Next
       
    End Sub
    And the code I've recorded that I want done to the newly created combined sheet is this:
    Sub Macro2()
    '
    ' Macro2 Macro
    '
    
    '
        Columns("A:D").Select
        Columns("A:D").EntireColumn.AutoFit
        Columns("B:B").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.EntireRow.Delete
        Columns("A:A").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.FormulaR1C1 = "=R[-1]C"
    End Sub
    Is there a way I can combine these macros into one so that it does both at the same time in the proper sequence? Thanks!

  2. #2
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: Joining two codes into one

    Try this.. COnsidering your First execution code is Sub Test()

    
    Sub Test()
        Dim SH As Worksheet
        Dim J As Integer
        Application.ScreenUpdating = False
            For Each SH In ThisWorkbook.Sheets
                SH.Rows("1:3").EntireRow.Delete
                SH.Columns("A:B").EntireColumn.Delete
                SH.Columns("E:E").EntireColumn.Delete
                SH.Cells.UnMerge
            Next
            
        On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a sheet in first place
        Sheets(1).Name = "Combined"
    
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
    
        ' work through sheets
        For J = 2 To Sheets.Count ' from sheet 2 to last sheet
            Sheets(J).Activate ' make the sheet active
            Range("A1").Select
            Selection.CurrentRegion.Select ' select all cells in this sheets
    
            ' select all lines except title
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
    
            ' copy cells selected in the new sheet on last line
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        Next
       Macro2
    End Sub
    
    
    Sub Macro2()
    '
    ' Macro2 Macro
    '
    
    '
        Columns("A:D").Select
        Columns("A:D").EntireColumn.AutoFit
        Columns("B:B").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.EntireRow.Delete
        Columns("A:A").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.FormulaR1C1 = "=R[-1]C"
    End Sub
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  3. #3
    Forum Contributor
    Join Date
    09-26-2014
    Location
    Moscow, Russia
    MS-Off Ver
    MSE 10, MSE 13
    Posts
    179

    Re: Joining two codes into one

    Hi there!

    You may call one macro from another with "Call MacroName" method. Try this one below. I also slightly improved "Macro2".

    Sub Test()
        Dim SH As Worksheet
        Dim J As Integer
        Application.ScreenUpdating = False
        For Each SH In ThisWorkbook.Sheets
            SH.Rows("1:3").EntireRow.Delete
            SH.Columns("A:B").EntireColumn.Delete
            SH.Columns("E:E").EntireColumn.Delete
            SH.Cells.UnMerge
        Next
        On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a sheet in first place
        Sheets(1).Name = "Combined"
    Call Macro2
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
        ' work through sheets
        For J = 2 To Sheets.Count ' from sheet 2 to last sheet
            Sheets(J).Activate ' make the sheet active
            Range("A1").Select
            Selection.CurrentRegion.Select ' select all cells in this sheets
            ' select all lines except title
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
            ' copy cells selected in the new sheet on last line
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        Next
    End Sub
    
    Sub Macro2()
        Columns("A:D").EntireColumn.AutoFit
        Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        Columns("A:A").SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    End Sub
    Best wishes and have a nice day!

  4. #4
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    854

    Re: Joining two codes into one

    Parth.

    I'm not sure if just calling Macro2 will do the trick as I'm not certain if he finishing on the new "Combined Sheet"

    Another approach is to place this at the end of your Test Macro.

    Note, you also do not need to 'select' something to do something with it.

    With Worksheets("Combined")
        Columns("A:D").EntireColumn.AutoFit
        Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        Selection.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    End With

  5. #5
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: Joining two codes into one

    ptmuldoon True indeed

  6. #6
    Registered User
    Join Date
    06-26-2015
    Location
    St.Paul, Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Joining two codes into one

    Thanks for the replies. Unfortunately I haven't had any sucess yet with either approaches. To clarify the order, my first code trims all of my data sheets, copies just the data, creates a new sheet called "Combined" and then pastes all the data into the combined sheet. I've had sucess with the first code, but I am now trying to join the second code into this same code with the adjustments that I would like to see done to the "Combined" sheet after it has just been created. Basically the adjustments, which you all seem to understand, to the combined sheet are to find blanks within Column B, delete the entire row with blanks found in B, and to also copy the data name down throughout column A, changing as a new name appears. Any further suggestions to combine these codes? Let me know if you need an attachment, because I would have to attempt to remove a bunch of confidential information beforehand. Thanks again!

  7. #7
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    854

    Re: Joining two codes into one

    Try this

    Sub Test()
        Dim SH As Worksheet
        Dim J As Integer
        Application.ScreenUpdating = False
            For Each SH In ThisWorkbook.Sheets
                SH.Rows("1:3").EntireRow.Delete
                SH.Columns("A:B").EntireColumn.Delete
                SH.Columns("E:E").EntireColumn.Delete
                SH.Cells.UnMerge
            Next
            
        On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a sheet in first place
        Sheets(1).Name = "Combined"
    
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
    
        ' work through sheets
        For J = 2 To Sheets.Count ' from sheet 2 to last sheet
            Sheets(J).Activate ' make the sheet active
            Range("A1").Select
            Selection.CurrentRegion.Select ' select all cells in this sheets
    
            ' select all lines except title
            Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
    
            ' copy cells selected in the new sheet on last line
            Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
        Next
       
       With Worksheets("Combined")
            .Columns("A:D").EntireColumn.AutoFit
            .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        End With
    End Sub

  8. #8
    Registered User
    Join Date
    06-26-2015
    Location
    St.Paul, Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Joining two codes into one

    Its almost there, the only changes that were made to the"Combined" sheet is that the columns A:D were Auto Fit for width, eveyrthing else stayed the same. Selecting blanks in column B and then deleting the entire rows with the blanks was not done, and copying the data name down in Column A was also not done. Thanks!

  9. #9
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    854

    Re: Joining two codes into one

    Sorry about that.

    Replace the bottom half with this
     With Worksheets("Combined")
            .Columns("A:D").EntireColumn.AutoFit
            .Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            .Columns("A:A").SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        End With

  10. #10
    Registered User
    Join Date
    06-26-2015
    Location
    St.Paul, Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Joining two codes into one

    Thanks, that did the trick! Quick question: Now that I'm looking at my data, is there a way I could add into the code a step that Filters column B to show any Numbers or Blank cells? I'd like to delete all those values as well, but I'm not sure if this can be done through code. Thanks again to Ptmuldoon and others for the replies.

+ 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. Replies: 1
    Last Post: 12-10-2013, 12:28 AM
  2. Replies: 0
    Last Post: 10-09-2013, 07:59 PM
  3. [SOLVED] Addresses - truncate 11 digit zip codes and leave 5 digit zip codes
    By landisf in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-25-2013, 11:15 PM
  4. Filter Zip Codes from a list codes
    By jaugent27 in forum Excel General
    Replies: 3
    Last Post: 06-11-2013, 02:01 PM
  5. Replies: 1
    Last Post: 04-15-2013, 05:16 PM
  6. Combining vba codes makes the previous codes broken !
    By MDPLUS in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-26-2013, 10:00 AM
  7. Replies: 6
    Last Post: 11-28-2006, 01:08 PM
  8. matching codes between 2 item codes
    By JChan in forum Excel General
    Replies: 1
    Last Post: 10-15-2005, 01:05 AM

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