+ Reply to Thread
Results 1 to 11 of 11

Need help with VBA copying selected range of row data

  1. #1
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192

    Need help with VBA copying selected range of row data

    Hi to all,

    I'm very new to VBA and I'm trying to make a code to copy the data to another worksheet/workbook last row.

    There are 2 problem that I face.

    1st problem is that it also triggers when I uses my up down keys which I do not understand why. I only want it to trigger when I uses my mouse to click on the text as i set the condition as if target.text

    2nd problem is that sometime it will not copy properly, like when i click within the cell box that contain the text, it will trigger and only copy that 1 cell itself, similar to using the up down key as it also copy only that 1 cell.


    Can some one assist me on my code. Greatly appreciate any kind of help.

    I have attached an example that I tried. Sheet 3 "print" text are the macros. Pressing it will paste to Sheet 2.

    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need help with VBA copying selected range of row data

    You have event code in Sheet3 code module that will call Macro3 code in code Module2 if the target cell = "Print". It then will copy whatever is selected to the next available row, based on column A content, of Sheet2.

    What do you want it to do? Do you want it to copy the entire row or just certain range of cells?
    Last edited by JLGWhiz; 01-26-2018 at 06:19 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192
    Quote Originally Posted by JLGWhiz View Post
    You have event code in Sheet3 code module that will call Macro3 code in code Module2 if the target cell = "Print". It then will copy whatever is selected to the next available row, based on column A content, of Sheet2.

    What do you want it to do? Do you want it to copy the entire row or just certain range of cells?

    Hi JLGWhiz,
    Yes thats right. Right now as attached above, it is working the way I wanted as click on the print text, it will copy the row base on my formula on the print text cell which i'm using hyperlink reference to highlight those cell(it should copy the same row of the print text that you clicked. Not sure how to code it in vba so i uses hyperlink reference) and run macro for the copying and pasting steps.

    But it is very buggy which i mention about on the 2 problem I face. Like the keyboard up down left right button between the cell with print text will trigger it. And also it triggers when you click on the CELL with the print text instead of the mouse hovering to the print text word clicking on it which resulted the copying of rows will only copy that particular cell itself which is the print text word. Thus ended up pasting the print text word instead of the row data into sheet2.
    Last edited by finalazy; 01-27-2018 at 02:02 AM.

  4. #4
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192

    Re: Need help with VBA copying selected range of row data

    I think it is due to wrong vba code used or the hyperlink issue. Could anyone enlighten or assist me on this. Thanks!

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need help with VBA copying selected range of row data

    Please Login or Register  to view this content.
    The reason it is only copying the single cell when you click on that cell is because the copy macro uses 'Selection' as the range reference. So it is copying the single cell that you select. I am not sure how to resolve your problem, because what you describe as your procedure seems to be erratic.

  6. #6
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192

    Re: Need help with VBA copying selected range of row data

    Hi JLGWhiz,

    Thanks for the explanation. I see the issue with the selection.copy.

    Is there any way to copy the row of the print text that is being clicked so that it copys that row of selection.
    Example if I choose to click on print text at column D7, i want to copy the data at row 7.

    I'm currently using hyperlink reference as an alternative because i'm not sure how to code for VBA. If a VBA code can be written, I will remove that hyperlink reference.
    Appreciate the help!
    Last edited by finalazy; 01-27-2018 at 01:31 PM.

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need help with VBA copying selected range of row data

    If you always want to copy the entire row then in your Macro3 code chang
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    If you do that, then you will not be able to copy just the selection of anything less that a full row.

  8. #8
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192

    Re: Need help with VBA copying selected range of row data

    Quote Originally Posted by JLGWhiz View Post
    If you always want to copy the entire row then in your Macro3 code chang
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    If you do that, then you will not be able to copy just the selection of anything less that a full row.

    Hi JLGwhiz,

    Thanks for quick reply and help! It's slightly better now.

    Just 1 more question, can I know is there anyway to prevent it from triggering when using keyboard up down left right key around the print text cell or clicking on the cell? Can I make it to trigger only when clicking on the word itself.
    Last edited by finalazy; 01-27-2018 at 01:57 PM.

  9. #9
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need help with VBA copying selected range of row data

    Quote Originally Posted by finalazy View Post
    Hi JLGwhiz,

    Thanks for quick reply and help! It's slightly better now.

    Just 1 more question, can I know is there anyway to prevent it from triggering when using keyboard up down left right key around the print text cell or clicking on the cell? Can I make it to trigger only when clicking on the word itself.
    You are using the 'Worksheet_SelectionChange' event to trigger the macros when the target (selected cell) = "Print". If you are using the arrow keys to maneuver the pointer, then anytime you move to a cell that equals "Print", you are going to trigger the code. The only way I know to not trigger it is to avoid those cells which contain the word "Print".

  10. #10
    Forum Contributor
    Join Date
    07-06-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2016
    Posts
    192

    Re: Need help with VBA copying selected range of row data

    Quote Originally Posted by JLGWhiz View Post
    You are using the 'Worksheet_SelectionChange' event to trigger the macros when the target (selected cell) = "Print". If you are using the arrow keys to maneuver the pointer, then anytime you move to a cell that equals "Print", you are going to trigger the code. The only way I know to not trigger it is to avoid those cells which contain the word "Print".
    Hi JLGWhiz,

    Once again thanks for the clear explaination. I will use other method instead of Worksheet_SelectionChange. Thanks for the guidance and I appreciate it.

  11. #11
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need help with VBA copying selected range of row data

    Quote Originally Posted by finalazy View Post
    Hi JLGWhiz,

    Once again thanks for the clear explaination. I will use other method instead of Worksheet_SelectionChange. Thanks for the guidance and I appreciate it.
    Happy to assist,
    regards, JLG

+ 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. [SOLVED] Copying Selected rows and columns of data into a new spreadsheet.
    By Coems in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-14-2015, 06:21 PM
  2. VBA Macro - Copying entire row from selected cell (within a pre-defined range)
    By ginja in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2014, 09:34 AM
  3. Copying & Pasting with Only User Selected Data
    By gimgandi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2013, 10:46 PM
  4. Replies: 2
    Last Post: 06-28-2012, 03:15 AM
  5. IFERROR copying row data for selected values
    By Danimarie in forum Excel General
    Replies: 3
    Last Post: 02-14-2012, 06:27 AM
  6. VB for copying a selected range
    By John McCann in forum Excel General
    Replies: 1
    Last Post: 12-01-2011, 01:03 PM
  7. Replies: 14
    Last Post: 10-23-2011, 09:49 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