+ Reply to Thread
Results 1 to 6 of 6

Copying/Pasting cell ranges

  1. #1
    Registered User
    Join Date
    02-27-2018
    Location
    Scotland
    MS-Off Ver
    2016
    Posts
    3

    Copying/Pasting cell ranges

    I've only just starting learning VBA by creating a couple of simple macros and looking at the code it produces.
    Running the following code, as a macro, seems to work fine.

    I would really like to run this code from a button, however the same code pasted into the button doesn't pick up any values, so nothing is getting pasted. What am I doing wrong?

    Also, as I'm always going to be pasting the 2nd range of values 8 cells to the right on same row/worksheet, surely there would then be no need to ask for a starting point for where the next range is to be pasted? I'm sure my code sucks and could be written better, but I am a newbie at this

    Please Login or Register  to view this content.
    Also, I'm not really clear on what the difference is between Activate and Select

    Any help greatly appreciated

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copying/Pasting cell ranges

    Welcome to the forum

    There is no need to select if all you are doing is extracting or writing values
    - just refer to the sheet and range
    - selecting slows your code down

    The input boxes allow you to click on a cell to select destination, so activating correct sheet in advance is best here

    "Activate" a sheet (or workbook) and "Select" a range generally works (but it does depend on what you are doing)
    - activating moves the focus to the object
    - only one object can be active, but multiple objects can be selected

    Please Login or Register  to view this content.
    Use loops and avoid duplicating code:
    Please Login or Register  to view this content.
    You could condense your code like this:
    Please Login or Register  to view this content.
    Last edited by kev_; 02-28-2018 at 03:33 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Registered User
    Join Date
    02-27-2018
    Location
    Scotland
    MS-Off Ver
    2016
    Posts
    3

    Re: Copying/Pasting cell ranges

    Thankyou so much for the reply Kev - which works. Unfortunately, I've since realised the row being copied from can change also.
    To re-iterate - User will be copying data from TWO cell ranges from one worksheet and pasting them into another.
    Each range consists of 4 adjacent cells (on same row), but the 1st cell of range 2 is always 9 cells to the right of 1st cell of Range 1
    It is ONLY the ROWS where the data is copied from AND pasted to, that can change. So I assume macro would only need user input as to where the starting cells are for copying and pasting? i.e D3 and B5 respectively, for eg: copy from D3:G3 and M1:P1 (Report worksheet), then paste to B5:E5 and J5:M5 (numbers). Hope this is clear for anyone who might want to help further.
    Last edited by wandelust99; 03-01-2018 at 12:27 PM.

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copying/Pasting cell ranges

    So I assume macro would only need user input as to where the starting cells are for copying and pasting? i.e D3 and B5 respectively, for eg: copy from D3:G3 and M1:P1 (Report worksheet), then paste to B5:E5 and J5:M5 (numbers). Hope this is clear for anyone who might want to help further.
    msgbox.jpg

    Assuming that the offsets in both cases are consistent, this method should work for you
    As you can see it matches exactly what you asked for when D3 and B5 selected
    I added a function to return range addresses which you may find useful again

    Please Login or Register  to view this content.
    Last edited by kev_; 03-01-2018 at 01:50 PM.

  5. #5
    Registered User
    Join Date
    02-27-2018
    Location
    Scotland
    MS-Off Ver
    2016
    Posts
    3

    Re: Copying/Pasting cell ranges

    That's brilliant kev! Exactly what I was looking for. I will try take something from this. Thankyou so so much

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copying/Pasting cell ranges

    You are welcome.
    Please mark thread SOLVED
    (See thread tools @ top of thread)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying Mulitple Ranges, Pasting to body of Email
    By Master Blaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2012, 01:57 AM
  2. Copying and pasting Named (Dynamic) Ranges from one workbook to the other
    By Let in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-23-2012, 10:06 AM
  3. Beginner Macro Question - Copying and Pasting Ranges
    By sfmatt13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2012, 07:49 AM
  4. Copying and pasting named ranges
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-21-2012, 09:16 PM
  5. Copying/pasting cell value not formula
    By Dervlan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-28-2010, 02:42 PM
  6. Copying and pasting row based on the value in a cell
    By LDT in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-04-2010, 11:44 AM
  7. copying and pasting ranges
    By inkexit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2008, 10:11 PM
  8. Replies: 2
    Last Post: 10-14-2007, 01:05 PM

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