0

i need help writing the following macro.

sheet 1 has the following cells in column A 0001 0002 0003 0004 0005

sheet 2 has the following cells in Column A 0001 0003 0004

i need a macro to find the number from sheet 2 cell A1 in sheet 1, then copy the row A1 - G1 from sheet 2 to the corresponding number's row in sheet 1.

the numbers in sheet 2 will differ daily so i need the macro to search for whatever is typed in cell A1. i have tried everything i know for a macro to search for the contents of a cell with no luck.

the only way i can think of is to bring up a search box during the macro and type in the contents manualy

thanks

4

1 回答 1

1

尝试这个:

Sub CopyCells()

Dim rnSource As Range, rnDest As Range, rnTempSource As Range, rnTempDest As Range

Set rnDest = Sheet1.Range("A1", Sheet1.Range("A60000").End(xlUp).Address)
Set rnSource = Sheet2.Range("A1", Sheet2.Range("A60000").End(xlUp).Address)

'Loop through Sheet2 column A, find value in Sheet1 and copy cells across

For Each rnTempSource In rnSource
    If rnTempSource.Value <> "" Then
        Set rnTempDest = rnDest.Find(rnTempSource.Text) 'Used .text to ignore unusual number format 0001
        Sheet1.Range(rnTempDest, rnTempDest.Offset(0, 6)).Value = Sheet2.Range(rnTempSource, rnTempSource.Offset(0, 6)).Value
    End If
Next

End Sub
于 2013-01-07T15:38:25.423 回答