2

よろしければ、以下の点にご協力いただけますと幸いです。

ファイル命名システムで使用される個別の変数として設定できるように、VBA で一部のデータを分離する必要があります。

次のコードがあります。

    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

最初の 3 つの strVal は、コードをテストするためにコードを実行するデータの 3 つの異なる選択肢です。\ の数は状況によって異なる場合があります。

必要な結果は次のとおりです。

データセット 1 strVal2 = KingMcManaman strVal3 = Nicolson

データセット 2 strVal2 = f strVal3 = A5 KHAKI

データセット 3 strVal2 = BPD strVal3 = A5 GROUP

私は運がなかったので、あなたの意見に感謝します。

よろしく、

サム

4

2 に答える 2

7

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

等々...

于 2013-03-18T17:23:07.403 に答える
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
于 2013-03-18T17:46:44.857 に答える