arraysexcelvbacurrency

Conversion currency in Array


In the cell I have mixed data: amount and after a space the data. I have developed a vba code to extract this data to the cells next to it. The code works well - the data is extracted and the amount is converted to currency. But it sometimes happens that the conversion to the amount is not performed. What can this depend on?

    Sub Rozdzielenie2()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Dim Tablica() As String
Dim Dane As Range
Dim i As Integer
Dim j As Integer: j = ActiveCell.Row

Set Dane = ActiveCell
    Tablica = Split(Dane.Value, Chr(10))
        For i = 1 To UBound(Tablica) + 1
            Arkusz1.Range("C" & j).Value = Tablica(i - 1)
            Arkusz1.Range("D" & j) = "=LEFT(RC[-1],LEN(RC[-1])-10)"
            Arkusz1.Range("D" & j).Value = Trim(CCur(Arkusz1.Range("D" & j)))
            Arkusz1.Range("E" & j) = "=RIGHT(RC[-2],10)"
            Arkusz1.Range("F" & j) = "=DATEVALUE(RC[-1])"
            Arkusz1.Range("F" & j).Value = CDate(Arkusz1.Range("F" & j))
            
            Selection.Offset(1, 0).Select
            Selection.EntireRow.Insert
           
            j = j + 1
        Next i
    Selection.EntireRow.Delete
    Columns("F:F").NumberFormat = "dd/mm/yyyy r."
    Columns("D:D").NumberFormat = "#,##0.00 zł"

'Columns("E").Delete
'Columns("C").Delete

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub

The amount 745.20 is not converted

enter image description here


Solution

  • It most likely has something to do with additional spaces your code is not removing. Using LEFT and RIGHT is not a reliable way to extract strings. I would suggest parsing it in a different way to make sure that you hate the right values to process. Something like that could work for you:

    Sub SplitLines()
        Dim ws As Worksheet
        Dim lines As Variant, parts As Variant
        Dim lineText As String, amountText As String, dateText As String
        Dim firstRow As Long, lastRow As Long
        Dim r As Long, i As Long, numLines As Long
    
        Set ws = ActiveSheet
        firstRow = ActiveCell.Row
        lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
        For r = lastRow To firstRow Step -1
            If ws.Cells(r, "A").Value <> "" Then
                lines = Split(ws.Cells(r, "A").Value, vbLf)
                numLines = UBound(lines) - LBound(lines) + 1
                If numLines > 1 Then
                    ws.Rows(r + 1 & ":" & r + numLines - 1).Insert Shift:=xlShiftDown
                End If
                For i = LBound(lines) To UBound(lines)
                    lineText = Trim(lines(i))
                    lineText = Application.Trim(lineText)
                    parts = Split(lineText, " ")
                    If UBound(parts) >= 1 Then
                        amountText = parts(0)
                        dateText = parts(1)
                    Else
                        amountText = lineText
                        dateText = ""
                    End If
                    ws.Cells(r + i, "B").Value = lineText
                    ws.Cells(r + i, "C").Value = CCur(amountText)
                    ws.Cells(r + i, "D").Value = dateText
                    If dateText <> "" Then ws.Cells(r + i, "E").Value = CDate(dateText)
                Next i
            End If
        Next r
    
        ws.Columns("C").NumberFormat = "#,##0.00 zl"
        ws.Columns("E").NumberFormat = "dd/mm/yyyy"
    End Sub
    
    

    This is just an example based on your code, you can easily remove some of the intermediate steps.