Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recorded macro in "Personal.xlsb" only works in original spreadsheet

    Hello all,
    I have recorded a simple macro in which 3 columns are converted into a chart. The macro works fine but only in the spreadsheet which it was created in. When I try to run it in a different spreadsheet it comes back with a "Run-time error '1004': Method 'Range' of object '_Global' failed". When I run the macro debug it always highlights the same row, obviously, which is the text in red below. I would like to be able to run this macro in any spreadsheet which will have similar data but more/less rows with the starting cell always being B2 but ending cell changing depending how many rows there are, D*. I would also like to make the macro run at "open" but I won't even attempt that until this is resolved. Any help would be greatly appreciated.
    Here is the recorded macro code:


    Sub lineChart()
    '
    ' lineChart Macro
    '
    ' Keyboard Shortcut: Ctrl+o
    '
    Columns("B:B").Select
    Selection.NumberFormat = "h:mm:ss;@"
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range( _
    "'test 2012-0702-1136-31'!$B$2:$D$11546")

    ActiveChart.ChartType = xlLine
    Range("F8").Select
    End Sub

    I've also attached a word doc of the code.
    Thank You
    az
    Attached Files Attached Files
    Last edited by AdamZ; 2012-07-02 at 14:35. Reason: More information added to original post

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Adam,

    This is only AIR code but you might want to try something along the lines of:
    Code:
    Sub lineChart()
    '
    ' lineChart Macro
    '
    ' Keyboard Shortcut: Ctrl+o
    '
        Columns("B:B").Select
        Selection.NumberFormat = "h:mm:ss;@"
        Range("B2").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.Shapes.AddChart.Select
    
        With ActiveChart
           .SetSourceData Source:=Range(Selection.Address(,A1))
               .ChartType = xlLine
        End With
    
        Range("F8").Select
    
    End Sub
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    New Lounger
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank You for a quick response and please excuse my ignorance. I will have to do a little research on the Selection.Address function as I have no idea how to use it. Am I supposed to include individual cell addresses or a range in the parenthesis after the Address?
    thanks
    az

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Adam,

    Not to worry that's why the lounge is here.

    Actually Address is a property in this case of the Selection Object. So it returns the Address of the current selection on the worksheet. The arguments in the parenthesis following .Address tell .Address how to work.
    In the case above we don't need the 1st or 2nd arguments but I used commas to hold their place. The third argument tells .Address how to return the address. In this case I wanted a standard A1 style address so I used the constant xlA1 {looking back I see I erred in the AIR code using only A1}.

    Property Address([RowAbsolute], [ColumnAbsolute], [ReferenceStyle As XlReferenceStyle = xlA1], [External], [RelativeTo]) As String
    SelectionObject.JPG
    The other reference style is xlR1C1 for Row/Column type references.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •