+ Reply to Thread
Results 1 to 29 of 29

String to array using split

  1. #1
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    String to array using split

    Hi all,

    I have many csv files which I searching. If some data fit my conditions I put this lines into excel sheet.
    Example content of file

    10-02-2022 02:09:26;lider;0123456789;1295.606;6.646416;40.26785;6.856215;219.5962;7.735233;7.840924;5.326111;5.34971;3.108031
    10-02-2022 02:09:36;lider;0123456789;180.3252;6.055035;0.0;5.745333;19.70758;7.860642;7.872498;5.408381;5.459498;0.0
    10-02-2022 02:09:46;lider;0123456789;1068.001;6.895689;24.23427;7.128262;220.8212;7.932885;7.978186;5.763059;5.788158;2.269124
    10-02-2022 02:09:56;lider;0123456789;759.2748;6.048777;12.00476;6.061655;216.1708;7.951935;7.983704;4.823963;4.857661;1.581083
    10-02-2022 02:10:06;lider;0123456789;1004.858;7.143832;27.27258;7.321005;220.8766;7.987683;8.026818;5.918257;5.943105;2.714073

    Files I open this function

    Please Login or Register  to view this content.
    content of file I assign to array this function

    Please Login or Register  to view this content.
    to put data into range I use this code

    Please Login or Register  to view this content.
    The problem is with some date format. For example date 10-02-2022 02:09:26 is shown in excel cell as 02-10-2022 02:09:26

    How to fix this problem?

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,361

    Re: String to array using split

    Maybe you could try the QueryTable method:
    Please Login or Register  to view this content.
    If things don't change they stay the same

  3. #3
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Quote Originally Posted by CheeseSandwich View Post
    Maybe you could try the QueryTable method:
    Please Login or Register  to view this content.
    Thanks, I tried but it's take much time when processing thousands of files Current method is quicker

  4. #4
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,361

    Re: String to array using split

    I see, well you may have to slow your method somewhat as you will need to deal with the dates differently as they are imported.

    This would potentially be done with a function called CDate, but to use it you would need to know where the date was in the array before output.

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: String to array using split

    Try this
    Use additional one more Replace function Replace "-" by "/".
    Last edited by kvsrinivasamurthy; 04-25-2022 at 08:03 AM.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: String to array using split

    Quote Originally Posted by mtcas View Post
    Hi all,
    The problem is with some date format. For example date 10-02-2022 02:09:26 is shown in excel cell as 02-10-2022 02:09:26
    Just loop through tabi, and swap the day/month or follow your regional settings if the line is like "##-##-#### ##:##:##".

  7. #7
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Yes, this is the way I will take, but want to avoid additional loop if possible

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: String to array using split

    I don't understand why people doesn't like loops...
    Loop within array(memory) is almost instant.

  9. #9
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    I like loops but sometimes I'm looking for very efficient solutions. I'll measure time maybe indeed difference is imperceptible

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: String to array using split

    Efficient...
    Less lines? speed?
    Last edited by jindon; 04-26-2022 at 06:03 AM.

  11. #11
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Speed. Of course if less lines is also good.

  12. #12
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: String to array using split

    Have you seen post #5.

  13. #13
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Quote Originally Posted by kvsrinivasamurthy View Post
    Have you seen post #5.
    Sorry, I missed, but tested and unfortunately result is the same as previous - nothing changed

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: String to array using split


    Quote Originally Posted by mtcas View Post
    How to fix this problem?
    Hi,

    attach at least a source csv text file and accordingly the exact expected workbook result …

  15. #15
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Files attached
    Attached Files Attached Files

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: String to array using split

    deketed (n w)
    Last edited by jindon; 04-26-2022 at 06:06 AM.

  17. #17
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    This is my code.
    I think all functions are unnecessary to explain, the problem is only with date formatting.


    Please Login or Register  to view this content.

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    According to your post #15 attachment a VBA demonstration for a starting point to paste only to the Sheet2 (all) worksheet module :

    PHP Code: 
    Sub Demo1()
      Const 
    #10/2/2022 10:17:00 AM#, U = #10/2/2022 10:27:00 AM#
        
    Dim C$, F%, V
            C 
    ThisWorkbook.Path "\20220210_103831.csv":  If Dir(C) = "" Then Beep: Exit Sub
            F 
    FreeFile
            UsedRange
    .Clear
            Open C 
    For Input As #F
            
    Split(Input(LOF(F), #F), vbCrLf)
            
    Close #F
            
    Application.ScreenUpdating False
        With 
    [A1].Resize(UBound(V) + 1)
           .
    Value2 Application.Transpose(V)
           .
    TextToColumns 1xlTextQualifierNone, , , True, , , , , , "."
        
    End With
        With UsedRange
    .Columns
           
    .Item(1).AutoFilter 1"<" Format(L, [A2].NumberFormat), 2">=" Format(U, [A2].NumberFormat)
            If 
    Application.Subtotal(103, .Item(1)) > 1 Then .Offset(1).EntireRow.Delete
           
    .AutoFilter
    '       .AutoFit
        End With
            Application.ScreenUpdating = True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  19. #19
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,361

    Re: String to array using split

    Maybe:
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: Try this !

    Quote Originally Posted by Marc L View Post

    According to your post #15 attachment a VBA demonstration for a starting point to paste only to the Sheet2 (all) worksheet module :

    PHP Code: 
    Sub Demo1()
      Const 
    #10/2/2022 10:17:00 AM#, U = #10/2/2022 10:27:00 AM#
        
    Dim C$, F%, V
            C 
    ThisWorkbook.Path "\20220210_103831.csv":  If Dir(C) = "" Then Beep: Exit Sub
            F 
    FreeFile
            UsedRange
    .Clear
            Open C 
    For Input As #F
            
    Split(Input(LOF(F), #F), vbCrLf)
            
    Close #F
            
    Application.ScreenUpdating False
        With 
    [A1].Resize(UBound(V) + 1)
           .
    Value2 Application.Transpose(V)
           .
    TextToColumns 1xlTextQualifierNone, , , True, , , , , , "."
        
    End With
        With UsedRange
    .Columns
           
    .Item(1).AutoFilter 1"<" Format(L, [A2].NumberFormat), 2">=" Format(U, [A2].NumberFormat)
            If 
    Application.Subtotal(103, .Item(1)) > 1 Then .Offset(1).EntireRow.Delete
           
    .AutoFilter
    '       .AutoFit
        End With
            Application.ScreenUpdating = True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Thanks, but in may sheet still shows 02-10 instead of 10-02

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: String to array using split


    So your bad 'cause you misread my post #14 as my demonstration reproduces exactly your attachment expected result workbook !

  22. #22
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Quote Originally Posted by Marc L View Post

    So your bad 'cause you misread my post #14 as my demonstration reproduces exactly your attachment expected result workbook !
    Ok, maybe my english is not enough, but I copied your code into new worksheet and result is - date is incorrect(day swapped with month). What I do bad?

  23. #23
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Quote Originally Posted by CheeseSandwich View Post
    Maybe:
    Please Login or Register  to view this content.
    Thanks, probably additional loop will be the simplest solution.

  24. #24
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: String to array using split


    Quote Originally Posted by mtcas View Post
    What I do bad?
    Your post #15 attachment does not have the exact expected result workbook as requested in my post #14 …

  25. #25
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Quote Originally Posted by Marc L View Post

    Your post #15 attachment does not have the exact expected result workbook as requested in my post #14 …
    Ok.
    Do you like wasting time? Just read post #1 and you should know that result I want to reach is "For example date 10-02-2022 02:09:26 is shown in excel cell as 02-10-2022 02:09:26
    How to fix this problem?"
    Yes, my bad, in the file "result" is wrong date format

  26. #26
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    I tried also set cell formatting as "TEXT" but then debugger stops on line when tried fill the range. So excel is forcing change 10.02 for 02.10

    Please Login or Register  to view this content.

  27. #27
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this …


    Next time you will create a thread you should be more accurate in your initial post
    as is it so difficult to indicate the text file date format like to attach the exact expected result ?‼

    As a reminder VBA CDate function well works only if the Windows Regional Settings are the same than the date format used within the csv file …

    So according to the post #15 bad attachment, according to Excel basics & VBA help,
    a VBA demonstration for starters to paste only to the Sheet2 (all) worksheet module :

    PHP Code: 
    Sub Demo2()
        
    Dim S$, As DateAs DateF%, VR&, As Date
            S 
    ThisWorkbook.Path "\20220210_103831.csv":  If Dir(S) = "" Then Beep: Exit Sub
            L 
    DateSerial(2022210) + TimeValue("10:17")
            
    DateSerial(2022210) + TimeValue("10:27")
            
    FreeFile
            UsedRange
    .Clear
            Open S 
    For Input As #F
            
    Split(Input(LOF(F), #F), vbCrLf)
            
    Close #F
        
    For 1 To UBound(V)
            If 
    V(R) > "" Then
                D 
    DateSerial(Mid(V(R), 74), Mid(V(R), 42), Left(V(R), 2)) + TimeValue(Mid(V(R), 138))
                If 
    Or >= U Then V(R) = False Else Mid(V(R), 15) = Format$(D"mm-dd")
            Else
                
    V(R) = False
            End 
    If
        
    Next
            V 
    Filter(VFalseFalse)
        
    With [A1].Resize(UBound(V) + 1)
           .
    Value2 Application.Transpose(V)
           .
    TextToColumns 1xlTextQualifierNone, , , True, , , , , , "."
        
    End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  28. #28
    Forum Contributor
    Join Date
    03-14-2017
    Location
    Poland
    MS-Off Ver
    2016
    Posts
    107

    Re: String to array using split

    Ok, problem solved. Im not pasted this code which is executed after all

    Please Login or Register  to view this content.
    this replaced me the dates.

    Thank you all for help

  29. #29
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: String to array using split

    you just store Range A value as text or is it further used for calculation purpose.

+ 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. Split string in array not in cell range vba excel macro
    By Yajson in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 12-31-2017, 12:07 AM
  2. [SOLVED] Split String at every "/" then put split string results into a multiline textbox.
    By Andrew Andromeda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2015, 01:49 AM
  3. VBA Find Partial String in String Array and Output the Found String Array Value
    By scherich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2014, 11:20 AM
  4. [SOLVED] Return matching string in array if cell contains string contained in the array
    By AaronsZ in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-03-2013, 01:55 PM
  5. Can excels Split() function split a string up at multiple spots?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2011, 02:36 PM
  6. Split string by line break into array
    By kennychung in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2009, 12:18 PM
  7. how to split a string and return array
    By xiang in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-14-2005, 07:55 AM

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