I would like to split my addresses into separate cells. My address is combined by commas and basically, the number of separate cells is comma-dependant.
I found and implemented a very good solution, which is under the following thread here:
It works, but the primary condition is keeping the string with the same number of commas.
If for example, the address looks like this:
1 - 40 Williams Court, 24-26 Poole Road, Bournemouth, BH4 9DT
then it's fine (regarding my whole macro),
but when the address is shorter (including 2 instead of 3 commas in the whole string)
12 Boyd Close, Coventry, CV2 2NF
then in turn I am getting a mess like below:
So I need the if statement, which will allow me to distinguish the shorter and longer address strings.
I've prepared one column, where I defined the number of commas.
Regarding this I tried to implement the following code:
Dim Wksht As Worksheet
Dim MyArray() As String, myPath As String
Dim lRow As Long, i As Long, j As Long, c As Long
Set Wksht = ThisWorkbook.Sheets("Final")
Set Wksht = ThisWorkbook.Sheets("Final")
Sheets("Address").Application.Union(Columns("J"), Columns("P"), Columns("O")).Copy
Wksht.Columns("A:B").PasteSpecial xlPasteValues
Wksht.Columns("A").ColumnWidth = 60
Wksht.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Wksht.Columns("A:D").Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAbove
Dim LastRow As Long, i As Long
With Wksht
LastRow = .Cells(.Rows.Count, "F").End(xlUp).row
End With
For i = 1 To LastRow
If ActiveSheet.Range("U" & i) = 3 Then
With Wksht
lRow = .Range("E" & .Rows.Count).End(xlUp).row
For i = 1 To lRow
If InStr(1, .Range("E" & i).Value, ",", vbTextCompare) Then
MyArray = Split(.Range("E" & i).Value, ",")
c = 1
For j = 0 To UBound(MyArray)
.Cells(i, c).Value = MyArray(j)
c = c + 1
Next j
End If
Next i
End With
End If
Next i
There is no error at all. The debugger just shows:
If InStr(1, .Range("E" & i).Value, ",", vbTextCompare) Then
but I don't understand, why am I receiving empty columns. Why this code is not executed at all?
I want these addresses split regarding the number of commas defined in the separate column.
UPDATED:
This approach also doesn't work
For i = 1 To lLastRow
If Wksht.Range("F" & i).Value = 2 Then
Wksht.Range("C" & i).Value = Wksht.Range("D" & i).Value
End If
Next i