+ Reply to Thread
Results 1 to 11 of 11

Macro only runs on sheet i recorded it on.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Macro only runs on sheet i recorded it on.

    I have a macro that i recorded on the first tab of my spreadsheet. I need it to function on all the tabs, and it looks likeits hanging up because it specficially only works on the first tabe. Here is the code form the macro. Can you guys pleasehelp me figure out how to make it function on all the tabs of the workbook.

    Thanks,
    brian


    Sub pastImacro()
    '
    ' pastImacro Macro
    '
    ' Keyboard Shortcut: Ctrl+i
    '
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("Table2").Select
        Range("F3").Activate
        Application.CutCopyMode = False
        ActiveWorkbook.Worksheets("01-Sep-2010").ListObjects("Table2").Sort.SortFields. _
            Clear
        ActiveWorkbook.Worksheets("01-Sep-2010").ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Status]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("01-Sep-2010").ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Created On]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("01-Sep-2010").ListObjects("Table2").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("F3").Select
        ActiveWindow.SmallScroll Down:=-27
        Range("A3:F3").Select
        Range("F3").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.249977111117893
            .PatternTintAndShade = 0
        End With
        Range("A4:F4").Select
        Range("F4").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.349986266670736
            .PatternTintAndShade = 0
        End With
        Range("A3:F4").Select
        Selection.Copy
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Range("A5:F101").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    End Sub
    Last edited by BF15; 09-19-2011 at 06:44 PM.

  2. #2
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro only runs on sheet i recorded it on.

    just to add to this, all the tabs are excat copies of each other, other than having a different date. Im trying to use this macro to jsut simply be able to do a paste and it sort, and format the page.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Macro only runs on sheet i recorded it on.

    Hello and welcome to the forum - unfortunately before we go any further you need to read the forum rules and use code tags around any code that you post. If you edit your original post then I'm sure somebody will be happy to help you.

  4. #4
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro only runs on sheet i recorded it on.

    Quote Originally Posted by Andrew-R View Post
    Hello and welcome to the forum - unfortunately before we go any further you need to read the forum rules and use code tags around any code that you post. If you edit your original post then I'm sure somebody will be happy to help you.
    Ok, i think i wraped it correctly.

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Macro only runs on sheet i recorded it on.

    Yup, looks like the job.

    Macros written with the macro recorder are rather inefficient, because they have in a lot of selecting data which doesn't need to happen. It's late here in the UK, so rather than rewrite this properly I've just hacked it to work, but work it should...

    Sub pastImacro()
    '
    ' pastImacro Macro
    '
    ' Keyboard Shortcut: Ctrl+i
    '
    Dim lSheetLoop As Long
    
    For lSheetLoop = 1 To ActiveWorkbook.Sheets.Count
    
      ActiveWorkbook.Sheets(lSheetLoop).Activate
    
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("Table2").Select
        Range("F3").Activate
        Application.CutCopyMode = False
        ActiveSheet.ListObjects("Table2").Sort.SortFields. _
            Clear
        Activesheet.ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Status]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        Activesheet.ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Created On]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With Activesheet.ListObjects("Table2").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("F3").Select
        ActiveWindow.SmallScroll Down:=-27
        Range("A3:F3").Select
        Range("F3").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.249977111117893
            .PatternTintAndShade = 0
        End With
        Range("A4:F4").Select
        Range("F4").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.349986266670736
            .PatternTintAndShade = 0
        End With
        Range("A3:F4").Select
        Selection.Copy
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Range("A5:F101").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    
    Next lSheetLoop
    
    End Sub

  6. #6
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro only runs on sheet i recorded it on.

    Quote Originally Posted by Andrew-R View Post
    Yup, looks like the job.

    Macros written with the macro recorder are rather inefficient, because they have in a lot of selecting data which doesn't need to happen. It's late here in the UK, so rather than rewrite this properly I've just hacked it to work, but work it should...

    Sub pastImacro()
    '
    ' pastImacro Macro
    '
    ' Keyboard Shortcut: Ctrl+i
    '
    Dim lSheetLoop As Long
    
    For lSheetLoop = 1 To ActiveWorkbook.Sheets.Count
    
      ActiveWorkbook.Sheets(lSheetLoop).Activate
    
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("Table2").Select
        Range("F3").Activate
        Application.CutCopyMode = False
        ActiveSheet.ListObjects("Table2").Sort.SortFields. _
            Clear
        Activesheet.ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Status]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        Activesheet.ListObjects("Table2").Sort.SortFields. _
            Add Key:=Range("Table2[Created On]"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, DataOption:=xlSortNormal
        With Activesheet.ListObjects("Table2").Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("F3").Select
        ActiveWindow.SmallScroll Down:=-27
        Range("A3:F3").Select
        Range("F3").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.249977111117893
            .PatternTintAndShade = 0
        End With
        Range("A4:F4").Select
        Range("F4").Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.349986266670736
            .PatternTintAndShade = 0
        End With
        Range("A3:F4").Select
        Selection.Copy
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Range("A5:F101").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    
    Next lSheetLoop
    
    End Sub
    I replaced my existing code on in th emacro with this.

    Now when i try to sue ctrl i to do the special paste it does not paste anything.

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Macro only runs on sheet i recorded it on.

    Please don't quote entire posts when replying, it just clutters up the thread.

    Is the Ctrl-i short-cut still assigned to the macro? That would be the first thing to check.

  8. #8
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro only runs on sheet i recorded it on.

    Quote Originally Posted by Andrew-R View Post
    Please don't quote entire posts when replying, it just clutters up the thread.

    Is the Ctrl-i short-cut still assigned to the macro? That would be the first thing to check.
    Sorry about that.

    Ok, that was the problem. Now im having an issue when i run the macro.

    run-time error '1004':

    PasteSpecial method of Range class failed.

    Do you have any idea what could cause this?
    Thanks for all your help.

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Macro only runs on sheet i recorded it on.

    It looks like the issue is that you're trying to paste special without actually copying anything first - are you running this macro after selecting a range to copy?

  10. #10
    Registered User
    Join Date
    09-19-2011
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Macro only runs on sheet i recorded it on.

    Quote Originally Posted by Andrew-R View Post
    It looks like the issue is that you're trying to paste special without actually copying anything first - are you running this macro after selecting a range to copy?
    yes, im copying the datafrom a seperate spreadsheet. and trying to paste it into the one the macro is ran on.

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Macro only runs on sheet i recorded it on.

    OK, that's what's causing the problem. On the first pass through the worksheet loop you're pasting the data then turning the cut and paste mode off, so subsequent passes through have nothing to paste.

    Is it the range "Table2" that you're pasting?

+ 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.6.0 RC 1