Prepare Macros which add comma after every 10 digit in a column

 Question:

was trying to add a comma after every 10 digits in a column. However I have succeeded in creating macros.
Meanwhile I am facing another challenge because the data has 20 columns (Range A1:A20)Raw Data snapshot and I need to run the macros each time by clicking on different columns.
Please help whether it can be done in a single macro for all the columns.
 


 Answers: 


Please try this, it should work for you,

Sub AddCommas()
Dim s As String
Dim i As Long
Dim j As Long
For k = 1 To 20
For i = 1 To Rows.Count
    s = ""
    If Not IsEmpty(Cells(i, k).Value) And Int(Len(Cells(i, k)) / 10) >= 1 Then
        For j = 1 To Int(Len(Cells(i, k).Value) / 10) + 1
            If j = 1 Then
                s = s & Left(Cells(i, k).Value, j * 10) & ","
            Else
            If j = (Int(Len(Cells(i, k).Value) / 10) + 1) Or (Len(Cells(i, k).Value) / 10) = j Then
                s = s & Mid(Cells(i, k).Value, ((j - 1) * 10) + 1, 10)
            Else
                s = s & Mid(Cells(i, k).Value, ((j - 1) * 10) + 1, 10) & ","
            End If
            End If
        Next j
        Cells(i, k).Value = s
    End If
Next i
Next k
End Sub


Adjust the k variable and i variable as per your starting row position and number of columns. Hope this helps.

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net