Solved

one column into diffrent rows

Posted on 2003-11-25
2
244 Views
Last Modified: 2010-05-03
Hi,
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,
Maha
0
Comment
Question by:mahalakshmi_s
2 Comments
 
LVL 28

Expert Comment

by:vinnyd79
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
Else
    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
Else
    TmpStr = TmpStr & "," & Ln
End If
Loop
Print #tf, TmpStr
Close #ff
Close #tf
Next i

End Sub
0
 
LVL 2

Accepted Solution

by:
rekhasri earned 50 total points
ID: 9822937
Hi,

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
Wend

Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)

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

End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now