2013-03-18 2 views
2

다음 사항에 도움을 주셔서 감사합니다.VBA에서 문자열 분리 문제가 있습니다.

VBA에서 일부 데이터를 분리해야 파일 명명 시스템에서 사용할 별도의 변수로 설정할 수 있습니다. 시작시 세 strVal는 데이터가 코드를 테스트하기에 코드를 실행하기위한 3 개 가지 선택이 있습니다

Sub StripSlash1() 
Dim strVal As String, strVal2 As String, strVal3 As String, strVal4 As String 

'strVal = "jack\tom\rich\Nicolson\KingMcManaman" 
'strVal = "L:\Pictures\A B C\A5 GROUP\A5 KHAKI\f" 
strVal = "L:\Pictures\A B C\A5 GROUP\BPD" 

    Cells(2, 5).Formula = strVal 

    strVal2 = Right(strVal, InStr(strVal, "\") - 1) 
    Cells(2, 6).Formula = strVal2 

    strVal4 = Left(strVal, InStrRev(strVal, "\") - 1) 
    Cells(2, 7).Formula = strVal4 

    strVal3 = Right(strVal4, InStr(strVal4, "\") - 1) 
    Cells(2, 8).Formula = strVal3 


End Sub 

:

나는 다음과 같은 코드가 있습니다. \의 수는 상황에 따라 다를 수 있습니다.

우리가 필요로하는 결과는 다음

데이터 세트 1 strVal2 = KingMcManaman strVal3 = 니콜슨

데이터 세트 2 strVal2 = F strVal3 = A5 KHAKI

데이터 세트 3 strVal2 = BPD strVal3 = A5 GROUP

내가 입력 해 주셔서 감사합니다. 이 행운은 없었습니다.

감사합니다,

답변

8

당신의 상황에서 다음을 수행 Split 기능 사용을 고려하십시오 :

strVal = "L:\Pictures\A B C\A5 GROUP\BPD" 
Dim arrVal As Variant 
    arrVal = Split(strVal, "\") 

은의 일부를 얻을 수를 strVal 당신은 arrVal 배열임을 기억해야한다 :

strVal2 = arrVal(UBound(arrVal))   'result: BPD 
strVal3 = arrVal(UBound(arrVal)-1)  'result: A5 GROUP 

등등 ...

+0

감사합니다. 나는 이것을 사용하고 치료를했습니다. – SCGB

+0

+1 효율적으로 완료되었습니다. – brettdj

1
Sub Tester() 

    Dim s, arr 

    s = "aaa\bbb\ccc\d\eee" 
    arr = Split(s, "\") 

    With ActiveSheet.Cells(2, 5) 
     .Value = s 
     .Offset(0, 1).Resize(1, UBound(arr) + 1).Value = arr 
    End With 

End Sub 
+0

감사합니다. 나는 Kaz Jaw가 나를 위해 완벽하게 일했기 때문에 이전 답변으로 이것을 사용하지 않았다. – SCGB