mercredi 9 novembre 2016

VBA search for value in a column after specific pattern

I have input data like this:

gen,N,,,GONGD,,,N,,,KL,0007bd,,,,,,,,TAK,
gen,N,,,RATEC,,,N,,,KP,0007bc,,,,,,,,TAZ,
kap,N,,,EBFWE,,KP,002bd4,N,,,,,,,,,KP,002bd5,,,KP,123000,,,,,N,,,,P
kap,N,,,ST,WEIT,E3,EBFWEI,,,KP,002bd2,N,,,,,,KP,002bd3,,,,,,,Z,MG00,,,,,N,,,,P

One line is in one column. First I need to know I am in a row starting with "kap" and then I need to save 6 characters (or numbers) after first ",KP," as "Variable1" and save also the 6 chars or numbers after the second ",KP," as "Variable2". Please help me, I am hopeless.

Expected result: Var1 = 002bd4, Var2 = 123000 for the first "kap" row. Var3 = 002bd2, Var4 = 002bd3

I have tried this to know I am in "kap" starting line:

Sub Find()
Dim rFoundAddress As Range
Dim sFirstAddress As String
Dim x As Long

With ThisWorkbook.Worksheets("Sheet1").Columns(1)
    Set rFoundAddress = .Find("msz,*", LookIn:=xlValues, LookAt:=xlWhole)
    If Not rFoundAddress Is Nothing Then
        sFirstAddress = rFoundAddress.Address
        Do


            Set rFoundAddress = .FindNext(rFoundAddress)
        Loop While Not rFoundAddress Is Nothing And _
            rFoundAddress.Address <> sFirstAddress
    End If
End With
End Sub

THANKS!

Aucun commentaire:

Enregistrer un commentaire