0

The original functioning code by VBasic2008 in the accepted answer.

I thought I'd understood the code, and have often modified it as necessary without problem. I'm now having trouble with the following:

For r = 1 To rCount
     rString = CStr(sData(r, 3))
       If InStr(rString, "Completed") = 1 Then    ' if cell r in column C has the string "Completed"
      nIndex = Application.Match(rString, Faelle, 0)
        If IsNumeric(nIndex) Then dData(r, 1) = Werte(nIndex - 1)
    End If
Next r

I modified the code by shortening the loop to meet just one condition, and that condition is to check wether the cell does contain a specific string (not whether the cell is empty).

when I run the code, nothing happens. I don't get any error message, so I can't figure out why it's behaving like that.

Here's the complete sub:

Sub setStatus()
    
    Dim wb          As Workbook: Set wb = ThisWorkbook
    Dim ws          As Worksheet: Set ws = wb.Worksheets(3)
    Dim rg          As Range: Set rg = ws.Range("A1").CurrentRegion
    
    Dim rCount      As Long: rCount = rg.Rows.Count
    Dim srg         As Range: Set srg = rg.Resize(rCount)
    Dim sData()     As Variant: sData = srg.Resize(, 3).Value
    Dim dData()     As Variant: ReDim dData(1 To rCount, 1 To 1)
    Dim nIndex      As Variant, r As Long, rString As String
    
    Dim Faelle()    As Variant: Faelle = VBA.Array("Status1", "Status2", "Status3", "Status4", "Status5")
    Dim Werte()     As Variant: Werte = VBA.Array(1, 2, 3, 4, 5)
    

For r = 1 To rCount
    rString = CStr(sData(r, 3))
    If InStr(rString, "Completed") = 1 Then        ' if cell r in column C has the string "Completed"
    nIndex = Application.Match(rString, Faelle, 0)
    If IsNumeric(nIndex) Then dData(r, 1) = Werte(nIndex - 1)
End If
Next r

Dim drg             As Range: Set drg = srg.Columns(6)

drg.Value = dData

End Sub

To clarify, the purpose of this sub is, having a condition met, match the value of the cell/first array with the values of the second array. The values of the second array should be inserted in the corresponding cell in column 6.

maliebina
  • 205
  • 6
  • `InStr(rString, "Completed") = 1` means `rString` starts with "Completed" rather than contains it. – GSerg Mar 03 '23 at 13:16
  • That makes sense, thanks! I've tried both `If rString = "Completed" Then` and `If StrComp(rString, "Completed", vbBinaryCompare) = 0 Then`, but neither seems to work, either. Is there a special function I should be using for this specific loop instead? @GSerg – maliebina Mar 03 '23 at 13:28
  • 2
    Use `If InStr(rString, "Completed") > 0 Then` or just `If InStr(rString, "Completed") Then` or alternatively `If rString Like "*Completed*" Then` – CDP1802 Mar 03 '23 at 13:30

0 Answers0