Results 1 to 7 of 7

Thread: Novice Macro Amendment Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    2

    Novice Macro Amendment Question

    Hi All,

    I am wondering if someone might be able to help me.

    I recorded a macro in Excel, it contains the following text:

    Range("C2:C10").Select
    Selection.Copy

    However when it comes to running the macro, I may need to copy more than just C2 to C10, it might be C2 to C20 or even more.

    How can I adjust the code to accommodate this? Maybe it involves some kind of code that counts how many lines in column C are populated?

    If it helps, no more rows in C will be populated except the ones I need to copy.

    Thanks so much in advance.

  2. #2
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Novice Macro Amendment Question

    Hmm, macro recording is a great way to learn some code on parts of excel lets say that your not familiar with, but it is NOT a place to start learning VBA or any other language for that matter.

    i recommend learning some basic principles with vba and this problem you have would be pretty simple to fix.

    there is about 20 ways you can do what your asking so im not sure what to tell you. if you have a changing amount of rows that you need to copy then try recording this.

    Select the top cell in the row
    press ctrl shift and down
    then press ctrl c to copy
    and see what code that gives you

    This will select everything in the column you first selected and also select every row below that until a blank cell is encountered.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2021
    Posts
    2

    Re: Novice Macro Amendment Question

    Quote Originally Posted by GBeats View Post
    Hmm, macro recording is a great way to learn some code on parts of excel lets say that your not familiar with, but it is NOT a place to start learning VBA or any other language for that matter.

    i recommend learning some basic principles with vba and this problem you have would be pretty simple to fix.

    there is about 20 ways you can do what your asking so im not sure what to tell you. if you have a changing amount of rows that you need to copy then try recording this.

    Select the top cell in the row
    press ctrl shift and down
    then press ctrl c to copy
    and see what code that gives you

    This will select everything in the column you first selected and also select every row below that until a blank cell is encountered.
    Thanks, that's exactly what I did when I originally recorded the macro and it's put in C2:C10 (which is the number of rows that were populated when I was doing the example) - problem is that when I run the macro the number of rows can vary, so I wanted to code it to take that into account.

  4. #4

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Novice Macro Amendment Question

    try like
    Code:
    lastrow = cells(rows.count, 3).end(xlup).row
    range("c2").resize(,lastrow -1).copy
    no need to select the cells to copy
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Novice Macro Amendment Question

    Quote Originally Posted by DanLewis View Post
    Thanks, that's exactly what I did when I originally recorded the macro and it's put in C2:C10 (which is the number of rows that were populated when I was doing the example) - problem is that when I run the macro the number of rows can vary, so I wanted to code it to take that into account.
    Both the posts (i didnt check the links but im sure they are good) should be good, the example i also left is basically be the same as what 'westconn1' said (he left the actual way we would code it) this should work from the situation you provided.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  7. #7

    Re: Novice Macro Amendment Question

    Select the top cell in the row
    press ctrl shift and down
    then press ctrl c to copy
    and see what code that gives you

    This will select everything in the column you first selected and also select every row below that until a blank cell is encountered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width