+ Reply to Thread
Results 1 to 9 of 9

How to make a macro cell independent

Hybrid View

  1. #1
    Registered User
    Join Date
    10-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    How to make a macro cell independent

    Hi,

    I am new to macro recording and VBA. How do I make a macro cell independent? For example, I currently have:

    Range("K4").Value = Range("K4").Value - 1
    i.e. minus 1 from the current value in the cell K4.

    Say then I move the content of K4 to some new location, e.g. I've added a new row above so K4 has now been shifted to K5. How do I program it so that the macro automatically performs the original operation in the new arbitary location?

    Thanks for any help
    Last edited by Ads.; 10-17-2011 at 09:10 AM.

  2. #2
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: How to make a macro cell independent

    Hi Ads,

    When it comes to referencing a specific cells in VBA, it is recommended that we use named ranges instead. That way, adding in rows/columns cut/paste will not affect (to a certain degree) the VBA code.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to make a macro cell independent

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    ADDED THIS TIME
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How to make a macro cell independent

    is there data below thye relevant cell?

  5. #5
    Registered User
    Join Date
    10-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How to make a macro cell independent

    Yes. The macro is being applied to a cell in a table. So I need the macro to work if either a new row is added to the table, or the table is to moved to a different position in the spreadsheet (much like if you have a formula in a cell, the formula will still work identically if the cell is moved around)

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,972

    Re: How to make a macro cell independent

    Then you need a named range, as suggested, or some means of identifying the cell in question so your code can find it.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Registered User
    Join Date
    10-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How to make a macro cell independent

    Ok thanks. I will attempt that.

  8. #8
    Registered User
    Join Date
    10-17-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How to make a macro cell independent

    Marvellous. Just what I was after. Thanks quekbc and romperstomper.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,972

    Re: How to make a macro cell independent

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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