VBA / Excel Find a string in a cell to determine the value of the next cell

advertisements

I need to find a string in a cell, and if that string is found, the cell next to it has to put that string in the next cell. In layman's terms:

IF "Medina" is found in a C3
    put "ME" in cell D3;
ELSEIF "Brunswick" is found in C3
    put "BR" in D3;
ELSE
    put "OTH"in D3;

And this has to go through out the sheet, i.e., C4 & D4, C5 & D5, etc.

Thanks in advance


Sounds like you're looking for the InStr function (see this link: https://msdn.microsoft.com/en-us/library/8460tsh1(v=vs.90).aspx). Basically we want to check if there is a non-zero answer for this, which indicates the string can be found.

So you could do something like below. Note I just put in 40 as your last row arbitrarily, but you can change this as you need.

Dim indStartRow As Integer
Dim indEndRow As Integer
Dim i As Integer

indStartRow = 3
indEndRow = 40

For i = indStartRow To indEndRow
    If InStr(Sheets("Sheetname").Range("C" & i).Value, "Medina") > 0 Then
        Sheets("Sheetname").Range("D" & i).value = "ME"
    ElseIf InStr(Sheets("Sheetname").Range("C" & i).Value, "Brunswick") > 0 Then
        Sheets("Sheetname").Range("D" & i).value = "BR"
    Else
        Sheets("Sheetname").Range("D" & i).value = "OTH"
    End If
Next i