2011-11-11 2 views
0

나는 A 열에 이름이 있고 B 열 2 열에 이름이있는 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. VLookup보다는 Index(Match(을 더 공식을
  3. 이중 Substitute을 shorthen하는 '이 s는 조회 범위에서 ,.을 대체 이름을 사용했습니다
  4. 공식은 Array formula에 있어야합니다 Substitues가 작동합니다.
  5. Substitute 때문에 반환 값은 문자열입니다. 나는 숫자로 다시 변환하기 위해 Value에있는 함수를 래핑했습니다. 필요하지 않은 경우 이것을 제거하십시오.
  6. 조회 값은 - ... 현재

'경우 전원 스트립, VLOOKUP 호출하기 전에 준비

I에 대해 확실하지 오전 샘플 데이터의 한 측면이있다
.FormulaArray = "=VALUE(VLOOKUP(" & _ 
    "LEFT(RC[-1],IFERROR(FIND("" - "",RC[-1])-1,LEN(RC[-1])))," & _ 
    "SUBSTITUTE(SUBSTITUTE(Data,"","",""""),""."",""""),2,0))" 


For it all - MikeFor 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 - JoeDoug, 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 열을 일치시킨 다음 A가 일치하면 각 시트에서 B 열과 일치 시키려고합니다. 이름은 거의 같지만 쉼표 또는 마침표가 추가되었거나 표시되지 않습니다. 그래서 내 논리는 sheet1 A1 = sheet2 A1 then Sheet2 B1 will = Sheet1 B1 – user1013478

+0

죄송합니다 @ user1013478 지금까지 귀하의 응답을 알리지 못했습니다. 내 코드를 당신의 시트와 같이 테스트했을 때, 그것은 내가 계획 한대로했으나 이것이 당신이 원하는 것이 아니었고 그 효과를 눈치 채지 못할 수도 있습니다. 열 B가 바뀌길 바랐다는 것에 감사 드리지 않습니다. 내 코드는 두 개의 Column A 값을 취하고 하이픈과 쉼표 및 점 뒤에 나오는 모든 것을 제거합니다. 결과 값이 같으면 시트 2 열 A를 변경되지 않은 시트 1 열 A로 설정합니다. 열 B에는 아무 것도 적용되지 않습니다. 테스트 통합 문서를 찾고, 코드를 수정하여 열 B를 업데이트하고 수정합니다 원래 게시. –

관련 문제