+ Reply to Thread
Results 1 to 7 of 7

VBA - Selecting a cell based on relative position vs cell #

  1. #1
    Registered User
    Join Date
    05-07-2007
    Posts
    4

    VBA - Selecting a cell based on relative position vs cell #

    Hello,
    I am creating a macro in VBA, but I'm not a great programmer. I usually use the 'record' function and tweak the code to make edits. I would appreciate any and all advice/help!

    I am trying to select the last two cells in a row and copy them to another sheet. When I record this macro it shows up as:

    << Range("A2").Select
    Selection.End(xlToRight).Select
    Range("P2:Q2").Select
    Selection.Copy>>

    The problem with this is that it isn't sustainable. The most recent data (the cells I want to copy) might not always be in cells p2 and q2. Ultimately I just want to copy the LAST TWO CELLS (furthest to the right), regardless of their position. Is there a code to do this?
    Thanks!
    Last edited by jago_ML; 05-07-2007 at 01:09 PM.

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    Try with this code:
    Please Login or Register  to view this content.
    Offset(row, column) move the cell selection, Resize (rows, columns) modify the dimension of selection.

    Regards,
    Antonio

  3. #3
    Registered User
    Join Date
    05-07-2007
    Posts
    4

    Thank you!

    Wow, thank you. It worked, I'm not sure how, but it sure worked!
    I've never seen 'my row' and 'mylastcolumn'. How does that work exactly?
    Cool stuff. Thanks again

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    jago_ML,

    myRow and myLastColumn are actually variables. They are not functions.


    Please Login or Register  to view this content.
    This is simply assigning the value of 1 to the variable myRow

    Please Login or Register  to view this content.
    This is finding the last column with information in it on Row 1 (myRow) and assigning the value to the variable myLastColumn.

    The is very useful when doing a loop such as

    Please Login or Register  to view this content.
    Each time the code loops myRow is incremented by 1 in this particular example.

    Hopes this help you understand things a little better!
    Sincerely,
    Jeff

  5. #5
    Registered User
    Join Date
    05-07-2007
    Posts
    4

    hmmm

    Hey guys,
    thanks for the explanations, I understand the concept.
    I am having trouble, however, executing the code on the data that I have.

    If you have a sec, check out the workbook attached. I am trying to pull the last 2 quarters of sales data (on sheet 'IS' in row 2) onto 'Sheet 1', and I'm struggling.

    Again, thanks for the continued help.
    Attached Files Attached Files

  6. #6
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Try this and see if it gives you what you are wanting.

    Please Login or Register  to view this content.
    I was not sure where you want the data placed on Sheet1 so I put it in the first two cells on row 1. You can easily change that by editing
    Please Login or Register  to view this content.
    Hope this helps you move forward with what you are doing.

  7. #7
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    See the attached file... now it would do what u need.

    I'm sorry but in my previous code I didn't put the source sheet where take data to copy:
    Please Login or Register  to view this content.
    Regards,
    Antonio
    Attached Files Attached Files

+ 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