I am currently splitting strings containing 3 elements (a/b/c) from Worksheet 1 and pasting each part on Worksheet 2 in different columns. This is done by a Loop.
However, if the string has only 2 elements, so "c" from above is empty, I get a Runtime Error 9: "Index out of Range".
The string can and will sometimes only have 1 or 2 elements instead of all 3. Is there any way I can avoid this error?
My research led me to try If Len(Trim()) = vbnullstring and Len() = 0 but nothing worked.
Any help would be greatly appreciated!
For Each IDrow In wsInput.Range(IDcolLetter & "2:" & IDcolLetter & lastRow)
'Fourthly, get the respective row-number for each skill
IDrowNumber = Split(IDrow.Address, "$")(2)
'Fifthly, split the strings in 3 parts
Dim myElements() As String
myElements = Split(wsInput.Range(IDcolLetter & IDrowNumber).value, "\")
'Sixthly, for every skill of that supplier, copy the ID in A, CG in B, Category in C and Product in D
NextRow = ws4.Range("A" & Rows.Count).End(xlUp).row + 1
If Len(myElements(2)) = 0 Then <<<<<<<<<<<<<<<<<<<<<ERROR HERE<<<<<<<<<<<<<<<<<<<<<<<
wsInput.Range(IDcolLetter & "1").Copy Destination:=ws4.Range("A" & NextRow) 'ID
ws4.Range("B" & NextRow) = myElements(0) 'Commodity Group
ws4.Range("C" & NextRow) = myElements(1) 'Category
Else
wsInput.Range(IDcolLetter & "1").Copy Destination:=ws4.Range("A" & NextRow) 'ID
ws4.Range("B" & NextRow) = myElements(0) 'Commodity Group
ws4.Range("C" & NextRow) = myElements(1) 'Category
ws4.Range("D" & NextRow) = myElements(2) 'Product
End If
Next IDrow