+ Reply to Thread
Results 1 to 2 of 2

Thread: Macro relative references

  1. #1
    Registered User
    Join Date
    10-26-2010
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2003
    Posts
    1

    Macro relative references

    I'm attempting create a macro that will add a new line below the line in which the user is, copies all data and formulas from the line above, except for columns G and H. Ends in Column G, ready for editing. I came up the macro below, which does what I want, if you are in row 3, but how do I generalize it.

    Another way to ask the question, how do I change the reference
    Rows("4:4").Select
    to something that means select the row below the one I'm in. e.g.
    Rows("the-one-I'm-in+1").Select

    Sub AddSecondProduct()
    '
    ' AddSecondProduct Macro
    
    '
        Rows("4:4").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("3:3").Select
        Selection.Copy
        Rows("4:4").Select
        ActiveSheet.Paste
        Range("G4:H4").Select
        Application.CutCopyMode = False
        Selection.ClearContents
        Range("G4").Select
    End Sub
    Any help appreciated.
    JensPeter
    Last edited by davesexcel; 10-27-2010 at 12:03 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007,2010
    Posts
    6,843

    Re: Macro relative references

    Maybe activecell would work, here is an example using activecell
    select a cell in column a and run the code

        ActiveCell.Range("A1:F1").Copy
    
        ActiveCell.Range("A1:E1").Insert Shift:=xlDown
        ActiveCell.Offset(1, 0).Range("A1").PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
                                                         SkipBlanks:=False, Transpose:=False
        ActiveCell.Range("G1").Select
        Application.CutCopyMode = False

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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