one column into diffrent rows

Posted on 2003-11-25
Last Modified: 2010-05-03
I have a "file1.csv" that has 1 column which has 10 values (rows).Now I want a VB prg to read file1.csv and make the 10 rows as 1 row and 10 columns(transposing).I need thi sin loop as I have to do transposing for 5000 CSV files.
Pls help asap,
Question by:mahalakshmi_s
LVL 28

Expert Comment

ID: 9820149
If you add a FileListbox you can loop through it and write each file to a new file.This will create a new file with the prefix "new" in front of each filename:

Private Sub Form_Load()
File1.Pattern = "*.csv"
File1.Path = "C:\Somefolder"
End Sub

Private Sub Command1_Click()
Dim i As Integer, ff As Integer, tf As Integer
Dim Ln As String, TmpStr As String
Dim origFile As String, newFile As String
For i = 0 To File1.ListCount - 1

If Right$(App.Path, 1) <> "\" Then
    origFile = File1.Path & "\" & File1.FileName
    newFile = File1.Path & "\new" & File1.FileName
    origFile = File1.Path & File1.FileName
    newFile = File1.Path & "new" & File1.FileName
End If

ff = FreeFile
Open origFile For Input As #ff
tf = FreeFile
Open newFile For Output As #tf
Do Until EOF(ff)
Line Input #ff, Ln
If i = 0 Then
    TmpStr = Ln
    TmpStr = TmpStr & "," & Ln
End If
Print #tf, TmpStr
Close #ff
Close #tf
Next i

End Sub

Accepted Solution

rekhasri earned 50 total points
ID: 9822937

ive just copied the file to a 2d array and written it back after transposing.

change the hardcodes acc to ur need

here is the code:

Private Sub Form_Load()

Dim fso     As FileSystemObject
Dim f       As File
Dim ts      As TextStream
Dim sIp     As String
Dim txt()   As String
Dim tempArr() As String
Dim i       As Integer
Dim j       As Integer
Dim k       As Integer
Dim l       As Integer
Dim sOp     As String

Set fso = New FileSystemObject
Set f = fso.GetFile("F:\trials\New Folder\ip.csv")

Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)

i = 0
ReDim txt(10, i) As String
While Not ts.AtEndOfStream
    sIp = ts.ReadLine
    tempArr = Split(sIp, ",", , vbTextCompare)
    For j = LBound(tempArr) To UBound(tempArr)
        txt(j, i) = tempArr(j)
    Next j
    i = i + 1
    ReDim Preserve txt(10, i) As String

Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)

For k = 0 To 9
    sOp = vbNullString
    For l = 0 To i - 1
        sOp = sOp & txt(k, l) & ","
    sOp = Mid$(sOp, 1, Len(sOp) - 1)
    ts.WriteLine (sOp)

End Sub

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question