+ Reply to Thread
Results 1 to 7 of 7

Trying to make a recorded macro work properly

Hybrid View

  1. #1
    Registered User
    Join Date
    08-22-2012
    Location
    Bridgend, Wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Trying to make a recorded macro work properly

    Hello everyone

    Although I've been a member for a while, this is my first post.

    This is my problem. I have recorded a macro to pull data from an Excel worksheet into a pivot table on a new worksheet with defined formatting and layout.
    I also want the macro to rename the tabs of the data sheet and pivot table sheet. I want the macro to be able to be run no matter what the data sheet name be, "Sheet1", "Data", etc, it's only the 'new' name that is important, in this instance I want them to be titled, "Labour Hours" for the data sheet and "Hours Pivot" for the pivot.

    I have seen many comments here about wrapping code into tags, but I have no idea what that is so rather than make the same error, can someone tell me how to upload my recorded code. I have attached the data sheet to begin with.

    Any help you can give would be hugely appreciated.

    Many thanks in advance.

    Nick
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to make a recorded macro work properly

    To upload your code, copy your code into your post window, then select it and press the # icon at the top of your post window.

    Alternatively, type [code] before your code and [/code] after your code and that will input the code tags for you.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    08-22-2012
    Location
    Bridgend, Wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Trying to make a recorded macro work properly

    Hi arlu

    Thank you very much for your guidance, here is the code I need help with ...

    Sub Labour_Hours()
    '
    ' Labour_Hours Macro
    ' abour actuals into pivot pre determined layout
    ' Recorded by Nicholas Owen 04-Dec-12
    
    '
        Sheets("ActiveSheet").Name = "Labour Hours"
        Columns("Y:Y").Select
        Selection.Insert Shift:=xlToRight
        Range("Y1").Select
        ActiveCell.FormulaR1C1 = "Total Hours"
        With ActiveCell.Characters(Start:=1, Length:=11).Font
            .Name = "Arial"
            .FontStyle = "Regular"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        Range("Y2").Select
        ActiveCell.FormulaR1C1 = "=SUM(RC[-2],RC[-1])"
        Range("Y2").Select
        Selection.AutoFill Destination:=Range("Y2:Y2596"), Type:=xlFillDefault
        Range("Y2:Y2596").Select
        Rows("1:1").Select
        Selection.Font.Bold = True
        Cells.Select
        Cells.EntireColumn.AutoFit
        Sheets.Add
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Labour Hours!R1C1:R1048576C34", Version:=xlPivotTableVersion12). _
            CreatePivotTable TableDestination:="Sheet2!R3C1", TableName:="PivotTable1" _
            , DefaultVersion:=xlPivotTableVersion12
        Sheets("Sheet2").Select
        Cells(3, 1).Select
        Sheets("Sheet2").Select
        Sheets("Sheet2").Name = "Hours Pivot"
        Range("A20").Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Staff Name")
            .Orientation = xlRowField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Cost Code")
            .Orientation = xlRowField
            .Position = 2
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("G/L Date")
            .Orientation = xlColumnField
            .Position = 1
        End With
        ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("Total Hours"), "Count of Total Hours", xlCount
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Total Hours")
            .Caption = "Sum of Total Hours"
            .Function = xlSum
        End With
        Range("B4").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Rows("4:4").Select
        Range("XEF4").Activate
        Selection.NumberFormat = "d-mmm-yy"
        Range("B5").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Style = "Comma"
        Range("Q5").Select
    End Sub

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to make a recorded macro work properly

    Like this portion
    Range("B5").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Style = "Comma"
    can be replaced by

    Dim lrow as long 'You can add this line at the top with your dim statements
    lrow = range("B" & rows.count).end(xlup).row
    range("B5:B" & lrow).Style = "Comma"

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to make a recorded macro work properly

    Are you getting any error or do you just want to make it more efficient?

  6. #6
    Registered User
    Join Date
    08-22-2012
    Location
    Bridgend, Wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Trying to make a recorded macro work properly

    Hi Arlette

    During my search for a fix I have seen that this recording is far from efficient, so in answer to your question, both please.

    My original error was a "Run Time Error 9: Subscript out of range" which in my very limited knowledge I thought is due to it was referencing the name of the sheet in which I created the macro.

    I suppose I have a number of issues;

    next to no knowledge of macros
    I want the macro to run in the active worksheet no matter what it's named
    I want the macro to rename the data sheet and pivot sheet as per my original post
    I would like to be as efficient as possible so I can learn from it for the future
    I don't know where to start

  7. #7
    Registered User
    Join Date
    08-22-2012
    Location
    Bridgend, Wales
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Trying to make a recorded macro work properly

    Thanks to all who contributed here. I eventually solved this through trial and error, and referencing lots of www sites, so thanks to all the contributors and sources out there; I couldn't have done it without you.

+ 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