2016-03-30 9 views
-2

Ich brauche Hilfe, Code zu schreiben für Wert von Zelle zu finden und platzieren zu einer anderen Zelle entsprechenden WertWie finde ich den Wert aus der Zelle und platziere den entsprechenden Wert mit VBA in einer anderen Zelle?

Angenommen Zelle A Daten wie erwähnt enthalten unter

ControlM: servername: Jobname: Fehlertyp (nicht OK Ended, longrunning ...)

Ich möchte den Fehlertyp aus dem Zellinhalt prüfen und bestimmte zugehörige Werte in eine andere Zelle legen.

Antwort

0

Die Formel, die Sie wollen, ist:

=RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,":","~",LEN(A1)-LEN(SUBSTITUTE(A1,":",""))))) 

Offensichtlich "A1" ändern, was auch immer für Zelle Sie verweisen.

Sie können VBA verwenden diese Formel in die Zelle einzufügen, die Sie schätzen möchte erscheinen

0

Ich bin nicht sicher, ob dies ist, was Sie wollen, aber versuchen Sie dies:.

Sub printErrorCode() 

Dim errorType, errorCode As Variant 'define two variant variable to store errors and codes 
Dim errorPosition As Integer 'define errorPosition which will return the position of the searched string 

With ThisWorkbook.Sheets("YourSheetName") 'In my example I worked with a single sheet so I will avoid specifying the workbook and the sheet in every line 

errorType = .Range("ListOfErrorsRange").Value 'store possible error types 
errorCode = .Range("ListOfErrorCodesRange").Value 'store associated codes 

For i = LBound(errorType) To UBound(errorType) 'from the first error type to the last 
    errorPosition = InStr(1, .Range("YourStringRange"), errorType(i, 1), vbTextCompare) 'return the position of errorType String in YourRange string if found, or 0 if not found 
    If errorPosition <> 0 Then 'if errorPosition <>0 it found the string 
     .Range("YourStringRange").Offset(0,1).Value = errorCode(i, 1) 'if errorType string found return corresponding errorCode 
     Exit Sub 'exit the routine 
    End If 
Next i 

MsgBox "Error Not Found In List" 'if errorPosition = 0 for all i, then the Error was not on the list of errors 

End With 

End Sub 
0

Nachfolgend finden der Code müssen Sie in Modul einzufügen

Public Function getcontent(r As Range) As String 
    str1 = InStr(r.Cells.Value, "failure type") 
    If str1 > 0 Then 
     str2 = Mid(r.Cells.Value, str1 + 13, Len(r.Cells.Value)) 
     getcontent = str2 
    Else 
     getcontent = "" 
    End If 
End Function 

nehmen den Inhalt "ControlM: servername: Jobname: Fehlertyp (nicht OK Ended, longrunning ...)" in Zelle A1

in Zelle A2, = getcontent (A1)

Sie erhalten den Inhalt des Fehlertyps allein.

Hoffe, das ist inline mit Ihren Anforderungen.

Verwandte Themen