+ Reply to Thread
Results 1 to 5 of 5

Convert table data to rows (csv)

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2014
    Location
    London, UK
    MS-Off Ver
    2016
    Posts
    54

    Convert table data to rows (csv)

    Hi All,
    Hope you are well and keeping safe.

    I need some help converting a table into rows.

    I have attached an example with some data which hope helps understand.

    1. Prompt user for 'Start Date', then 'Number of days'. Default will be 7.
    2. Find date in Row 1 (Sheet1).
    3. Copy/paste 'Dates' to Sheet3 (Date).
    4. Check the 'Number' column (Sheet1) up to the last number - so anything up to 'Reserve'.
    5. Copy to Sheet3 (same name).
    Note: Numbers may be added/removed (Sheet2) - so is important for the count up to 'Reserve' - Reserve excluded.
    6. Copy each column to match Date/Number.

    For example;
    Start Date = 4/2/21
    Number of days = 4
    Result = See Sheet4

    Hopefully this makes sense and someone can help soon to save me from this laborious task!

    Thanks in advance.

    Best,
    Al
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,244

    Re: Convert table data to rows (csv)

    Ad 1. "Convert table data to rows (csv)"
    But what does the "csv" format have to do with it - there is no "csv" here, just a simple pasting of the transposed data into the worksheet ?


    Ad 2. "4/2/21" => ??? (d/m/yy) ???
    What format is this date in: d/m/yy, m/d/yy, y/m/dd ?
    Such a format is so enigmatic that it is not known how to control it later, there will be errors in the data
    That would be better => dd/mm/yyyy => "04/02/2021" or mm/dd/yyyy => "02/04/2021"

  3. #3
    Registered User
    Join Date
    12-17-2014
    Location
    London, UK
    MS-Off Ver
    2016
    Posts
    54

    Re: Convert table data to rows (csv)

    Thanks Porucha Vevrku.

    Ad 1 - that sheet will be saved in csv format to upload elsewhere.

    Ad 2 - Date format = 'dd/mm/yyyy' please.

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,244

    Re: Convert table data to rows (csv)

    Try what's below, but I don't know how this code will react to the date system in your os, there is always a problem with that (different system date formats).
    Protections against false data/date are only symbolic, so don't try to sabotage that code.
    Option Explicit
    
    Sub jkl()
        '------------------ Declarations --------------------------
        Const lstdtemsg = "The number of available dates (counted from the starting date)" & _
                            vbNewLine & "is outside the acceptable range"
        Const prmpt = "Enter the start date and number of days (data separated by a semicolon)"
        Const ttl = "Enter data" & " in format: dd/mm/yyyy;Number of days"
        Const dflt = "04/02/2021;4"
        Const cpc = "Reserve"
        
        Dim c As Long, n As Long, nd As Long, r As Long, rw As Long
        Dim cls, rws, sdat, vdat
        
        '------------------ Input data ----------------------------
        sdat = Replace(InputBox(prmpt, ttl, dflt), " ", vbNullString, 1, -1, 1)
        
        '------------------ Verification of data ------------------
        sdat = Split(sdat, ";", -1, 1)
        
        If UBound(sdat) <> 1 Then MsgBox "Incorrect data scheme - The End": Exit Sub
        
        If Not IsNumeric(sdat(1)) Then MsgBox "Incorrect value for the number of days - The End": Exit Sub
        
        If Not sdat(0) Like "##/##/####" Then MsgBox "Incorrect date scheme - The End": Exit Sub
        c = CLng(Left(sdat(0), 2)): n = CLng(Mid(sdat(0), 4, 2)): r = CLng(Right(sdat(0), 4))
        If (c < 1 Or c > 31) Or (n < 1 Or n > 12) Or (r < 1900 Or r > 2100) Then
            MsgBox "Incorrect date elements - The End": Exit Sub
        End If
        If Not IsDate(sdat(0)) Then MsgBox "Incorrect date - The End": Exit Sub
        
        '------------------ Creating a data set -------------------
        nd = Val(sdat(1))
        sdat = DateSerial(r, n, c)
        c = 0: n = 0: r = 2
        
        On Error Resume Next
            With Sheets("Sheet1").Range("A1").CurrentRegion
                rw = .Columns(1).Find(cpc, , xlValues, xlPart, xlByColumns).Row
                    
                    If Err.Number <> 0 Then MsgBox "No reference point - The End": Exit Sub
                    
                c = .Rows(1).Find(CStr(sdat), , xlValues, xlWhole, xlByRows).Column
                    
                    If Err.Number <> 0 Then MsgBox "No date you are looking for - The End": Exit Sub
                    If c + (nd - 1) > .Columns.Count Then MsgBox lstdtemsg: Exit Sub
                    
                sdat = Empty: sdat = .Resize(rw - 1, .Columns.Count).Value
            End With
        On Error GoTo 0
        
        rws = Evaluate("Row(1:" & rw - 1 & ")")
        cls = "{1," & c
        For n = c + 1 To c + (nd - 1)
            cls = cls & ("," & n)
        Next
        cls = Evaluate(cls & "}")
        
        vdat = Application.Index(sdat, rws, cls)
        cls = Empty: rws = Empty: sdat = Empty
        
        c = 0: rw = rw - r
        ReDim sdat(1 To nd * rw, 1 To 3)
        
        For n = 1 To nd
            For r = 1 To rw
                c = c + 1
                sdat(c, 1) = Format(vdat(1, n + 1), "mm/dd/yyyy")
                sdat(c, 2) = vdat(r + 1, 1)
                sdat(c, 3) = vdat(r + 1, n + 1)
            Next
        Next
        
        '------------------ Output data ---------------------------
        With Sheets("Sheet3")
            With .Range("A1")
                .CurrentRegion.ClearContents
                .Resize(1, 3).Value = Array("Date", "Number", "Value")
                .Offset(1, 0).Resize(c, 3).Value = sdat: sdat = Empty
                .CurrentRegion.EntireColumn.AutoFit
            End With
            .Select
        End With
    End Sub
    Ps:
    My nickname is just a nickname, it does not require typing with proper-capital letters for first letters

  5. #5
    Registered User
    Join Date
    12-17-2014
    Location
    London, UK
    MS-Off Ver
    2016
    Posts
    54

    Re: Convert table data to rows (csv)

    Thanks, I'll give that a go.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Convert Long Rows into Compact Table
    By bwiebe in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-13-2018, 12:45 PM
  2. [SOLVED] Convert multiple rows of horizontal data into three rows of vertical data
    By msutton in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-10-2015, 03:27 AM
  3. Convert some columns to pivot table rows?
    By dalfiuss in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 06-02-2015, 02:54 PM
  4. [SOLVED] Convert Rows to a Table
    By Seraph122 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2013, 03:45 AM
  5. How to convert XML files into an EXCEL table (rows)?
    By pstein in forum Excel General
    Replies: 0
    Last Post: 01-10-2013, 03:48 AM
  6. Replies: 1
    Last Post: 11-29-2012, 06:24 PM
  7. Replies: 6
    Last Post: 08-18-2012, 05:00 AM

Tags for this Thread

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