+ Reply to Thread
Results 1 to 15 of 15

Extract multiple date and time values to multiple rows

Hybrid View

WilliamV Extract multiple date and... 10-09-2012, 06:03 AM
HaHoBe Re: Extraxt multiple date and... 10-09-2012, 10:52 AM
WilliamV Re: Extraxt multiple date and... 10-16-2012, 05:18 AM
HaHoBe Re: Extraxt multiple date and... 10-16-2012, 11:30 AM
WilliamV Re: Extract multiple date and... 10-17-2012, 09:13 AM
HaHoBe Re: Extract multiple date and... 10-17-2012, 02:31 PM
WilliamV Re: Extract multiple date and... 10-18-2012, 03:02 AM
HaHoBe Re: Extract multiple date and... 10-18-2012, 11:17 AM
WilliamV Re: Extract multiple date and... 10-29-2012, 04:52 AM
HaHoBe Re: Extract multiple date and... 11-04-2012, 08:02 AM
  1. #1
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Extract multiple date and time values to multiple rows

    In cell A1 there are 5 date and time values. How to extract all data to rows 1 to 5 etc... using VBA ?

    I'm not able to upload a file as example at his moment, so:

    e.g. cell A1 contains 27/08/2012 om 14:33 28/08/2012 om 18:33 29/08/2012 om 19:33 30/08/2012 om 16:33 01/09/2012 om 12:33 etc ...
    Row 1 needs to be extracted to rows 1 to 5 with values:
    • A1 - 27/08/2012 | B1 - 14:33
    • A2 - 28/08/2012 | B2 - 18:33
    • A3 - 29/08/2012 | B3 - 19:33
    • A4 - 30/08/2012 | B4 - 16:33
    • A5 - 01/09/2012 | B5 - 12:33


    Thank you.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Extraxt multiple date and time values to multiple rows

    Hi, WilliamV,

    if Data is always laid down as in your example and in this example there is enough empty cells under the given Data:
    Sub WilliamV()
    Dim varSplit
    Dim lngCounter As Long
    Dim lngOffset As Long
    
    lngOffset = 0
    varSplit = Split(ActiveCell.Value, " ")
    
    For lngCounter = LBound(varSplit) To UBound(varSplit) Step 3
      With ActiveCell.Offset(lngOffset, 0)
        .Value = varSplit(lngCounter)
        .Offset(0, 1).Value = varSplit(lngCounter + 2)
        lngOffset = lngOffset + 1
      End With
    Next lngCounter
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Extraxt multiple date and time values to multiple rows

    Holger, thank you, this does the job.
    Only thing left to do know is to copy the entire row. In your code only the values of A are splitted into A & B over needed rows, but the data from all other columns must be copied as well. So if the first row has 10 columns (A to J), these columns must be pasted as well.

    Also, can it be applied to a range, e.g. K1:K ? Say in column K there are multiple rows with multiple values, how to extract those to needed rows with colomn K and L ?

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Extraxt multiple date and time values to multiple rows

    Hi, WilliamV,

    if the range is filled and I have to insert I prefer to work from the bottom up. For this example I suggested that the last line of data in K should be the start for working the way up and splitting the contents.

    Sub WilliamV_RangeAndInsert()
    
    Dim varSplit
    Dim lngCounter As Long
    Dim lngOffset As Long
    Dim blnNewColumn As Boolean
    Dim lngRC As Long
    
    Const clngSTEP As Long = 3
    Const cstrCOL As String = "K"
    
    For lngRC = Cells(Rows.Count, cstrCOL).End(xlUp).Row To 1 Step -1
      If blnNewColumn = False Then
        Cells(1, cstrCOL).Offset(0, 1).EntireColumn.Insert
        blnNewColumn = True
      End If
      With Cells(lngRC, cstrCOL)
        lngOffset = 0
        varSplit = Split(.Value, " ")
        
        .Offset(1, 0).Resize(WorksheetFunction.RoundDown(UBound(varSplit) / clngSTEP, 0), 1).EntireRow.Insert
        For lngCounter = LBound(varSplit) To UBound(varSplit) Step clngSTEP
          With .Offset(lngOffset, 0)
            .Value = varSplit(lngCounter)
            .Offset(0, 1).Value = varSplit(lngCounter + 2)
            lngOffset = lngOffset + 1
          End With
        Next lngCounter
      End With
    Next lngRC
    
    With Cells(1, cstrCOL).CurrentRegion
      .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
    End With
    
    End Sub
    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Extract multiple date and time values to multiple rows

    Holger, ok, thank you.

    I would like to apply this code to included file Uitrapport.xls. You will find a file Uitrapport_filter.xls as well. This last one imports (through VBA) data from the first one. When openening it will ask to select a source file, you need to select the file Uirapport.xls. I have used VBA and formula's to obtain needed results. In column N you can find the date- and timedata. At this moment, i only use a formula to extract the time data in format from HH:MM to HH:MM, and i extend this formula manually to the available range.
    I would like to apply your above code to extract dates (if multiple present) and concerning times (per date, from and till). Multiople dates imply multiple rows, as in your examples above.

    Can you help me out with this? Thank you.
    Last edited by WilliamV; 10-18-2012 at 03:03 AM. Reason: correction

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Extract multiple date and time values to multiple rows

    Hi, WilliamV,

    according to the workbook in Column N the header is Lange beschrijving, and there are no date and time sequences as far as the Autofillter says. Can you help me out with that, please?

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Extract multiple date and time values to multiple rows

    Holger,

    Strange.
    The Header with date and time sequences is named Tijdsinfo samenvatting. When i open the workbook UitRapport.xls this is situated in column N. When you open the workbook UitRapport_filter the data is placed into the G-column.

    Hope this clarifies things... Thank you again.
    Grtz, William.

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Extract multiple date and time values to multiple rows

    Hi, William,

    okay, I got that one. But of I look at the first line another question occurs to me.

    Contents in G2 is
    van 05/09/12 tot 12/06/13   woe van 18:40 tot 21:35 (zo, ma, di, do, vrij, za gesloten)
    No problem for the time but a small problem concerning about the dates. To me this would mean fill up a range of dates from 05/09/12 to 12/06/13 where the weekdays meet the weekdays indicated inside the parentheses (Sun, Mon, Tue, Thur, Fri - correct me if I´m wrong with figuring these out). So shall just be these or should all dates be added?

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Extract multiple date and time values to multiple rows

    Well Holger, good you mention this, i've overseen it.

    To situate: the workbook is an extract of an event database. We need to 'model' this to our needs for generating an event booklet every month.

    The example in G2 is a reoccuring event: from 05/09/12 till 12/06/13 every wednesday from 18:40 till 21:35. The other days (yes, you did interpret it well: Sun, Mon, Tue, Thur, Fri) just stand for 'event closed (in dutch = gesloten, in german = geschlossen )' or 'event not taking place' so these have no importance and may be ignored.
    So, to answer your question: above example should be converted to all wednesdays between the mentioned date range. This maybe can be obtained by a condition: day (e.g. woensdag) from (=van) ... till (=tot) ... ?
    Note: If no time is given (e.g. G27) only the date range counts and all of the rest may be ignored.

    I hope this clarifies ... Thank you again for the efforts taken.
    Grtz, William.

  10. #10
    Registered User
    Join Date
    01-27-2012
    Location
    Blankenberge, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Extract multiple date and time values to multiple rows

    Holger,

    Your macro works like a charm (for the ActiveCell).
    Only: the time data is extracted to existing columns (the next 2 columns in line) and thus overwrite existing data.

    Important! Cells do not must be splitted if:
    • no specific days are specified (e.g. van 27/10/2012 tot 4/11/2012)
    • text Altijd open is mentioned
    • text open is present (e.g. maandag open, dinsdag open, ...) and the time span is not present
    An event example for higher mentioned items (to better understand) could be an exhibition taking place from e.g. 27/10/2012 till 4/11/2012. I hope this doesn't complicate things too much ...

    Regarding the single time span: you are right, this does counts for each and any date.

    Next step is to apply this macro to a range (e.g. N1:N) and to automate like in my earlier example.

    Thank you in advance (again!) for this superb and very time-consuming help!

    Grtz, William.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Extract multiple date and time values to multiple rows

    Hi, WilliamV,

    back again

    About the inserting of two columns for not overwríte data: can be done by something like this:

        If Not blnInserted Then
          With Cells(1, cstrCOL)
            .Offset(0, 1).Resize(1, 2).EntireColumn.Insert
            .Offset(0, 1).Resize(1, 2).ColumnWidth = 8
          End With
          blnInserted = True
        End If
    I started putting the code to run in a loop from the bottom up - and got stuck on the very first line of data with an error. I doubt it is a very good idea to code every possible combination into the code.

    As I don´t know how the data is coming to you it might be a better way to start and look at the contents of the cells and change them in a way to get a more consistent data with less exceptions to code for (that would make the code easier to maintain instead of writing new parts for any possible mistake that could appear). Like I said maybe there´s a way to straighten up the data - depending on how the data is delivered (easiest thing would be if you would supply a template to be filled where you could influence the data to yous/my needs).

    Ciao,
    Holger

+ 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