0

Excelで位置合わせされていないデータを位置合わせ/照合しようとしています。現在、データは次のようになっています。

customerid  customerid2 emailaddress          firstname         lastname

1           2           bobhope@.com          Chris             Anderson
2           1           chrisanderson@.com    Bob               Hope
7           8           Bryansmoth@.com       Jenn              Lacy
8           7           Jennlacy@.com         Bryan             Smoth
9                       123@.com
10          11          RonnieWilliams@.com   Andrew            Smoth
11          10          Andrewsmoth@.com      Ronnie            Williams

基本的に、列AとBのIDをそれに応じて一致させ、すべてのデータを右側に揃えようとしています。したがって、次のようになります。

1            1          bobhope@.com          Bob               Hope
2            2          chrisanderson@.com    Chris             Anderson
7            7          Bryansmoth@.com       Bryan             Smoth
8            8          Jennlacy@.com         Jenn              Lacy
9                       123@.com
10           10         RonnieWilliams@.com   Ronnie            Williams
11           11         Andrewsmoth@.com      Andrew            Smoth

私はこれを手動で行いますが、17,000 の間違ったエントリがあります。私が理解していることから、この問題の修正に役立つ VBA を Excel で実行できます。

4

1 に答える 1

1

私は通常、すぐにコードを提供することはありませんが、これは興味深い質問のように思えました。この簡単なコードを試してください。

私の仮定

  1. データのあるシートの名前が呼び出されますSheet1
  2. データはColumns A:E
  3. の複数出現はありません。customerid2
  4. 行 1 にはヘッダーがあります

ロジック:

  1. 範囲B:Eを配列に格納する
  2. B:E出力用の列をクリアする
  3. 列 A をループし、配列の列 1 と一致させ、一致が見つかった場合は、関連する行に入力します。

コード:

コードにコメントしました。それでも問題が発生した場合は、単に投稿してください。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim MyData As Variant
    Dim i As Long, j As Long, lRow As Long

    '~~> This is your relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> get the last row which has data
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Get your Col B:E Range
        Set rng = ws.Range("B2:E" & lRow)

        '~~> Store the range in an array
        MyData = rng

        '~~> Clear Col B:E for output
        rng.ClearContents

        '~~> Loop through Col A
        For i = 2 To lRow
            '~~> Loop through Array
            For j = LBound(MyData) To UBound(MyData)
                '~~> If match found then write to relevant row
                If MyData(j, 1) = .Range("A" & i).Value Then
                    .Range("B" & i).Value = MyData(j, 1)
                    .Range("C" & i).Value = MyData(j, 2)
                    .Range("D" & i).Value = MyData(j, 3)
                    .Range("E" & i).Value = MyData(j, 4)
                    Exit For
                End If
            Next j
        Next i
    End With
End Sub

スクリーンショット(前後)

ここに画像の説明を入力

于 2013-09-27T10:46:17.517 に答える