PC Review


Reply
Thread Tools Rate Thread

Copying between worksheets using a Lookup?

 
 
DJ
Guest
Posts: n/a
 
      20th Mar 2007
Hello,

Looking for assistance on using VBA to copy entire rows from one
worksheet to another based on criteria contained in a third worksheet.

I have a workbook containing sales records by salesperson. The
salesperson's name is listed in column 1. I would like to
programmatically copy entire rows from this worksheet (up to 65,000
rows) to a new worksheet in the same workbook based on the salesperson's
name. I would like to be able to list the salesperson's names in a third
worksheet and have the macro copy only the rows of the salespeople
listed on this third worksheet. The list of sales people could change
each time the macro is run, so the macro would need to dynamically refer
to this list of salespeople.

I've done some light VBA programming in the past but I'm not sure how to
approach this problem. From what I've read, vlookup may be part of the
solution, but again I'm not clear on how to implement this.

Any ideas, suggestions or examples would be appreciated. Thanks!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      20th Mar 2007
The code below should work. I assume there were no header rows in your
speadsheet. Adjustment would have to be made in code if there are heders.
Also I assume the Salespeople Names were in Column A (both 1st and last).
You also may have to change the constants for the sheet names to match your
worksheet.

Sub copysalepeople()

'AS abbreviation for All SalesPeople
Const ASP = "Sheet1"
'SSP abbreviation for SelectedSalesPeople
Const SSP = "Sheet2"
'SS abbreviation for SelectedSales
Const SS = "Sheet3"


SSPLastRow = Sheets(SSP).Cells(Rows.Count, 1).End(xlUp).Row
Sheets(SSP).Activate
Set SSPNames = Sheets(SSP).Range(Cells(1, 1), Cells(SSPLastRow, 1))

ASPLastRow = Sheets(ASP).Cells(Rows.Count, 1).End(xlUp).Row
Sheets(ASP).Activate
Set ASPNames = Sheets(ASP).Range(Cells(1, 1), Cells(ASPLastRow, 1))

SSRowCount = 1
For Each SSPCell In SSPNames

For Each ASPCell In ASPNames

If (StrComp(SSPCell, ASPCell) = 0) Then

Sheets(ASP).Cells(ASPCell.Row, 1).EntireRow.Copy _
Destination:=Sheets(SS).Cells(SSRowCount, 1)
SSRowCount = SSRowCount + 1

End If

Next ASPCell


Next SSPCell

End Sub

"DJ" wrote:

> Hello,
>
> Looking for assistance on using VBA to copy entire rows from one
> worksheet to another based on criteria contained in a third worksheet.
>
> I have a workbook containing sales records by salesperson. The
> salesperson's name is listed in column 1. I would like to
> programmatically copy entire rows from this worksheet (up to 65,000
> rows) to a new worksheet in the same workbook based on the salesperson's
> name. I would like to be able to list the salesperson's names in a third
> worksheet and have the macro copy only the rows of the salespeople
> listed on this third worksheet. The list of sales people could change
> each time the macro is run, so the macro would need to dynamically refer
> to this list of salespeople.
>
> I've done some light VBA programming in the past but I'm not sure how to
> approach this problem. From what I've read, vlookup may be part of the
> solution, but again I'm not clear on how to implement this.
>
> Any ideas, suggestions or examples would be appreciated. Thanks!
>

 
Reply With Quote
 
DJ
Guest
Posts: n/a
 
      22nd Mar 2007
Your example worked great! As promised, the only tweeks needed were for
sheet names and columns. Other than that it was copy & paste Thanks for
sharing this with me!

DJ

Joel wrote:
> The code below should work. I assume there were no header rows in your
> speadsheet. Adjustment would have to be made in code if there are heders.
> Also I assume the Salespeople Names were in Column A (both 1st and last).
> You also may have to change the constants for the sheet names to match your
> worksheet.
>
> Sub copysalepeople()
>
> 'AS abbreviation for All SalesPeople
> Const ASP = "Sheet1"
> 'SSP abbreviation for SelectedSalesPeople
> Const SSP = "Sheet2"
> 'SS abbreviation for SelectedSales
> Const SS = "Sheet3"
>
>
> SSPLastRow = Sheets(SSP).Cells(Rows.Count, 1).End(xlUp).Row
> Sheets(SSP).Activate
> Set SSPNames = Sheets(SSP).Range(Cells(1, 1), Cells(SSPLastRow, 1))
>
> ASPLastRow = Sheets(ASP).Cells(Rows.Count, 1).End(xlUp).Row
> Sheets(ASP).Activate
> Set ASPNames = Sheets(ASP).Range(Cells(1, 1), Cells(ASPLastRow, 1))
>
> SSRowCount = 1
> For Each SSPCell In SSPNames
>
> For Each ASPCell In ASPNames
>
> If (StrComp(SSPCell, ASPCell) = 0) Then
>
> Sheets(ASP).Cells(ASPCell.Row, 1).EntireRow.Copy _
> Destination:=Sheets(SS).Cells(SSRowCount, 1)
> SSRowCount = SSRowCount + 1
>
> End If
>
> Next ASPCell
>
>
> Next SSPCell
>
> End Sub
>
> "DJ" wrote:
>
>> Hello,
>>
>> Looking for assistance on using VBA to copy entire rows from one
>> worksheet to another based on criteria contained in a third worksheet.
>>
>> I have a workbook containing sales records by salesperson. The
>> salesperson's name is listed in column 1. I would like to
>> programmatically copy entire rows from this worksheet (up to 65,000
>> rows) to a new worksheet in the same workbook based on the salesperson's
>> name. I would like to be able to list the salesperson's names in a third
>> worksheet and have the macro copy only the rows of the salespeople
>> listed on this third worksheet. The list of sales people could change
>> each time the macro is run, so the macro would need to dynamically refer
>> to this list of salespeople.
>>
>> I've done some light VBA programming in the past but I'm not sure how to
>> approach this problem. From what I've read, vlookup may be part of the
>> solution, but again I'm not clear on how to implement this.
>>
>> Any ideas, suggestions or examples would be appreciated. Thanks!
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lookup in two worksheets Dee Microsoft Excel Discussion 2 27th Mar 2010 05:29 AM
Lookup between two worksheets =?Utf-8?B?d2hhdHp6dXA=?= Microsoft Excel New Users 8 18th Aug 2007 05:39 PM
RE: Lookup between two worksheets =?Utf-8?B?d2hhdHp6dXA=?= Microsoft Excel New Users 0 17th Aug 2007 01:58 AM
RE: Lookup between two worksheets =?Utf-8?B?TWlrZSBI?= Microsoft Excel New Users 0 17th Aug 2007 01:44 AM
LOOKUP to various different worksheets David Microsoft Excel Worksheet Functions 2 13th Nov 2003 07:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:42 AM.