Solved

one column into diffrent rows

Posted on 2003-11-25
2
243 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

708 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

13 Experts available now in Live!

Get 1:1 Help Now