+ Reply to Thread
Results 1 to 5 of 5

Convert rows to columns

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2013
    Location
    Nyköping, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    3

    Convert rows to columns

    Hello,

    I need to convert a .skv file containing three rows into one row with several columns.

    The file looks like this:
    D1000;286;AR;2
    ;0; 1
    END

    And I want it to look like this instead:

    D1000 286 AR 2 0 1 END

    Can somebody please help me with the the code for this operation?

    Regards
    Olov
    Last edited by Ledarn; 05-29-2013 at 03:55 AM. Reason: Solved!

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Convert rows to columns

    try this code, if problems attach please the file
    Sub a()
    folderPath = "C:\users\user\desktop\"
    Filename = "test.skv"
        s = CreateObject("scripting.filesystemobject").opentextfile(folderPath & Filename).readall
        s1 = Replace(s, vbCrLf, ";")
        sn = Split(s1, ";")
        For j = 0 To UBound(sn)
            sp = Split(sn(j), ";")
            u = UBound(sp)
            If u <= 0 Then u = 0
            Cells(1, j + 1).Resize(u + 1) = sp
        Next
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-14-2013
    Location
    Nyköping, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Convert rows to columns

    It works excellent when I have one file. I didin´t mention that I have several skv-files that I would like to convert and insert into one excel sheet.

    I have a code that imports all the skv-files but I want them to be transposed like your code does.

    My current code looks like this:

    Dim fPath   As String:      fPath = "P:\8203 Vrena Deponi\11 UNDERLAG\Loggdata från Vrena\2013\02\"    'path to SKV files, include the final \
    Dim fSKV    As String
    Dim wbSKV   As Workbook
    Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("Mätvärden")
    
    If MsgBox("Rensa befintliga mätvärden innan import?", vbYesNo, "Rensa?") _
        = vbYes Then wsMstr.UsedRange.Clear
    
    Application.ScreenUpdating = False  'speed up macro
    
    fSKV = Dir(fPath & "*.SKV")         'start the SKV file listing
    
        Do While Len(fSKV) > 0
          'open a SKV file
            Set wbSKV = Workbooks.Open(fPath & fSKV)
                        
            Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
               TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                Semicolon:=True, Comma:=False, Space:=False, Other:=False, OtherChar:="", _
                FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), _
                Array(5, 1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
            
            'insert col A and add SKV name
            Columns(1).Insert xlShiftToRight
            Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
                 
            'copy date into master sheet and close source file
           ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(2)
              
           wbSKV.Close False
          'ready next SKV
           fSKV = Dir
        Loop
     
    Application.ScreenUpdating = True
    End Sub
    So I want to have a loop that imports the skv-files, convert them into columns and then add the filename i a new column.

    Is that possible?

    //Olov

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Convert rows to columns

    Sub a()
    Dim fPath   As String
    Dim fSKV    As String
    Dim wbSKV   As Workbook
    Dim wsMstr  As Worksheet
    Set wsMstr = ThisWorkbook.Sheets("Mätvärden")
    fPath = "P:\8203 Vrena Deponi\11 UNDERLAG\Loggdata från Vrena\2013\02\"    'path to SKV files, include the final \
    If MsgBox("Rensa befintliga mätvärden innan import?", vbYesNo, "Rensa?") _
        = vbYes Then wsMstr.UsedRange.Clear
    
    Application.ScreenUpdating = False  'speed up macro
    
    fSKV = Dir(fPath & "*.SKV")         'start the SKV file listing
        r = 1
        Do While Len(fSKV) > 0
            Set wbSKV = Workbooks.Open(fPath & fSKV)
            s = CreateObject("scripting.filesystemobject").opentextfile(fPath & fSKV).readall
            s1 = Replace(s, vbCrLf, ";")
            sn = Split(s1, ";")
            For j = 0 To UBound(sn)
              sp = Split(sn(j), ";")
              u = UBound(sp)
              If u <= 0 Then u = 0
              Cells(r, j + 1).Resize(u + 1) = sp
            Next
          'ready next SKV
           fSKV = Dir
           r = r + 1
        Loop
     Application.ScreenUpdating = True
    
    End Sub
    attach a sample file with desired result

  5. #5
    Registered User
    Join Date
    05-14-2013
    Location
    Nyköping, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Convert rows to columns

    Thank you!

    I tweaked the code a little bit and made it work!

    The code that worked the way I wanted to looks like this:
    Sub a()
    Dim fPath   As String
    Dim fSKV    As String
    Dim wbSKV   As Workbook
    Dim wsMstr  As Worksheet
    Set wsMstr = ThisWorkbook.Sheets("Mätvärden")
    fPath = "P:\8203 Vrena Deponi\11 UNDERLAG\Loggdata från Vrena\2013\02\"    'path to SKV files, include the final \
    If MsgBox("Rensa befintliga mätvärden innan import?", vbYesNo, "Rensa?") _
        = vbYes Then wsMstr.UsedRange.Clear
    
    Application.ScreenUpdating = False  'speed up macro
    
    fSKV = Dir(fPath & "*.SKV")         'start the SKV file listing
        r = 1
        Do While Len(fSKV) > 0
            Set wbSKV = Workbooks.Open(fPath & fSKV)
            s = CreateObject("scripting.filesystemobject").opentextfile(fPath & fSKV).readall
            s1 = Replace(s, vbCrLf, ";")
            sn = Split(s1, ";")
            For j = 0 To UBound(sn)
              sp = Split(sn(j), ";")
              u = UBound(sp)
              If u <= 0 Then u = 0
              Cells(r, j + 1).Resize(u + 1) = sp
            Next
          
          'insert col A and add SKV name
            Columns(1).Insert xlShiftToRight
            Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
          
          'copy date into master sheet and close source file
          ActiveSheet.Range("A1:I1").Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
          
            wbSKV.Close False
          'ready next SKV
           fSKV = Dir
           'r = r + 1
        Loop
     Application.ScreenUpdating = True
    
    
    End Sub
    Thank you very much for you help!

    //Olov

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1