Computing.Net > Forums > Office Software > Excel VBA conditional copy/paste

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Excel VBA conditional copy/paste

Reply to Message Icon

Name: VBA Beginner
Date: September 30, 2009 at 17:19:41 Pacific
OS: Windows XP
Product: Microsoft Excel 2003 (full product)
Subcategory: Microsoft Office
Comment:

Hi there!

I'm just beginning the long road in learning VBA and already have an immediate need for some code that is quite beyond me.

I have an excel worksheet that contains thousands of rows of data in columns A thru U, and another workbook with multiple worksheets that are blank and waiting to receive data. I need to search the value in column A of the first worksheet (let's say it's valued with the words "Bird", "Cat", "Dog", etc.) - I will search for all "Birds" - and copy/paste those rows into the worksheet of the other workbook that's named "Birds" and so on, until I've copied all of the records on the first worksheet to their proper worksheets in the other workbook (that workbook may have up to a hundred or so worksheets). The one good thing is that the first spreadsheet is sorted by column A, so it might be easy to get chunks of records at a time, if I knew how.

What is the best way to do this in VBA? I have started some code, but did not post it yet because it's very sloppy right now. I tried doing the copy/paste one row at a time, then ran into trouble with the subsequent rows. Besides, doing it one row at a time seemed inefficient.

This is something that I will need to do monthly so I really appreciate any help that you can provide.

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Humar
Date: September 30, 2009 at 19:22:50 Pacific
Reply:

Hi,

Please provide some hard examples of your data.

All the vague examples don't give sufficient to information to offer any solution.

When you say:
I tried doing the copy/paste one row at a time, then ran into trouble with the subsequent rows.
what trouble did you run into.

Why did subsequent copy/pastes not work - what error did you get. Was this a VBA copy paste or a manual paste. If it was a VBA copy paste, what code did you use.

Regards


1

Response Number 2
Name: DerbyDad03
Date: October 1, 2009 at 05:23:53 Pacific
Reply:

If I understand your setup correctly, you have a list of data in Sheet1 that looks something like this:

     A      B
1  Bird     1
2  Bird     2
3  Dog      1
4  Dog      2

You also have sheets named Bird and Dog

You want to copy each row that has Bird in Column A to the sheet named Bird, etc.

If that's correct, just to simplify things I would first copy the sheet with the list to the workbook with the Bird and Dog sheets and place it as Sheet1.

Then you could try this code:

Sub MoveDataToSheetName()
'Determine how many rows from Sheet1 to copy
  lastListRow = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
'Loop through rows
    For myItems = 1 To lastListRow
'Set Sheet name based on value in Sheet1 Column A for each list item
     shtName = Sheets(1).Range("A" & myItems)
'Determine next empty row in Sheet with the same name 
'as the value in Sheet1 Column A
     destRow = Sheets(shtName).Range("A" & Rows.Count).End(xlUp).Row + 1
'Copy current list item to proper sheet
      Rows(myItems).EntireRow.Copy _
        Destination:=Sheets(shtName).Range("A" & destRow)
'Loop
    Next
End Sub


1

Response Number 3
Name: Razor2.3
Date: October 1, 2009 at 11:02:27 Pacific
Reply:

I originally asked this in his original topic on the Programming forum, but seeing as that topic was deleted, I should probably repost here.

VBA Beginner: (let's say it's valued with the words "Bird", "Cat", "Dog", etc.) - I will search for all "Birds" - and copy/paste those rows into the worksheet of the other workbook that's named "Birds"
Search for "Bird" for search for "Birds"?
Also, what links the value "Bird" to the workbook name "Birds"? Do you just tack on an "s"? What do you do with the value of "Mouse"?


0

Response Number 4
Name: VBA Beginner
Date: October 1, 2009 at 17:17:37 Pacific
Reply:

Thank you all so much for pointing me in the right direction! Each response helped to guide me closer to finding a solution.

The response from DerbyDad03 provides a good foundation for me to build on. I think I should be able to complete it from here.

Your time and expertise are much appreciated!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Find function Custom Forms


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Excel VBA conditional copy/paste

Excel VBA Conditional Copy of Rows www.computing.net/answers/office/excel-vba-conditional-copy-of-rows-/7628.html

Excel-Copy paste Data download from internet www.computing.net/answers/office/excelcopy-paste-data-download-from-internet/9063.html

Conditional Copy in Excel www.computing.net/answers/office/conditional-copy-in-excel/9635.html