Microsoft KB Archive/213292

= XL2000: How to Copy One of Each Record Type to Another Worksheet =

Article ID: 213292

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213292





SUMMARY
Using Microsoft Visual Basic for Applications In Microsoft Excel 2000, you can use Microsoft Visual Basic to copy a single instance of a record type to another worksheet.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To copy a single instance of a record type to another worksheet, use the steps in the following example.

The following example uses a Visual Basic macro to search the "Part no." field and then copies each non-duplicate "Part no." to a second worksheet.  Start Microsoft Excel 2000.  Type the following data on Sheet 1:   A1:  Part no.    B1:  Description     C1:  On Hand       D1:  Store A2: 10AB        B2:  Nuts            C2:  1             D2:  1 A3: 11AB        B3:  Bolts           C3:  2             D3:  2 A4: 10AB        B4:  Nuts            C4:  3             D4:  3 A5: 12AB        B5:  Nuts            C5:  4             D5:  4 A6: 11AB        B6:  Bolts           C6:  5             D6:  5   Type the following data on Sheet 2:   A1:  Part no.    B1:  Description     C1:  On Hand       D1:  Store  Press ALT+F11 to start the Visual Basic editor. On the Insert menu, click Module.</li>  Type the following macro on the new module sheet: <pre class="fixed_text">  Sub CopyNoDupes

' Selects sheet1. Worksheets("sheet1").select ' Selects cell A2. Range("a2").Select ' Turns off screen updating, which helps macro run faster. Application.ScreenUpdating = False

' Will run the below code until the active cell is blank. Do While ActiveCell.Value <> "" ' Flag is used to determine whether the record should be pasted ' to Sheet2. flag = True ' The variable value1 is assigned the value in the currently ' selected cell, initially cell A2. valuea = ActiveCell.Value ' Valueb is assigned the value in the cell one column to the ' right of the activecell, initially cell B2. valueb = ActiveCell.Offset(0, 1).Value

' Beginaddrs is assigned the address of the activecell. beginaddrs = ActiveCell.Address ' Endaddrs is assigned the address of the last contiguous cell ' of data on the active row. endaddrs = ActiveCell.End(xlToRight).Address

' Copies the current row's record to Clipboard. Range(beginaddrs & ":" & endaddrs).Copy ' Selects sheet2. Sheets("sheet2").Select ' Selects cell A2. Range("a2").Select

' Determine if the record type has already been copied to          ' Sheet2. Do While ActiveCell.Value <> ""

' If valuea, which contains the value from sheet1, equals ' the active cell's value in sheet2, and valueb equals ' the value in the cell immediately to the right of the ' active cell, then do the lines before the Else. If valuea = ActiveCell.Value And valueb = _ ActiveCell.Offset(0, 1).Value Then

' Flag used in an If statement below. False indicates do                  ' not paste record. flag = False

' Rowcount is assigned the current number of contiguous ' rows of records. rowcount = Range("a1").CurrentRegion.Rows.Count

' Selects a blank row to exit out of Do While. Range("a" & rowcount).Offset(1, 0).Select

Else

' Otherwise, select next record on Sheet2. ActiveCell.Offset(1, 0).Select

End If

' Check next record for a duplicate. Loop

' If flag was not set to False in the previous Do While Loop, ' for example, record type not in sheet2, then do the lines ' before the End If. If flag Then

' Rowcount is assigned the current number of contiguous rows ' of records. rowcount = Range("a1").CurrentRegion.Rows.Count

' Pastes the new record type after the last record. Range("a" & rowcount).Offset(1, 0).PasteSpecial End If

' Selects sheet1. Sheets("sheet1").Select ' Selects the next record on Sheet1. Range(beginaddrs).Offset(1, 0).Select

' Returns back to first Do While to repeat the above process. Loop

' Turns ScreenUpdating back on. Application.ScreenUpdating = True

' Removes the marquee around last copied record. Application.CutCopyMode = False

End Sub </li> Press ALT+F11 to switch to Excel.</li> On the Tools menu, point to Macro, and then click Macros.</li> In the Macro name box, select CopyNoDupes, and then click Run.</li></ol>

The Macro returns the following on Sheet 2: <pre class="fixed_text">A1:Part no.    B1:Description  C1:On Hand      D1:Store A2:10AB        B2:Nuts         C2:1            D2:1 A3:11AB        B3:Bolts        C3:2            D3:2 A4:12AB        B4:Nuts         C4:4            D4:4

<div class="references_section">