+ Reply to Thread
Results 1 to 5 of 5

Applying a macro over a range/selection

  1. #1
    Registered User
    Join Date
    05-27-2009
    Location
    Pune
    MS-Off Ver
    Excel 2007
    Posts
    5

    Applying a macro over a range/selection

    I'm trying to apply a macro over a cell/range. However, I'm clueless on how to utilize the range in my code. My current code for one cell is as follows:

    Please Login or Register  to view this content.
    What I'm doing here is populating an active cell with data that's contained in a file who's name I identify from the first column of the respective row that is active. This code works fine when I have just one cell selected.

    I wish to extend this functionality when I select a range of cells on the worksheet as well. I just selected a range and tried to run the macro, but it populated only the first (active) cell, which I guess is valid.

    My question is: What properties would let me access a selection/range in the above case? I also need to ensure that the selection is 1 column * multiple rows (that is, the range should only stack vertical where the column no. should be the same). I could do this if I get the first and the last cells in the range and checking if the column is same, but I'm not sure how this is done..

    PS: I tried looking at the documentation for Selection, Areas etc. but couldn't clearly understand what needs to be used.

    Any help would be appreciated.

    Thanks in advance

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Applying a macro over a range/selection

    Untested, of course, but this is the kind of FOR/NEXT loop you will need:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Applying a macro over a range/selection

    Hello HydroXidE1, and welcome to the forum.

    I haven't tested this as I don't have your workbook or test txt files, but try this on a backup workbook/txt file:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-27-2009
    Location
    Pune
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Applying a macro over a range/selection

    Hi Paul,
    Thanks for welcoming me..
    And your code worked just fine! .. thanks again..

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Applying a macro over a range/selection

    If that takes care of your need, be sure to EDIT your original post (Go Advanced) and mark the PREFIX box [SOLVED].




    (Also, use the "scales" icon in our posts to leave Feedback, it is appreciated)

+ 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