2016-08-05 5 views
0

다음은 일부 코드를 읽고 원하는 방식으로 문자열을 읽는 코드입니다. 나는 레이어 번호 중복 기능에 'COL1'문자열의 시작 부분을 무시하면서 중복을 제거합니다.

입력을 무시하면 중복 적으로

Layer 1: 25 xs 50 attaches at 6.98m and exhausts at 8.35m 
Layer 2: 100 xs 75 attaches at 8.35m and exhausts at 13.5m 
Layer 3: 44 xs 175 attaches at 13.5m and exhausts at 15.85m 
Layer 4: 144 xs 175 attaches at 13.5m and exhausts at 21.43m 
Layer 5: 148 xs 319 attaches at 21.43m and exhausts at 30.55m 
Layer 8: 40 xs 35 attaches at 6.04m and exhausts at 8.35m 
Layer 9: 65 xs 75 attaches at 8.35m and exhausts at 11.67m 
Layer 10: 100 xs 140 attaches at 11.67m and exhausts at 17.m 
Layer 11: 148 xs 240 attaches at 17.m and exhausts at 25.51m 
Layer 12: 162 xs 140 attaches at 11.67m and exhausts at 20.46m 
Layer 13: 100 xs 35 attaches at 6.04m and exhausts at 11.41m 
Layer 14: 15 xs 35 attaches at 6.04m and exhausts at 6.98m 

코드 만이 COL1이되는 않는 경우를 반환

Layer 1: 25 xs 50 attaches at 6.98m and exhausts at 8.35m 
Layer 2: 100 xs 75 attaches at 8.35m and exhausts at 13.5m 
Layer 3: 44 xs 175 attaches at 13.5m and exhausts at 15.85m 
Layer 4: 144 xs 175 attaches at 13.5m and exhausts at 21.43m 
Layer 5: 148 xs 319 attaches at 21.43m and exhausts at 30.55m 
Layer 6: 25 xs 50 attaches at 6.98m and exhausts at 8.35m 
Layer 7: 100 xs 75 attaches at 8.35m and exhausts at 13.5m 
Layer 8: 40 xs 35 attaches at 6.04m and exhausts at 8.35m 
Layer 9: 65 xs 75 attaches at 8.35m and exhausts at 11.67m 
Layer 9: 25 xs 50 attaches at 6.98m and exhausts at 8.35m 
Layer 10: 100 xs 140 attaches at 11.67m and exhausts at 17.m 
Layer 11: 148 xs 240 attaches at 17.m and exhausts at 25.51m 
Layer 12: 162 xs 140 attaches at 11.67m and exhausts at 20.46m 
Layer 13: 100 xs 35 attaches at 6.04m and exhausts at 11.41m 
Layer 14: 65 xs 75 attaches at 8.35m and exhausts at 11.67m 
Layer 14: 15 xs 35 attaches at 6.04m and exhausts at 6.98m 
Layer 15: 25 xs 50 attaches at 6.98m and exhausts at 8.35m 
Layer 16: 65 xs 75 attaches at 8.35m and exhausts at 11.67m 

수 제거 할 숫자로 대체되고 열 번호가 동일하기 때문에 문자열이 균등하게 나뉘어지면 문자열이 만들어집니다.

For Each cell In wb.Sheets("RP Analysis").Range("F5:F" & lastRow) 

RSet col1 = WorksheetFunction.RoundDown(cell.Value, 2) 
RSet col2 = WorksheetFunction.RoundDown(cell.Offset(0, 2).Value/1000000, 2) 
RSet col3 = WorksheetFunction.RoundDown(cell.Offset(0, 3).Value/1000000, 2) 
RSet col4 = Format$(WorksheetFunction.RoundDown(cell.Offset(0, 10).Value, 2), "#.##") 
RSet col5 = Format$(WorksheetFunction.RoundDown(cell.Offset(0, 11).Value, 2), "#.##") 
RSet col6 = Format$(WorksheetFunction.RoundDown(cell.Offset(0, 6).Value, 2), "#.##") 
RSet col7 = Format$(WorksheetFunction.RoundDown(cell.Offset(0, 7).Value, 2), "#.##") 



