I have a piece of code that is used to split up the text in a cell. The data is outputted by a survey program that doesn't make use of any useful delimiter, so unfortunately converting text to columns isn't of any help to me.
I wrote this piece of code, but it turns out that the outcomes are different in two cases.
I run the code step by step until the first column is added, and then let it finish
I run the code from the execute macro's menu
In the first case the output is as I designed it to be. After a column with the header Crop: XXX
(Which contains the raw data that needs to be split) there are columns for each separate entry numbered 1 to X. Data from every row starts to be split in column X and then moves as far as there are entries. Like this:
| Crop XXX | 1 | 2 | 3 | 4 |
|-------------|----|----|----|----|
| X1,X2,X3 | X1 | X2 | X3 | |
| X1,X2,X3,X4 | X1 | X2 | X3 | X4 |
In the second case all columns are numbered 1, and every new row enters its data before the data of the previous row. Like such:
| Crop XXX | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|-------------|----|----|----|----|----|----|----|
| X1,X2,X3 | | | | | X1 | X2 | X3 |
| X1,X2,X3,X4 | X1 | X2 | X3 | X4 | | | |
The code I use to input and number these columns is this:
If Not UBound(inarray) = 0 Then
For i = UBound(inarray) To LBound(inarray) Step -1
If ws.Cells(1, col + i).Value = i Then
If i = UBound(inarray) Then
Exit For
End If
col_to_add = col + i + 1
Exit For
Else
addcol = addcol + 1
End If
col_to_add = col + i
Next i
If Not i = UBound(inarray) Then
col1 = ConvertToLetter(col_to_add)
col2 = ConvertToLetter(col_to_add + addcol - 1)
ws.Columns(col1 & ":" & col2).Insert shift:=xlToRight
End If
If Not addcol = 0 Then
For j = 1 To addcol
If col_to_add = col + j Then
If j = 1 Then
ws.Cells(1, col_to_add) = 1
Else
ws.Cells(1, col_to_add + j - 1) = Cells(1, col_to_add + j - 2).Value + 1
End If
Else
ws.Cells(1, col_to_add + j - 1) = Cells(1, col_to_add + j - 2).Value + 1
End If
Next j
End If
For k = UBound(inarray) To LBound(inarray) Step -1
ws.Cells(row, col + k) = inarray(k)
Next k
End If
In this example Inarray()
is a 1d array containing the below values for the first row:
| Inarray() | Value |
|-----------|-------|
| 1 | X1 |
| 2 | X2 |
| 3 | X3 |
ConvertToLetter
is the following function:
Function ConvertToLetter(iCol As Integer) As String
Dim vArr
vArr = Split(Cells(1, iCol).Address(True, False), "$")
ConvertToLetter = vArr(0)
End Function
Could anyone point out why this difference occurs between scenario 1 and 2? Usually these things happen when objects aren't fully classified, but I thought I tackled that problem this time.