+ Reply to Thread
Results 1 to 24 of 24

Variable Range

  1. #1
    Registered User
    Join Date
    03-21-2012
    Location
    waterford
    MS-Off Ver
    Excel 2003
    Posts
    5

    Variable Range

    Hi
    I am trying to write code to select a range of cells, copy and paste to sheet2
    In my sheet i have a button that adds a line copying the format of the cells above
    i need to be able to select all the text in column A down as far as the add Line button but not including the is cell
    I have the code to select all as seen below. How can i select all the text from A3 to the cell just above the cell named "LastCell"

    Please Login or Register  to view this content.
    Last edited by arlu1201; 03-22-2012 at 07:13 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hi Lynn81,

    I think you need a line of code that I use very often.

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    The above line of code will return a number of the last row with data in it, in column A. Then I believe you want LastRow = LastRow - 1?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    03-21-2012
    Location
    waterford
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Variable Range

    Hi Marvin
    That might work if there wasnt data under the Add line button there is writing in the cell which is needed for other Macros. so it still selects this cell
    I need to be able to select the range from one above the cell named "LastCell"

    if i use Range("A3", Range("LastCell").cell-1).Select
    i get an error
    ive tried all i can think of and cant find a simple way of doing it

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    How about this one then

    LastRow = Cells(3, "A").End(xlDown).Row

    Which will Start in Cell A3 and do a Ctrl+Down to find the last cell before a blank in column A.

  5. #5
    Registered User
    Join Date
    03-21-2012
    Location
    waterford
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Variable Range

    Thanks Martin
    Same problem the cell i want to omitt will have text in it so wont be blank
    and because the range is variable i need name the last cell to find where to stop

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hi Lynn,

    I guess I need to see a sample worksheet to see why you are having this problem. You can attach one by going to the Advanced Message type and clicking on the Paper Clip Icon above the message area.

    Sorry, I just don't understand what your need is.

  7. #7
    Registered User
    Join Date
    03-21-2012
    Location
    waterford
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Variable Range

    Sorry for not explaing it properly i have attached the sample spread sheet i am testing the code on
    so you might be able to see what i mean. i know its probably something simple but i just cant get it to work
    Thanks
    Attached Files Attached Files

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hi lynn81,

    I'm not sure I understand the question still. I've passed this on to Arlu1201, our Head Guru for a look. They will get back to you shortly.

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Variable Range

    Lynn81,

    Try this code -
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hi,

    Now that Arlette has solved your question (I think?)

    I think you need to use Range("LastCell").Row to get the row you want. Is that correct? Did Arlette's code help? I don't understand why s/he made the variables global. Arlette?

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Variable Range

    I think you need to use Range("LastCell").Row to get the row you want.
    You do not need to use this. I've drafted the code upto the copy part.So the range required is already selected. Just specify the location where the data has to be pasted.

    Global variables is out of habit. You can even move the dim statements below the sub copy_data() line.

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

    Re: Variable Range

    I'missing something. From the example workbook
    I Need to be able to select from A3 down to the cell above the button "ADD LINE" which can vary
    My code at the moment for the copy and move picks up all the cells from A3 to
    the cell under the button called "LastCell"
    You cannot use a button to mark cells really.

    I can't see where Arlu is getting "End" from, it's not in the sample data. Much more efficient code if we are looking for End is to automate AutoFilter. Also, for efficiency it's unnecessary to select the cells.

    Attach a better example
    Hope that helps.

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

    Free DataBaseForm example

  13. #13
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Variable Range

    If you move the button aside, you will find the word "End" which is also a named range "Lastcell".

  14. #14
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hi Arlette,

    I really think my suggestion might help Lynn81 understand her code a little better. I'm a little hurt with the suggestion that she doesn't need my suggestion.

    I'm studying your code and learning a lot from it. Thanks for showing how you'd do this problem.

  15. #15
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: Variable Range

    Arlette, why on earth would you loop through all the cells to find the supposed "End"? surely using the VBA find is both quicker and more efficient?

    @Marvin, i feel for you, good advice cast aside!

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

    Re: Variable Range

    I still wouldn't bother with Looping.

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Variable Range

    Simon,

    Find can surely be used. There are multiple ways to get the same solution. I just suggested a code i was comfortable with. The user is free to use it or any other code provided by any other user.

  18. #18
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: Variable Range

    Looping is expensive on resources, Selecting is a hinderance to code and uses resources unecessarily, as the range get's larger your suggestion wil get slower and slower. I would use RoyUk's code or
    Please Login or Register  to view this content.

  19. #19
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hey Simon,

    I'm always ready to learn from reading other people's code. I learned a lot from reading Arlette's. S/he showed me how to create global variables, instead of locals, that I'm not sure are good or needed for this example. I also saw a jump out of a loop based on a condition. I'm not sure that is good either. Many times I simply miss what the OP is asking for. The question of a named range of "LastCell" was mentioned twice. I did see the "End" under the button (sorry Roy) but don't see how that addressed the named range question.

    The bottom line is that the OP learns something and gets better at understanding Excel and its possibilities.
    Thanks for the support.

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

    Re: Variable Range

    I haven't use End, I used the named Range. Arlette's code looped until it found "End" - OK on small data sets but not efficient for large amounts of data.

    I didn't notice the text hidden behind the button at first. The OP had a Named range so it's best to use that.
    Last edited by royUK; 03-22-2012 at 11:45 AM.

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Variable Range

    Thanks Roy n Simon. A learning for me too.

  22. #22
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: Variable Range

    Quote Originally Posted by MarvinP View Post
    Hey Simon,

    I'm always ready to learn from reading other people's code. I learned a lot from reading Arlette's. S/he showed me how to create global variables, instead of locals, that I'm not sure are good or needed for this example. I also saw a jump out of a loop based on a condition. I'm not sure that is good either. Many times I simply miss what the OP is asking for. The question of a named range of "LastCell" was mentioned twice. I did see the "End" under the button (sorry Roy) but don't see how that addressed the named range question.

    The bottom line is that the OP learns something and gets better at understanding Excel and its possibilities.
    Thanks for the support.
    Marvin, your lucky to have those skills passed on to you, you'll be creating spaghettti code in no time at all
    Not all forums are the same - seek and you shall find

  23. #23
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Variable Range

    Hey Simon,

    I didn't say I was learning "good" things, just learning how others attack problems, give solutions, and reject other's suggestions. I've written enough spaghetti code to see it in action and shy away from it. It's always fun to see others who travel that path and get lost a few weeks later. It also allows me to see how accomplished programmers are by what they write.

  24. #24
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Re: Variable Range

    Marvin, when i wrote that i felt a serious lump in my cheek

+ 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.6.0 RC 1