2011-11-11 59 views
0

我有一个表格1在列A中有名称,而我在表格2列A中有名称。名称大多是相同的,除了对片材2,而不是片材1逗号或期间我需要匹配某些文字和取片1个B列并粘贴到薄片2的列B.匹配一些文本表格1a与表格2a匹配如果匹配表格1b到表格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. 我用VLookup,而不是Index(Match(进一步shorthen公式
  3. Substitute的在查找范围内替换,.
  4. 公式必须是Array formulaSubstitues工作。
  5. 由于Substitute返回的值是一个字符串。我已将Value中的函数转换回数字,如果不需要,请将其删除。
  6. 查找值是调用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 - Tom将成为Go for it Inc
- 此匹配Go for it, Inc.(成为Go for it Inc

Doug, Inc - Joe将变得Doug, Inc
- 这不会比赛Doug, Inc.如果你想忽略,.(成为道格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

对不起@ user1013478直到现在我没有注意到你的回应。当我用像你这样的工作表测试我的代码时,它按照我的计划做了,但这不是你想要的,你可能没有注意到这种效果。我不明白你想要B列改变。我的代码需要两列A值,删除连字符后的任何内容以及任何逗号和点。如果结果值相同,它将Sheet 2 Column A设置为未更改的Sheet 1 Column A.它对Column B没有任何影响。我将查找我的测试工作簿,修改我的代码以更新Column B并修改我的原始发布。 –