+ Reply to Thread
Results 1 to 19 of 19

Copy and paste selective rows of data from a user prompt

  1. #1
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Copy and paste selective rows of data from a user prompt

    I have search this forum for macros / VBA but I cannot find a close similarity with what I need. Here is my scenario:

    In my first column are dates. Now, I need a button to run a macro and this is what the macro needs to do:
    1. Opens a message box prompting for the date he wants to COPY. The date must be from the ones listed in the first column. It can be in a form of FROM and TO selection.
    2. The FROM and TO selection shall show the dates in the first column so that the user cannot enter an invalid date, like a Data Validation.
    3. When the user selects the FROM and TO dates, the marco then copies the contents of cell ranges (specified in the FROM and TO selection) in columns C through D and pastes the data into columns I through J, also copies (in the same rows) the contents in columns E through F and pastes into L through M.

    Here is a screen shot:
    http://filesxpress.com/d-72c45cb2

    In the screen shot above, if the user selects FROM as March 4, 2016 (A5) and TO as March 10, 2016 (A11), then the macro copies the range C5:D11 and pastes to I5:J11, copies E5:F11 and pastes to L5:M11.

    The file is attached below.

    http://filesxpress.com/d-cf2c89dd

    I tried attaching the file but the attachment button is not working. I hope the links work.

    Thank you for your help.

  2. #2
    Registered User
    Join Date
    01-12-2015
    Location
    Lisboa
    MS-Off Ver
    2013
    Posts
    8

    Re: Copy and paste selective rows of data from a user prompt

    hope this helps you, it does assume everything is correct...if u want some testing, just drop some ifs

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-12-2015
    Location
    Lisboa
    MS-Off Ver
    2013
    Posts
    8

    Re: Copy and paste selective rows of data from a user prompt

    you need the other columns also...so add

    Please Login or Register  to view this content.

    inside the IF next to the other "Range" line.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    Here is another option:
    it is a file for download since I too can't get the attachment button to work.
    http://filesxpress.com/d-ec68c319

    This version uses a simple userform with only dates contained in the sheet available for selection. It also makes the end date the same as the start date if it is not specified.
    Last edited by Arkadi; 03-09-2016 at 12:42 PM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Registered User
    Join Date
    01-12-2015
    Location
    Lisboa
    MS-Off Ver
    2013
    Posts
    8

    Re: Copy and paste selective rows of data from a user prompt

    Quote Originally Posted by Arkadi View Post
    Here is another option:
    it is a file for download since I too can't get the attachment button to work.
    http://filesxpress.com/d-ec68c319

    This version uses a simple userform with only dates contained in the sheet available for selection. It also makes the end date the same as the start date if it is not specified.
    Nice form.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    Thanks zaonpt, just threw a basic one together, usually they look nicer.

  7. #7
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Quote Originally Posted by zaonpt View Post
    you need the other columns also...so add

    Please Login or Register  to view this content.

    inside the IF next to the other "Range" line.
    Hi, thank you for your input. It is working and it is simple. Though the entry for the FROM and TO dates are manual, I can follow your logic easily! Thank you very much for your help.

  8. #8
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Quote Originally Posted by Arkadi View Post
    Here is another option:
    it is a file for download since I too can't get the attachment button to work.
    http://filesxpress.com/d-ec68c319

    This version uses a simple userform with only dates contained in the sheet available for selection. It also makes the end date the same as the start date if it is not specified.
    Thank you very much for the solution. The form is indeed very nice and correct to the point that the user can pick the FROM and TO date as required.
    However, in my actual work file (not the same as the sample file I uploaded or shared), I just copied (export and import) your FORM to my work file, copied the Module1 code, but when I click on the button, the Initialize part of the code gave an error:

    Compile error: User-defined type not defined.

    Maybe because my ACTUAL date column is in B12 to B42? And my dates are actually formulas and not plain dates? Please advise

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    A few things....

    Open VBA editor (alt-F11)
    At the top click Tools, then select "references" and scroll down to Microsofr ActiveX Data Objects 6.1 Library.... put checkmark in the box next to that ...
    then:
    under forms you will see "frm_dateselect", click it and double-click the copy button... it will bring up the code.
    edit this:
    Please Login or Register  to view this content.
    changing "Sheet1" to the name of your real sheet.
    also change:
    Please Login or Register  to view this content.
    changing the "A" to "B" if that is your date column.
    That fixes the CommandButton1 code... scroll down to the UserForm_Initialize code and look for this line:
    Please Login or Register  to view this content.
    You need to change Sheet1 to the real sheet name, and the word Date is the column heading, so if your date column is still called Date then you are fine, otherwise change it to the header for the date column.

  10. #10
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Thank you for the quick reply. After the edits, the Compile error was gone. However, I got a runtime error:
    http://filesxpress.com/d-4e6b35bb

    When I click debug, Module1 code is shown and frm_dateselect.Show is highlighted in yellow.
    Is this because my column B has no header?

  11. #11
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    Yes that would be why...
    if you have no headers then change the myConn.Open code to:
    Please Login or Register  to view this content.
    (the change here is just HDR=NO)

    Also then you need to change the datelist.open line of code to:
    Please Login or Register  to view this content.
    F2 means Field 2 which is the second column, also remember to change Sheet1 to your sheet name.

  12. #12
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Thank you.... The form indeed showed up, I was able to select the FROM and TO dates. But clicking COPY gave an error:

    Please Login or Register  to view this content.
    When I click Debug, the line highlighted in yellow is:

    Please Login or Register  to view this content.

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    Did it copy ANY rows? or just failed? Also, are there rows with no date in the middle of the data? and what is the first row with data?

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    In the commandbutton code lets change the i = 2 to lr loop. 2 is the first row it checks... forgot to mention you may need to update that. But also change the code to this:

    Please Login or Register  to view this content.
    it will avoid blanks and non-dates

  15. #15
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Nothing was copied, it just failed.
    There are no blank rows in the middle.
    The first row with data is B12

  16. #16
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Thank you. It is now working but the data copied are wrong and the data are pasted in the wrong places. Maybe because of the actual structure of the data. I believe you can take a look at the actual file.

    My goal is to copy the columns D through L and paste to AJ to AR and then copy columns X through AH and paste to AT through BD, starting from the row in START date and ending in the row in END DATE.

    http://filesxpress.com/d-bcd7c40e

    Thank you very much.

  17. #17
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    I believe the problem with copying the data is that you are reading the code with the logic of "copy from a to b" where as my code's logic is b=a.... so try these 2 lines in the for loop on the commandbutton code:

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    04-25-2013
    Location
    Phils
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    46

    Re: Copy and paste selective rows of data from a user prompt

    Quote Originally Posted by Arkadi View Post
    I believe the problem with copying the data is that you are reading the code with the logic of "copy from a to b" where as my code's logic is b=a.... so try these 2 lines in the for loop on the commandbutton code:

    Please Login or Register  to view this content.
    Thank you!!!! It is now working!!! Thank you very much

  19. #19
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Copy and paste selective rows of data from a user prompt

    My pleasure

+ 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] Copy data from selective rows from different files to a single file
    By narikisas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2014, 10:09 AM
  2. [SOLVED] selective copy and paste via command button vba
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-28-2014, 12:07 PM
  3. Add new row with user prompt and copy specific cells only
    By Backroads23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2012, 12:09 PM
  4. ** Selective Cut and Paste - Cutting and Pasting Rows if a Row has Key Word in Column
    By brad70989 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2011, 06:04 PM
  5. prompt user for 2 data choices and select and copy the data
    By Alfredt in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-11-2010, 03:03 PM
  6. copy / paste selective rows
    By Kenny Kendrena via OfficeKB.com in forum Excel General
    Replies: 5
    Last Post: 02-07-2005, 09:06 AM

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