+ Reply to Thread
Results 1 to 16 of 16

Copy and paste entire row to second sheet based on cell value

  1. #1
    Registered User
    Join Date
    04-13-2010
    Location
    Acle, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Copy and paste entire row to second sheet based on cell value

    Hi all,

    I am struggling to create a macro that will copy multiple rows of data from one sheet to another based on cell value in a given column.

    When data is pasted into the Data tab I then want the macro to copy each row based on the value in column A which will then populate into the corresponding named tab. Is this possible or should I be looking at a different way?

    I have added a macro to each tab to reflect when no data is currently entered into the sheet.

    Any help would be much appreciated.

    Thanks

    Darren
    Attached Files Attached Files

  2. #2
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Hi Darren,
    . You got lucky, I was just off to Bed when I caught this Thread.
    . I have answered loads of these sorts…



    . I adapted a code quickly to your case..

    Code:

    Please Login or Register  to view this content.


    …. See next Post as my code used up all the Post space ‘ere!!!!
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  3. #3
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    .. So if this would be a bit of your DATA sheet

    Using Excel 2007
    -
    A
    B
    C
    D
    1
    Service area
    MP
    MP
    Process No
    2
    Housing Bilbo Baggins
    5362462
    3
    Finance Bilbo Baggins
    5380289
    4
    Unallocated Bilbo Baggins
    5397568
    5
    Housing Clark Kent
    5398660
    6
    Local neighbourhood services Bruce Wayne
    5408944
    7
    Business relationship management Bilbo Baggins
    5439148
    DATA

    ..


    Then after running that code this would be a bit of one of the sheets it PRODUCES**



    Using Excel 2007
    -
    A
    B
    C
    D
    E
    F
    1
    2
    Service area
    MP
    MP
    Process No
    Start date
    working days old
    3
    Housing Bilbo Baggins
    5362462
    13-Feb-2015
    66
    4
    Housing Clark Kent
    5398660
    04-Mrz-2015
    53
    5
    Housing Bilbo Baggins
    5454435
    31-Mrz-2015
    34
    6
    Housing Bilbo Baggins
    5454472
    31-Mrz-2015
    34
    7
    Housing Bilbo Baggins
    5483692
    20-Apr-2015
    20
    8
    Housing Bruce Wayne
    5485561
    21-Apr-2015
    19
    Housing

    ** Note it starts by deleting all the sheets. Then it makes them fresh. We / You / me can change that a bit as well as any other little Format niceties later..

    .. I will drop off the working file for you to play with for now (Code is in Sheet DATA module). See How you get on and maybe catch you sometime tomorrow

    Gute Nacht

    Alan

    ( An Englishman stuck in Bavaria )

    P.s. 1
    Quote Originally Posted by SilverMonkey View Post
    …….

    I have added a macro to each tab to reflect when no data is currently entered into the sheet.
    …….

    . ----I found no macro from you in the File you sent??


    P.s .

    . If you want some more detail on the development and explanations of the code as well as a few other variation possibilities then see here for example..
    http://www.excelforum.com/excel-prog...ml#post4012679
    http://www.mrexcel.com/forum/excel-q...e-value-3.html
    http://www.mrexcel.com/forum/excel-q...-column-8.html
    http://www.mrexcel.com/forum/excel-q...fic-value.html
    http://www.mrexcel.com/forum/excel-q...fic-value.html
    http://www.mrexcel.com/forum/excel-q...ontents-2.html
    http://www.mrexcel.com/forum/excel-q...ml#post3981936
    http://www.mrexcel.com/forum/excel-q...ll-column.html
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    04-13-2010
    Location
    Acle, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Copy and paste entire row to second sheet based on cell value

    Thanks Alan!

    That would have taken me an eternity to work out for myself.

    Would there be anyway to drop the reference column A when it is pasted into the new sheet and used as the title in the empty 1st row?

    Housing
    MP MP Process Start Date
    Bilbo Baggins 5362462 13-Feb-2015
    Clark Kent 5398660 04-Mar-2015
    Bilbo Baggins 5454435 31-Mar-2015
    Bilbo Baggins 5454472 31-Mar-2015
    Housing

    I have also noticed that when there is no data in a newly created sheet then the tab name stays as Sheet# could this be updated to show the missing sheet name or changed in a way so that I can use a list of Names that are responsible for the data in the tab.

    The code I had used in each of the tabs was as below which would turn the tab red if there was no data in cell A3 but would default to no colour if there was data in the tab.

    Please Login or Register  to view this content.
    Any help or advice much appreciated.

    Thanks

    Darren

  5. #5
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Hi Darren,
    . Yeh , took me about a week the first time I wrote it!, but mostly now I just have to modify it a bit, I am surprised how often something like this is done…

    .. Anyways..

    . The first bit of what you asked for is fairly simple..Your screen sjots made it fairly clear what you want..
    . So To get the results you showed in your screen shot In Post #4 .. this would be the code , with the mods shown in Purple
    . I tried it out – seems to work..

    Code 2:

    Please Login or Register  to view this content.
    … the rest , I am not too sure.. I did not quite understand what you wanted.. I’m off now… maybe try to explain that the other stuff again and I will take another look tomorrow

    Alan

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Copy and paste entire row to second sheet based on cell value

    Hi..

    Here's another..

    Note: This assumes you only have the "Data" sheet present.. the code will create the others..

    Also notice how any sheet that had a "Service Area" value that was over the 31 sheet name character limit has been cut down to 31 characters..

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by apo; 05-22-2015 at 12:28 AM.

  7. #7
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Quote Originally Posted by apo View Post
    Hi..

    Here's another.........
    .......

    Hi apo

    Very nice and extremely helpful ### contribution, apo.
    . Code Works ( - Of course, yours always do!). – very rich in interesting Functions / Methods….
    . I think I understand the very basic idea, Using the Microsoft Runtime Scripting directory to get the unique values ( Sheet / Tab Titles in this case. ).
    . Unfortunately the “With / End With” s are the total “Killer” for me when it comes to trying to understand it… - The usual “Step through with F8 along with putting watches in most variables” type technique just doesn’t work. – here I cannot even get past the first z variable. It appears to be empty the entire time the code runs… but if I comment it out the code doesn’t work??. But that is just the start..
    . With / Ends are of course the compact Professional way to do it, cuts out a lot of unnecessary variable definition, I appreciate that. Best in the long run I guess..
    . So I have a brilliant code for me to add to my collection for answering these sort of Threads.. I will not use it thought until I “earn the privilege” by getting to understand it.. (But I would probably use it in it’s opened up” form.. without those With/ End Withs.. and “ruin it with my 'graffiti green comments…”
    ….(. apologies in advance for when I ruin it later with my interpretation what is almost certainly the real Profi Code..)

    …. Thanks for helping to make it a nice Thread.



    AND:----###


    Quote Originally Posted by apo View Post
    ............
    Also notice how any sheet that had a "Service Area" value that was over the 31 sheet name character limit has been cut down to 31 characters..
    .......




    .1. ) Well spotted that two sheet names were too big. I stupidly missed that. Probably explains one of the OPs points in Post #4 that I did not understand last night :. …
    Quote Originally Posted by SilverMonkey View Post
    …..also noticed that when there is no data in a newly created sheet then the tab name stays as Sheet# could this be updated to show the missing sheet name or changed in a way so that I can use a list of Names that are responsible for the data in the tab…….
    . I will need to correct that in my code. I did also not appreciate that my does not error, as the On Error Resume Next Error Handler ( always dangerous to use ) has prevented that. –So Instead a new sheet is made with the default name and no data is passed to it…, Experience makes wise – that name length check I will put as standard from now on in all my codes. And I will get my error handling better ( I just learnt all that from Rory, ( ..http://www.excelforum.com/excel-new-...err-clear.html
    http://excelmatters.com/2015/03/17/on-error-wtf/
    - so I should have checked that as well!!
    ) Good catch again there on your behalf..

    . 2) You have taken the trouble to apply it to the OPs particular application, giving the Correct Titles, Headings etc., which also makes me think I misunderstood his screenshot in Post #4 and so also chopped off headings after column E in my second code also in addition to chopping off column A which looking again was perhaps all he wanted to be chopped off.. . I shall need to correct that as well!!!!!

    Alan.
    Catch you again in Threads, ( I hope! )

  8. #8
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Hi Darren....

    Quote Originally Posted by SilverMonkey View Post
    …..also noticed that when there is no data in a newly created sheet then the tab name stays as Sheet# could this be updated to show the missing sheet name or changed in a way so that I can use a list of Names that are responsible for the data in the tab…….
    .


    .. Thanks to apo’s kind participation in this Thread I have noticed a couple of errors in my program. I shall correct them and post a new version shortly…. It explains your comment above which I did not get at the time. – Just goes to show how useful it can be with constructive multiple answers and participation in a Thread..

    Alan.

  9. #9
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    …Hi Darren.
    . Here is the “Latest version” of mine… !!!! ( The code proved too long to fit in one post, so half is in the next post. You will need to copy both bits and put them together in the same Code Module!!! )

    Code First Half:

    Please Login or Register  to view this content.

  10. #10
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Code Second Half:



    Please Login or Register  to view this content.

    Alan

  11. #11
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Copy and paste entire row to second sheet based on cell value

    Hi Doc..

    I have added some comments to the code.. hope they help..

    The method I went for was basically an alternative to auto filter.. and kept it all within arrays which theoretically should be faster (especially with large data sets)

    here I cannot even get past the first z variable. It appears to be empty the entire time the code runs… but if I comment it out the code doesn’t work??
    It is my understanding that the z variable is just used to add the key to the Dictionary if it doesn't exist already..

    Check out SNB's website for lots of good stuff on Dictionaries and arrays..
    http://www.snb-vba.eu/VBA_Dictionary_en.html

    Please Login or Register  to view this content.

  12. #12
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Hi apo,

    . I think I have “earned” the privilege” now to use your last code. I think I have some good understanding of it
    . Just some feedback, maybe some question but only if and when you have the time. I have rambled a bit.. I have already learnt and benefited a lot from your given code, and the comments were a great help.. I still needed to go through it carefully in detail, but the ‘green comments help getting a good start, and help greatly in knowing in which direction to think once I know your way of thinking in the code. (And additionally, as you noticed yourself, sometimes it helps reflect if any step is necessary or could be done in another way..etc…)

    . 1 )
    Quote Originally Posted by apo View Post
    ……It is my understanding that the z variable is just used to add the key to the Dictionary if it doesn't exist already..
    Check out SNB's website for lots of good stuff on Dictionaries and arrays..
    ….
    . I have visited that snb site quite a lot when trying to understand the Dictionary Stuff. It is a very good site. The way things are explained suits my way of thinking often.. As with other problems, though, I find I often have to read that site , then solve the problem myself, then go back and read again and see that the info is there and confirms my findings. So it has helped me a bit to get that “empty” z variable mystery solved in my mind. I believe he may have his explanation slightly in error.. Actually the idea with

    z = objDictionary.Item("AKeyX")

    .. I think, is that in this method the main feature is to assign the item with key “AKeyX” to the variable. A second feature, as you use in the code is that if the “AkeyX” does not exist it will be made. A very convenient feature to simplify a code only wishing to use the Dictionary in it’s “by – product “ characteristic of only allowing unique keys..
    . Where I differ a bit** with snb is
    .. a) My experiments suggest that in the case of the key not existing I am returned a “ value “ ., that being an “empty” as I must dimension Z as a variant, or the code crashes giving error of constant type inconsistency.. ** snb is inconsistent and in fact does agree with me later!
    . b) I think snb has listed this as 1 of 4 methods to add an item to the dictionary.. well I suppose it sort of does only if my .a) is correct..but then only in the case of the key not existing, otherwise it gives the variable z the item that is there ., so is not “adding” an item……
    …. I do accept I am splitting hairs a bit.. you could interpret differently what snb says.

    . I think perhaps that snb page needs to emphasize a few points, this is not the first time an important thing I found by experimenting was actually there, sort of , but not immediately obvious or too clear.... but the info is mostly there. …. snb I find an interesting character. He is probably one of the few that could answer that mystery for certain. – if he felt inclined: He often pops in a Thread like you, unexpectedly, with a nice new code alternative. The difference in my experience is that his do not work, at least he does not apply them to the particular problem. Getting them working can be a healthy learning experience, but you need a lot of time. I think you describe him in the Computer trade as a ”Troller” or similar. But I may be in error, certainly no offence intended to him, – I am an old Practical Physicists, no working experience with Computing, and did not spend much time with many of our Programmers back then.. I have learnt a lot from working though some of snb’s codes, but it took the ( long ) time I no longer have!!! - ‘green Comments like yours ( and the code actually working !! ) is a great advantage… !)

    (, BTW of course my conclusion ties up with your “ … the z variable is just used to add the key to the Dictionary if it doesn't exist already …. “ )



    . 2 )
    .
    Quote Originally Posted by apo View Post
    ……..The method I went for was basically an alternative to auto filter.. and kept it all within arrays which theoretically should be faster (especially with large data sets)…….
    … yeh, I should have got that point immediately. – It is a strange world.. when I woke up last wear after my 25 year computer coma , I started looking at Array math’s based on my Mathcad and minimum basic programming memory from back then… Then somehow went over to the Excel VBA Worksheet World, using and therefore interacting with all the “nice” pretty Range .Methods .Properties etc. .. Then recently based on my experience, your recommendations and others, I have gone over to capturing everything in one go into arrays and doing most workings there. Clearly interacting with the Spreadsheet is “unhealthy” in many ways . Speed… of code… and the brain slows down..… your neat idea of getting the grid co-ordinates for the rows for each sheet – just the sort of thing that back in those days for me after a Coffee what I would have come up with.. but I have been infected by the .OOP .Do-It-All-For-me mentality… which at the end of the day does something like your codes do, but just more inefficiently by wasting code making it so All-In-One user friendly!!
    . ( I “sliced out” the “column 1” of Array xx() and transposed that rather than transposing that Column(1) from the spreadsheet… Not sure if that is helping again to get away from sheet interaction…. It is not clear to me if by using Application.WorksheetFunction I interact with the spreadsheet or just use some Mathematical function??. Any idea on that??


    . 3 )
    …. y() Array !!!!! ( Your y Array )
    . Clearly you really need to know how VBA sequentially does things, for example to know how to know how your y() array will come out. Index appears to be doing here something different from how I know it until now.. –Here it is Looking for the entire row indices ( Which are in an Array ) one after the other , for each column , - the column given by the second argument indices in that second argument Array of indices. Then it goes on and does the same for the next column. ( I see, you assume “the maximum” and so this is repeated as many times as if the LookUpHeading ( Service area ) was one value) ( the second ( Column array) argument which you have organized to contain long numbers 2,3,4,5 etc. ). I can follow it clearly with putting a watch on the arrays and seeing how they are filled… BUT
    . 3 a) is it written anywhere how the Index behaves in such a situation??? Or is it just experience / instinct on your behalf??

    . 3 b) - Very minor point: I follow why just before you output your y Array to each sheet you resize to chop off the “Error” rows ( corresponding to columns in the transposed for output.. ) . However, this would I think not be unnecessary if you changes this..

    Please Login or Register  to view this content.
    to this

    Please Login or Register  to view this content.
    . In this case the y() Array only “extends” as far as necessary so we have no error rows in it…
    And then we can replace the output line

    Please Login or Register  to view this content.
    with..

    Please Login or Register  to view this content.
    … appears to work, but I may have missed a point.

    ……………………………………………………..

    . I include my full worked through or rather “raped “ version of your code .. split into two in the next 2 Posts #13 and #14. It is not a pretty sight. Sorry about that. But miraculously it still seems to work despite my best efforts…

    (. The bit to delete sheets at start I do partly to help me when I debug/develop the code so I don’t have to keep deleting manually all sheets if I re-run the code after any modifications. ( OPs if they are using the code in the practice, often say they may have new data to add on the end of rows in existing.. I suggest as an alternative store the current file as an .xlsx or ,xls File as a back up, then reopen the .xlsm file, add the new stuff in sheet 1 ( data ) and then run the code again..) ..




    Thanks again, especially for the ’green comments, they’re are very useful, my opinion

    Alan

    P.s. This shows my computing ignorance: I have no idea how you get the Private Sub CommandButton1_Click() thing up and running in VBA so I just have it a normal Sub. I did do a VB beginners course for fun last year, sounds something along these lines I guess, UserForms and the like.

  13. #13
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    First Code half:

    Please Login or Register  to view this content.

  14. #14
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Second Code half:


    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Copy and paste entire row to second sheet based on cell value

    Thanks Doc..

    To be honest.. the 3 main 'parts' to that code:

    1. Getting the Uniques using the Dictionary.
    2. Building a string of row numbers that will be used in the Application.Index part later.. and
    3. Populating the y array...

    Are all a result of studying SNB's website and following his postings.. his website is a goldmine of information. It is a great reference point .. many times i think to myself that something must be possible.. I head over to SNB's website and usually find either an answer or at least push in the right direction.

    Sometimes SNB's code makes my brain hurt,lol.. but that's a good thing right..

    Having said that.. almost everything else I have learnt about VBA has come from forums like this..

    3 a) is it written anywhere how the Index behaves in such a situation???
    I can't say i have seen it anywhere except on SNB's website..

    3 b) - Very minor point: I follow why just before you output your y Array to each sheet you resize to chop off the “Error” rows ( corresponding to columns in the transposed for output.. )
    appears to work, but I may have missed a point.
    Yeap.. you got the point.. and its not a minor one.. much better!

    btw..
    I am an old Practical Physicists
    Me.. I am a Cellarman.. beer is my forte..

    Just goes to show.. VBA is for everyone..

  16. #16
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Copy and paste entire row to second sheet based on cell value

    Thanks for the reply, apo
    Alan

+ 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 entire row based on 2 criteria and paste on the next blank row on a specific sheet
    By ALNER in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2014, 11:23 PM
  2. [SOLVED] Copy & paste entire row to different sheet based on cell entry
    By XxCMoneyxX in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-14-2013, 07:52 PM
  3. [SOLVED] copy paste entire row into a specific cell in sheet 2
    By melody10 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-29-2013, 04:40 PM
  4. Copy and Paste entire row based upon parts of value in a cell
    By queenstarks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2013, 11:05 AM
  5. Copy/Cut entire row to another sheet based on cell value
    By excelkeechak in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-02-2010, 04:23 PM

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