2011-11-11 10 views
0

私はA列に名前を持ち、2列Aに名前を持つsheet1を持っています。シート2上にはなく、シート1上にコンマまたは期間以外Iは、テキストの一部が一致し、シート1件の列Bを取り、シート2列Bに貼り付ける必要があり一部のテキストシートに一致する1 aとシート2 aペーストシートに一致する場合1 bとシート2 b

例:

シート1

A      B 
Doug, Inc.    $12.03 
For it all, LLC  $4452.03 
Go for it, Inc.  $235.60 

Sheet 2 
A      B 
Doug, Inc - Joe   
For it all - Mike 
Go for it Inc - Tom 

名前が正確に一致する場合にのみ、ダッシュ「 - 」の前に一致して貼り付けられるコードがあります。 カンマやピリオドを気にせずに、テキストの一部と一致するように助けが必要です。

Dim ws1 As Worksheet 
    Dim ws2 As Worksheet 
    Dim rng1 As Range 
    Set ws1 = Sheets(1) 
    Set ws2 = Sheets(2) 
    Set rng1 = ws2.Range(ws2.[a1], ws2.Cells(Rows.Count, "A").End(xlUp)) 
With rng1.Offset(0, 1) 
    .FormulaR1C1 = "=IF(RC[-1]<>"""",IF(NOT(ISERROR(MATCH(LEFT(RC[-1],FIND("" - "",RC[-1])-1),'" & ws1.Name & "'!C[-1],0))),INDEX('" & ws1.Name & "'!C,MATCH(LEFT(RC[-1],FIND("" - "",RC[-1])-1),'" & ws1.Name & "'!C[-1],0)),""""),"""")" 
    .Value = .Value 
End With 

答えて

0

私はあなたのためのシート1つのデータを名前付き範囲を使用する場合、それはあなたのコードを簡素化しますあなたの式

  1. をリファクタリングしています。ここで私は名前にData
  2. を使用していた私は、二重Substitute年代は、検索範囲内,.
  3. を交換式がためArray formulaにする必要があり、さらに式
  4. をshorthenするVLookupではなくIndex(Match(を使用しましたSubstituesが動作します。
  5. Substituteのため、戻り値は文字列です。私はValueの関数をラップして数字に戻しました。必要でない場合はこれを削除してください。
  6. ルックアップ値は、- ... 存在
  7. 場合

を剥ぎ取り、VLOOKUPへの呼び出しの前に用意され「

.FormulaArray = "=VALUE(VLOOKUP(" & _ 
    "LEFT(RC[-1],IFERROR(FIND("" - "",RC[-1])-1,LEN(RC[-1])))," & _ 
    "SUBSTITUTE(SUBSTITUTE(Data,"","",""""),""."",""""),2,0))" 

私は
For it all - Mikeが不明だサンプル・データの一つの側面がありますFor it allになります。
- このは(For it all LLCなり)試合For it all, LLC

Go for it Inc - TomGo for it Incなりません。
- (Go for it Incなる)この意志一致Go for it, Inc.

Doug, Inc - Joe

Doug, Incとなるであろう。
- このはないあなたが両方シートで,.を無視したい場合は

を(ダグInc`になります)試合Doug, Inc.、私は理解していないです

.FormulaArray = "=VALUE(VLOOKUP(" & _ 
    "SUBSTITUTE(SUBSTITUTE(LEFT(RC[-1],IFERROR(FIND("" - "",RC[-1])-1,LEN(RC[-1]))),"","",""""),""."","""")," & _ 
    "SUBSTITUTE(SUBSTITUTE(Data,"","",""""),""."",""""),2,0))" 
+0

返信いただきありがとうございますが、 "#NAME?" – user1013478

0

使用します。あなたが達成しようとしていること。シート2のB欄をクリアしているようなコードは分かりません。なぜ私は式を設定するマクロを使用しているのか分かりません。

以下のコードは、私があなたがやろうとしていることをしています。そうでない場合は、私のコードがあなたに求めるアイデアを与えて、あなたが求めるコードを作成できることを願っています。

私はあなたがExcel Basicに精通していないと推測しています。申し訳ありませんが、次の場合あなたの知識を侮辱します。私はあなたがむしろ混乱するよりもむしろ侮辱されると思います。

Sub Test2() 

    ' This is revised coding. I had not read the question carefully enough 
    ' so my original code did not do what was required. 

    Dim Pos2 As Integer   ' The 1s and 2s in variable names 
    Dim RowCrnt As Integer  ' identify the variable as being for Sheet1 
    Dim RowMax As Integer   ' or Sheet2. The same row variables are 
    Dim S1ColAB() As Variant  ' used for both sheets. 
    Dim S2ColAB() As Variant 
    Dim Value1 As String 
    Dim Value2 As String 

    With Sheets("Sheet2") 
    ' I generally use Find instead of End(xlUp) for reasons I no longer 
    ' remember. This searches column A (Columns("A")) for anything ("*") 
    ' starting from cell A1 (Range("A1")) and moving backwards 
    ' (xlPrevious) until it finds a value. 
    RowMax = .Columns("A").Find("*", .Range("A1"), xlFormulas, , _ 
               xlByRows, xlPrevious).Row 

    ' Range(Cells(A,B),Cells(C,D)) defines a rectangle of cells with Row=A, 
    ' Col=B as top left and Row=C, Col=D as bottom right. 
    ' The following statement loads the contents of a block of cells to a 
    ' variant array. Another question has led to a discussion about the value 
    ' of using variant arrays in this way. I have found that moving values 
    ' from one sheet to another can be very slow so I believe it is useful in 
    ' this situation. 
    S2ColAB = .Range(.Cells(1, 1), .Cells(RowMax, 2)).Value 

    ' Warning about moving values from a cell into a string or variant variable 
    ' and then from the variable into another cell. 
    ' ========================================================================= 
    ' When moving the value from the variable to the cell, Excel will 
    ' misinterpret the value if it can. 
    ' 
    ' For example, if the value is 13/1/11 (13 January 2011 here in England) 
    ' this value will be correctly transferred into the new cell. But if the 
    ' value is 4/1/11 (4 January 2011), Excel will recognise this as a valid 
    ' American date and set the new cell to 1 April 2011. The damage that bug 
    ' caused by corrupting a third my dates! I had tested my code towards the 
    ' end of a month and it worked perfectly until the next month. 
    ' 
    ' In this example, string $12.03 becomes currency 12.03 and displays 
    ' here as £12.03. 

    End With 

    With Sheets("Sheet1") 
    ' Load the matching cells from sheet 1 
    S1ColAB = .Range(.Cells(1, 1), .Cells(RowMax, 2)).Value 
    End With 

    With Sheets("Sheet2") 
    For RowCrnt = 1 To RowMax 
     ' I move the Column A values for matching row from the arrays to string 
     ' variables so I can amend their values without losing the original 
     ' values. This was essential with my original code and I have not 
     ' changed it since I think it makes the code easier to understand and 
     ' probably marginally faster. 
     Value1 = S1ColAB(RowCrnt, 1) 
     Value2 = S2ColAB(RowCrnt, 1) 
     ' The following code removes anything following a hyphen from Value 2. 
     ' It then removes all commas and dots from both Value1 and Value2. If 
     ' the final values are the same, it moves the Column B of Sheet1 to 
     ' Sheet2. 
     Pos2 = InStr(1, Value2, "-") 
     If Pos2 <> 0 Then 
     ' Pos2 is not zero so extract the portion of Value2 up to the "-" 
     ' and then trim trailing spaces. 
     Value2 = RTrim(Mid(Value2, 1, Pos2 - 1)) 
     End If 
     ' Replace all commas with nothing. 
     Value1 = Replace(Value1, ",", "") 
     ' Replace all dots with nothing. 
     Value1 = Replace(Value1, ".", "") 
     ' Merge the two replaces into a single statement. 
     Value2 = Replace(Replace(Value2, ",", ""), ".", "") 
     If Value1 = Value2 Then 
     ' If the modified values are equal, copy the Column 2 (B) from 
     ' Sheet1 to Sheet2. 
     .Cells(RowCrnt, 2).Value = S1ColAB(RowCrnt, 2) 
     End If 
    Next 

    End With 

End Sub 

これが役立ちます。私が自分自身を適切に説明していなければ元に戻ってください。

+0

返事をありがとう、私はあなたのコードを試して何も起こることはありませんでした。私はちょうど各シートの列Aと一致するようにしようとしている場合は、各シートの列Bに一致します。名前はほぼ同じですが、コンマやピリオドが追加されているかどうかは不明です。だから私の論理は、もしsheet1 A1 = sheet2 A1ならばSheet2 B1は= Sheet1 B1 – user1013478

+0

Sorry @ user1013478私は今まであなたの反応を知らなかった。私のコードをあなたのようなシートでテストしたところ、私が計画したことはありましたが、これはあなたが望むものではなく、あなたはその効果に気付かなかったかもしれません。私はあなたがカラムBを変更したかったことに感謝しませんでした。私のコードは2つのColumn Aの値をとり、ハイフンとコンマとドットの後ろにあるものはすべて削除します。結果値が同じ場合は、シート2の列Aを変更されていないシート1の列Aに設定します。どちらの列にも何も変わりません。テストブックを探し、コードを修正して列Bを更新し、元の投稿。 –

関連する問題