+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: How do you put a macro in a macro

  1. #1
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Smile How do you put a macro in a macro

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
        Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=IF(RC[-2]=R1C2,RC[-1],IF(RC[-2]=R3C2,RC[-1],0))"
        Range("D1").Select
        Selection.AutoFill Destination:=Range("D1:D44160")
        Range("D1:D44160").Select
        Selection.Copy
        Columns("C:C").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("D:D").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows2"
        Application.Run "'ASX Closing Prices.xlsm'!x"
        Application.Run "'ASX Closing Prices.xlsm'!x"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=(RC[-1]-R[1]C[-1])/R[1]C[-1]"
        Range("D1:D2").Select
        Selection.AutoFill Destination:=Range("D1:D27632")
        Range("D1:D27632").Select
        Selection.Copy
        Columns("D:D").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("B:C").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows3"
        Columns("B:B").Select
        Selection.Style = "Percent"
        Columns("A:B").Select
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range( _
            "B1:B56142"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet2").Sort
            .SetRange Range("A1:B56142")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    This is my main macro (above) and in this macro I have used some macros which are shown below.
    I want to merge these two macros to create one macro, because at the moment I am creating a macro to run a macro - I was wondering if I could get the macro of the macros I am running and put it in my main macro

    Sub DeleteBlankARows1()
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    Dim r As Long
    For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
    If Cells(r, 3) = "" Then Rows(r).Delete
    Next r
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub
    Sub DeleteBlankARows2()
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    Dim r As Long
    For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
    If Cells(r, 3) = "0" Then Rows(r).Delete
    Next r
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub
    Sub DeleteBlankARows3()
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    Dim r As Long
    For r = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If Cells(r, 1) = "" Then Rows(r).Delete
    Next r
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub
    Sub x()
    Dim rng1 As Variant, rng2 As Variant, totalRng As Variant, cell As Variant
    Set rng1 = Worksheets("Sheet2").Range("B1")
    Set rng2 = rng1.End(xlDown)
    Set totalRng = Range(rng1, rng2)
    
    For Each cell In totalRng
        If cell.Value = cell.Offset(1, 0).Value Then
            cell.Rows.EntireRow.Delete Shift:=xlUp
        End If
    Next cell
    End Sub
    Last edited by drmix; 11-18-2010 at 11:07 PM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    You don't give us a lot to work with. You can insert this line of code in Macro1 anywhere you wish
    Call DeleteBlankARows1
    and it will execute the procedure DeleteBlankARows1 then return to Sub Macro1 at the point it was interrupted.

    Is this your issue?

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    I have updated my explanation (see above)

  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    It's late here and I'm going to bed. However I'll be happy to revisit this issue in the AM. In the meantime, please look at this link http://www.oaltd.co.uk/Indenter/Default.htm it'll help the readability of your code.

    Also, in my opinion, keeping your code compartmentalized is a good approach. Calling procedures to do specific tasks works for me.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    cool thanks

    basically i want to replace
    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
    with
    Sub DeleteBlankARows1()
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    Dim r As Long
    For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
    If Cells(r, 3) = "" Then Rows(r).Delete
    Next r
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub
    etc...

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: How do you put a macro in a macro

    Is the DeleteBlankARows1 macro in the same workbook?
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  7. #7
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    I feel as if I'm missing the point of your question. However, if the answer to RoyUK's question is affirmative, then you can simply replace the line of code as follows:
     '    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        Call DeleteBlankARows1
    The above would be my preference. However, if you only want one macro, you could modify the code as follows:
    '    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            Dim r As Long
            For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
                If Cells(r, 3) = "" Then Rows(r).Delete
            Next r
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    As I said, I feel I'm off point.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    Mmm - thanks but yes I think this is not what I was after

    Let me try and clarify:

    Currently the macro call "macro1" is my main macro. And in this macro I have "called" 4 other marcos called "DeleteBlankARows1", "DeleteBlankARows2" etc.

    The end result is that I do not want "macro1" to call the other 4 macros but have them embedded in "macro1".

    Is that possible? Hope that helps

    (Because if I run "macro1" in a new worksheet without the other 4 macros it stuffs up)

  9. #9
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    I'm still not clear on your question UNLESS you're asking if you can do this
    Option Explicit
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
        Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        Range("D1").Select
     
     
        '    ActiveCell.FormulaR1C1 = "=IF(RC[-2]=R1C2,RC[-1],IF(RC[-2]=R3C2,RC[-1],0))"
    Sub DeleteBlankARows1()
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            Dim r As Long
            For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
                If Cells(r, 3) = "" Then Rows(r).Delete
            Next r
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
     
     
    Range("D1").Select
    Selection.AutoFill Destination:=Range("D1:D44160")
    Range("D1:D44160").Select
    Selection.Copy
    Columns("C:C").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Columns("D:D").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows2"
    Application.Run "'ASX Closing Prices.xlsm'!x"
    Application.Run "'ASX Closing Prices.xlsm'!x"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "=(RC[-1]-R[1]C[-1])/R[1]C[-1]"
    Range("D1:D2").Select
    Selection.AutoFill Destination:=Range("D1:D27632")
    Range("D1:D27632").Select
    Selection.Copy
    Columns("D:D").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Columns("B:C").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows3"
    Columns("B:B").Select
    Selection.Style = "Percent"
    Columns("A:B").Select
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range( _
            "B1:B56142"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A1:B56142")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    End Sub
    If that's what you're asking, in a word "No", to the best of my knowledge (admittedly limited), can't be done. You can "Call" the procedure but cannot "Embed" the entire procedure. You cannot have
    Sub DeleteBlankARows1()
    'Your Code
    End Sub
    inside another procedure. Sorry if I was misunderstanding your question.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  10. #10
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    mmm... I was wondering if

    Sub Macro1()
    ' Macro1 Macro
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
        Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        ...
    End sub
    could be replaced with this - and if not why not?

    Sub Macro1()
    ' Macro1 Macro
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    Dim r As Long
    For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
    If Cells(r, 3) = "" Then Rows(r).Delete
    Next r
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
        ...
    End sub

  11. #11
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    I believe that's what I suggested here
    '    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            Dim r As Long
            For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
                If Cells(r, 3) = "" Then Rows(r).Delete
            Next r
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    Replacing this line of code (notice that it's commented out)
    '    Application.Run "'ASX Closing Prices.xlsm'!DeleteBlankARows1"
    with this
    With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            Dim r As Long
            For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
                If Cells(r, 3) = "" Then Rows(r).Delete
            Next r
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    I must be missing something terribly here and I apologize for that...we don't appear to be communicating.
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  12. #12
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    awesome! i didnt see the commented out,

    however when i do this for the two 3 macros - there is an error - something to do with "dim r as long" do you know how to fix?

  13. #13
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    I REALLY DON"T LIKE working on code I can't test. Having said that, comment out this line of code
    Dim r As Long
    in the
    two 3 macros
    and see what happens.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  14. #14
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: How do you put a macro in a macro

    Hi drmix

    To help us help you refer to the error message. For example, you probably received a messgae like "Duplicate Declaration...". Refer to the error message so we know what's happening. This dosen't help
    something to do with "dim r as long"
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  15. #15
    Registered User
    Join Date
    11-16-2010
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: How do you put a macro in a macro

    Hi all thankyou for your patience - so I have put in the codes and this is the error i get

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
        Columns("A:A").Select
        Selection.Delete Shift:=xlToLeft
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp
        On Error Resume Next
        Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=IF(RC[-2]=R1C2,RC[-1],IF(RC[-2]=R3C2,RC[-1],0))"
        Range("D1").Select
        Selection.AutoFill Destination:=Range("D1:D44160")
        Range("D1:D44160").Select
        Selection.Copy
        Columns("C:C").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("D:D").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        Dim r As Long
        For r = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
        If Cells(r, 3) = "0" Then Rows(r).Delete
        Next r
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        End With
        Dim rng1 As Variant, rng2 As Variant, totalRng As Variant, cell As Variant
        Set rng1 = Worksheets("Sheet2").Range("B1")
        Set rng2 = rng1.End(xlDown)
        Set totalRng = Range(rng1, rng2)
    
        For Each cell In totalRng
        If cell.Value = cell.Offset(1, 0).Value Then
            cell.Rows.EntireRow.Delete Shift:=xlUp
        End If
        Next cell
        Dim rng1 As Variant, rng2 As Variant, totalRng As Variant, cell As Variant
        Set rng1 = Worksheets("Sheet2").Range("B1")
        Set rng2 = rng1.End(xlDown)
        Set totalRng = Range(rng1, rng2)
    
        For Each cell In totalRng
        If cell.Value = cell.Offset(1, 0).Value Then
            cell.Rows.EntireRow.Delete Shift:=xlUp
        End If
        Next cell
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "=(RC[-1]-R[1]C[-1])/R[1]C[-1]"
        Range("D1:D2").Select
        Selection.AutoFill Destination:=Range("D1:D27632")
        Range("D1:D27632").Select
        Selection.Copy
        Columns("D:D").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("B:C").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        Dim r As Long
        For r = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If Cells(r, 1) = "" Then Rows(r).Delete
        Next r
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        End With
        Columns("B:B").Select
        Selection.Style = "Percent"
        Columns("A:B").Select
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range( _
            "B1:B56142"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet2").Sort
            .SetRange Range("A1:B56142")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Compile error: Dupliate declaration in current scope

    I have also attached my worksheet - hope this helps! =]
    Attached Files Attached Files
    Last edited by drmix; 11-18-2010 at 09:44 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0