RMS = RMS & "Layer " & col1 & ":" & col2 & " xs " & col3 & " attaches at " & col4 & "m and exhausts at " & col5 & "m" & vbLf 

AIR = AIR & "Layer " & col1 & ":" & col2 & " xs " & col3 & " attaches at " & col6 & "m and exhausts at " & col7 & "m" & vbLf 

Next cell 

For Each cell In wb.Sheets("RP Analysis").Range("A9:A" & 19) 
    RSet col9 = Format$(WorksheetFunction.RoundDown(cell.Value, 2), "#####") 
     gucurve = gucurve & col9 & ":- " & Format(cell.Offset(0, 2).Value/cell.Offset(0, 1).Value, "Percent") & vbLf 
Next cell 

AIRmod = DeDupeString(AIR, vbLf) 
RMSmod = DeDupeString(RMS, vbLf) 

TextBox1.Value = "RP years RMS/AIR difference" & vbLf & gucurve & vbLf & RMSmod & vbLf & AIRmod 


End Function 

다음은 중복을 제거 내 기능입니다 완벽하게 이것은 Scripting.Dictionary 함께 할

Function DeDupeString(ByVal sInput As String, Optional ByVal sDelimiter As String = ",") As String 

Dim varSection As Variant 
Dim sTemp As String 

For Each varSection In Split(sInput, sDelimiter) 
    If InStr(1, sDelimiter & sTemp & sDelimiter, sDelimiter & varSection & sDelimiter, vbTextCompare) = 0 Then 
     sTemp = sTemp & sDelimiter & varSection 
    End If 
Next varSection 

DeDupeString = Mid(sTemp, Len(sDelimiter) + 1) 

End Function 
+0

리본 탭에서 데이터 | RemoveDuplicates 및 대화 상자가 나타나면 첫 번째 열의 선택을 취소하십시오 –

+0

VBA 코드를 원하면 간단한 매크로를 기록하십시오. –

+0

주어진 예상 출력에서 ​​중복 기준을 이해하고 있는지 잘 모르겠습니다. 레이어 7과 레이어 16이 각각 레이어 2와 레이어 9의 중복이 아닌 이유가 있습니까? – Comintern

답변

0

쉬운 방법 중 하나를 사용할 수 있습니다. 이 키를 사용하여 관심있는 문자열 부분을 복제본으로 저장하고 원래 값을 항목으로 저장할 수 있습니다. 이런 식으로 뭔가 작업을해야합니다 :이 늦은 바인딩되는

Private Function DeDupSections(ByVal raw As String) As String 
    Dim deduped As Object 
    Set deduped = CreateObject("Scripting.Dictionary") 

    Dim section As Variant 
    Dim test As String 
    For Each section In Split(raw, vbLf) 
     If Len(section) > 9 Then 
      test = Right$(section, Len(section) - 9) 
      If Not deduped.Exists(test) Then 
       deduped.Add test, section 
      End If 
     End If 
    Next 

    DeDupSections = Join(deduped.Items, vbLf) & vbLf 
End Function 

참고. 처음 두 줄을 ...로 변경하여 초기 경계로 변경할 수 있습니다.

Dim deduped As Scripting.Dictionary 
Set deduped = New Scripting.Dictionary 

... "Microsoft Scripting Runtime"에 대한 참조를 추가하십시오.

+0

나는 AIRmod = DeDupSections (AIR)로 가지고 있던 것을 대체 할 수 있을까요? 나는 그것을했다. 그리고 그것은 나에게 "invalid procedure call 또는 argument"를 준다. – bossman1111

+0

@ bossman1111 - 그래, 그것은 단지'AIRmod = DeDupSections (AIR)'이어야만한다. 'DeDupSections' 함수에서 "유효하지 않은 프로 시저 호출 또는 인수"입니까? 아니면 호출 사이트에서입니까? – Comintern

+0

@ bossman1111 - 편집보기 - 문자열 끝에 여분의'vbLF'가 없으므로 빈 요소가 있습니다. – Comintern

관련 문제