Excel Macro - Basic Data Transformations

We often face situations where we have data available in rows and columns and we have to delimit it with single quotes or double quotes and separate the fields by comma.

The below code will automatically determine the end row and the end column and do the data transformations.



CODE 
Option Explicit Sub Generateresult() Dim row, column As Integer Dim result As String row = 1 Do While TRUE result = " " column = 1 Do While TRUE result = result + '" + CStr(Sheet1.Cells(row, column)) + "'" column = column + 1 If (Sheet1.Cells(row, column) = "") Then Exit Do Else result = result + ", " End If Loop Sheet1.Cells(row, column + 2) = result row = row + 1 If (Sheet1.Cells(row, 1) = "") Then Exit Do End If Loop End Sub

Comments