Results 1 to 10 of 112

Thread: Notes tests, string, manipulation of text files and string manipulations

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Macro for this post
    https://eileenslounge.com/viewtopic....277957#p277957

    Code:
    Sub TextFileToTabular()    '      https://eileenslounge.com/viewtopic.php?p=277881#p277881
    Rem 1 Worksheets info, (any worksheet will do to test paste out to)
    Dim Ws1 As Worksheet: Set Ws1 = ThisWorkbook.Worksheets.Item("TextToTabular")
    Rem 2 Text file info
    ' 2a) get the text file as a long single string
    Dim FileNum As Long: Let FileNum = FreeFile(1)                                  ' https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/freefile-function
    Dim PathAndFileName As String, TotalFile As String
     Let PathAndFileName = ThisWorkbook.Path & Application.PathSeparator & "tt.txt"   '                                                               CHANGE TO SUIT                                                                                                         From vixer zyxw1234  : http://www.eileenslounge.com/viewtopic.php?f=30&t=34629     DF.txt https://app.box.com/s/gw941dh9v8sqhvzin3lo9rfc67fjsbic
    Open PathAndFileName For Binary As #FileNum 'Open Route to data. Binary is a fundemental type data input...
    TotalFile = Space(LOF(FileNum)) '....and wot recives it has to be a string of exactly the right length
    Get #FileNum, , TotalFile                                               'Debug.Print TotalFile
    Close #FileNum
    ' Let TotalFile = Replace(TotalFile, """", "", 1, -1, vbBinaryCompare):   Debug.Print TotalFile  '   removed enclosing quotes in  rabsofty's  text file
    ' Call WtchaGot_Unic_NotMuchIfYaChoppedItOff(TotalFile)
    '2a)(ii)  Some simple tidying up of complete string
     Let TotalFile = Replace(TotalFile, ": http", "http", 1, -1, vbBinaryCompare) ' there are some strange   : http  which in combination with the next line will/ would  introduce an error
     Let TotalFile = Replace(TotalFile, "http", " http", 1, -1, vbBinaryCompare)  ' this ensures at least two spaces before any  link
    ' 2b) Split into wholes line _ splitting the text file into rows by splitting by Line Seperator
    Dim arrRws() As String: Let arrRws() = Split(TotalFile, vbCr & vbLf, -1, vbBinaryCompare)
    Dim RwCnt As Long: Let RwCnt = UBound(arrRws()) + 1    '  +1 is nedeed as the  Split Function  returns indicies 0 1 2 3 4 5   etc...
    ' 2c) We are looping all row data: Some is single element(column) entries , some is multi element(column) data.  We want to try to fill up a 6 element(column) array
    Dim arrRw() As String: ReDim arrRw(1 To 6) ' we know our columns are 6 entries
    ' 2d) we make an array for all the final rows
    Dim arrHarray() As Variant: Dim HarryCnt As Long: Let HarryCnt = 1
    Dim Cnt As Long
        Do While Not Cnt = RwCnt - 1 ' We are looping all row data: Some is single element(column) entries , some is multi element(column) data
        'For Cnt = 1 To RwCnt
        ' 2c) _(A)
            Do While (arrRw(1) = "" Or arrRw(2) = "" Or arrRw(3) = "" Or arrRw(4) = "" Or arrRw(5) = "" Or arrRw(6) = "") ' we try to fill all 6 element(column) data in the array, but we try to deal with some missing
                If arrRws(Cnt) <> "" Then
                 Let Cnt = Cnt + 1
                  ' _(B)
                    If InStr(1, Trim(arrRws(Cnt - 1)), "  ", vbBinaryCompare) = 0 Then ' this is the case of a rouge line
                      ' _(C)
                        If InStr(1, Trim(arrRws(Cnt - 1)), "@", vbBinaryCompare) <> 0 Or InStr(1, Trim(arrRws(Cnt - 1)), "https://www.instagram.com", vbBinaryCompare) <> 0 Or InStr(1, Trim(arrRws(Cnt - 1)), "https://www.facebook.com", vbBinaryCompare) <> 0 Or InStr(1, Trim(arrRws(Cnt - 1)), "http://reddit.com", vbBinaryCompare) <> 0 Then
                            If arrRw(5) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                         Let arrRw(5) = Trim(arrRws(Cnt - 1))
                       ' _(D)
                        ElseIf InStr(1, Trim(arrRws(Cnt - 1)), "https://open.spotify.com/", vbBinaryCompare) <> 0 Then
                            If arrRw(6) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                         Let arrRw(6) = Trim(arrRws(Cnt - 1))
                        ElseIf IsNumeric(Trim(arrRws(Cnt - 1))) Then
                            If arrRw(4) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                         Let arrRw(4) = Trim(arrRws(Cnt - 1))
                        ElseIf InStr(1, Trim(arrRws(Cnt - 1)), ",", vbBinaryCompare) <> 0 Then
                        Dim ExtrGenitals As String
                            If ExtrGenitals <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                         Let ExtrGenitals = Trim(arrRws(Cnt - 1))
                        
                        Else
                        End If
                        
                    Else ' we have a line with multiple data, assuming that such data is seperated by at least 2 spaces  "  "
                    Dim DataSRw As String: Let DataSRw = arrRws(Cnt - 1)
                     Let DataSRw = LTrim(DataSRw) & "  " ' take off any preceding spaces and add a few spaces so that next  Loop  works for the last element
                    Dim posTwoSpcs As Long
                        Do While DataSRw <> "" '  looping to get all data from a dataS row ----
                        Dim ClmCnt As Long: Let ClmCnt = ClmCnt + 1
                         Let posTwoSpcs = InStr(1, DataSRw, "  ", vbBinaryCompare)
                            If ClmCnt > 3 Then ' after the third entry things may be not incorrect order
                            Dim UnOrdedIndataSRw As String
                             Let UnOrdedIndataSRw = Left(DataSRw, (posTwoSpcs - 1))
                                If InStr(1, UnOrdedIndataSRw, "@", vbBinaryCompare) <> 0 Or InStr(1, UnOrdedIndataSRw, "https://www.instagram.com", vbBinaryCompare) <> 0 Or InStr(1, UnOrdedIndataSRw, "https://www.facebook.com", vbBinaryCompare) <> 0 Or InStr(1, UnOrdedIndataSRw, "http://reddit.com", vbBinaryCompare) <> 0 Then
                                    If arrRw(5) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                                 Let arrRw(5) = UnOrdedIndataSRw
                                ElseIf InStr(1, UnOrdedIndataSRw, "https://open.spotify.com/", vbBinaryCompare) <> 0 Then
                                    If arrRw(6) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                                 Let arrRw(6) = UnOrdedIndataSRw
                                Else
                                    If arrRw(ClmCnt) <> "" Then Let Cnt = Cnt - 1: GoTo Missing
                                 Let arrRw(ClmCnt) = Left(DataSRw, (posTwoSpcs - 1))
                                End If
                            Else
                             Let arrRw(ClmCnt) = Left(DataSRw, (posTwoSpcs - 1))
                                If ClmCnt = 3 And InStr(1, arrRw(3), " ", vbBinaryCompare) <> 0 And (InStr(1, arrRw(3), "@", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "https://www.instagram.com", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "https://www.facebook.com", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "http://reddit.com", vbBinaryCompare) <> 0) Then ' We may have a problem that after the Genres data has a link added with just one space that should be the fifth column (Best Way To Contact)
                                Dim SptGenitral() As String: Let SptGenitral() = Split(arrRw(3), " ", -1, vbBinaryCompare)
                                 Let arrRw(5) = SptGenitral(UBound(SptGenitral))
                                 Let arrRw(3) = Replace(arrRw(3), " " & arrRw(5), "", 1, -1, vbBinaryCompare)
                                 Let posTwoSpcs = InStr(1, DataSRw, "  ", vbBinaryCompare)
                                ' _(B)(i) The macro will deal with some cases of  Curator   and  Genres   only being separated by one space  This next bit may sort out if the Curator is in two words and is only seperated from the Playlist Name by 1 space
                                ElseIf InStr(1, arrRw(3), "@", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "https://www.instagram.com", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "https://www.facebook.com", vbBinaryCompare) <> 0 Or InStr(1, arrRw(3), "http://reddit.com", vbBinaryCompare) <> 0 Then
                                 Let arrRw(5) = arrRw(3)
                                 Let arrRw(3) = arrRw(2)
                                Dim Spt1a() As String: Let Spt1a() = Split(arrRw(1), " ", -1, vbBinaryCompare)
                                 Let arrRw(2) = Spt1a(UBound(Spt1a()) - 1) & " " & Spt1a(UBound(Spt1a()))
                                ElseIf InStr(1, arrRw(3), "https://open.spotify.com/", vbBinaryCompare) <> 0 Then
                                 Let arrRw(6) = arrRw(3)
                                 Let arrRw(3) = arrRw(2)
                                 Let arrRw(1) = Replace(arrRw(1), arrRw(2), "", 1, 1, vbBinaryCompare)
                                Dim Spt1b() As String: Let Spt1b() = Split(arrRw(1), " ", -1, vbBinaryCompare)
                                 Let arrRw(2) = Spt1b(UBound(Spt1b()) - 1) & " " & Spt1b(UBound(Spt1b()))
                                 Let arrRw(1) = Replace(arrRw(1), arrRw(2), "", 1, 1, vbBinaryCompare)
                                End If
                            End If
                         Let DataSRw = Mid(DataSRw, posTwoSpcs)
                         Let DataSRw = LTrim(DataSRw)
    
                        Loop                   '  looping to get all data from a dataS row ----
                    End If
                Else ' case empty row
                 Let Cnt = Cnt + 1   '   increase to next data row from the text file
                    If Cnt = RwCnt Then GoTo Bed
                End If
            Loop ' While (arrRw(1) = "" Or arrRw(2) = "" Or arrRw(3) = "" Or arrRw(4) = "" Or arrRw(5) = "" Or arrRw(6) = "")
    Missing: ' _(A)(i)   we come here if we tried to fill an already filled element, which indicates we had something missing
             Let arrRw(3) = arrRw(3) & ExtrGenitals  '  modify Genres string to include any appearing in a rogue line
            ' 2d)(ii) At this point its time to put the current completed row data into the jagged array to use late in Index
    Rem 3  An array is built up by using that interesting "Index on a unjagged jagged 1Ds arrays  technique" that we first noticed here: https://eileenslounge.com/viewtopic.php?p=266691#p266691 https://eileenslounge.com/viewtopic.php?p=266727#p266727 https://www.ozgrid.com/forum/index.php?thread/1227920-slicing-a-2d-array/&postID=1239241#post1239241
            ReDim Preserve arrHarray(1 To HarryCnt)
             Let arrHarray(HarryCnt) = arrRw()
             Let HarryCnt = HarryCnt + 1
            
            ReDim arrRw(1 To 6) ' this resets (empties) the row array
             Let ClmCnt = 0
             Let ExtrGenitals = ""
        Loop ' While Not Cnt = RwCnt-1
    Bed: ' This section will deal with a problem of the last row in harry being missed if it is missing some data
    ReDim Preserve arrHarray(1 To HarryCnt)
     Let arrHarray(HarryCnt) = arrRw()
    Rem 4  Finally the array is pasted to  worksheet                             ' use of that interesting "Index on a unjagged jagged 1Ds arrays  technique" that we first noticed here: https://eileenslounge.com/viewtopic.php?p=266691#p266691 https://eileenslounge.com/viewtopic.php?p=266727#p266727 https://www.ozgrid.com/forum/index.php?thread/1227920-slicing-a-2d-array/&postID=1239241#post1239241
    Dim RngOut As Range: Set RngOut = Worksheets("TextToTabular").Range("A1:F" & UBound(arrHarray()) & "")
     Let RngOut.Value = Application.Index(arrHarray(), Evaluate("=row(1:" & UBound(arrHarray()) & ")"), Array(1, 2, 3, 4, 5, 6))
     Worksheets("TextToTabular").Columns("A:F").AutoFit
    '' 4b Option to remove the  little   ...    when i click on any cell that has output there is an option numbers stored as text, convert to numbers,help on this error,ignore error,edit in formula bar,error checking options(these are the options coming)…
    ' Let RngOut.Value = Evaluate("=IF(" & RngOut.Address & "="""","""",IF(ISNUMBER(1*" & RngOut.Address & "),1*" & RngOut.Address & "," & RngOut.Address & "))")   '    http://www.eileenslounge.com/viewtopic.php?p=272704#p272704
    End Sub
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    In support of this Thread
    https://www.ozgrid.com/forum/index.p...23#post1241623

    Before
    _____ Workbook: Sample excel file.xls ( Using Excel 2007 32 bit )
    Row\Col A B P Q R S T U V W X Y
    2 15 16 17 18 19 20 21 27
    3 SLA Su M T W Th F S Su M T
    4 RunbookBCompletionTime 6:00AM
    5 ActiveProducts N/A
    6 ActiveSKUs N/A
    7 Pending N/A
    8 Completed N/A
    9 CompletedOrderCount N/A
    10 PendingOrderCount N/A
    11
    12
    13 Active
    14
    Worksheet: Sheet1
    http://i.imgur.com/jrBBlXT.jpg

    text file: Sample.txt
    Code:
    ---------------------------------------------------- 
    This is a  report for last week 
    ---------------------------------------------------- 
     
    Date,RunbookBCompletionTime 
    20201116,05:44 AM
    20201117,05:47 AM
    20201118,05:39 AM
    20201119,06:10 AM
    20201120,05:49 AM
    20201121,07:13 AM
    20201122,06:01 AM
     
    Date,ActiveProducts 
    20201116,24
    20201117,244
    20201118,245 
    20201119,24 
    20201120,249 
    20201121,250 
    20201122,250 
     
    Date,ActiveSKUs  
    20201116,137
    20201117,13
    20201118,13
    20201119,1368
    20201120,13
    20201121,1381
    20201122,13
     
    Date,CompletedOrderCount 
    20201116,24
    20201117,24
    20201118,3
    20201119,24
    20201120,63
    20201121,69
    20201122,8
    20201123,9
     
    Date,PendingOrderCount 
    20201116,18
    20201117,5405
    20201118,6114
    20201119,6
    20201120,6482
    20201121,74
    20201122,128
    20201123,4

    After runningSub LookInAndImportTextStringSample()
    https://i.imgur.com/0m881xs.jpg
    _____ Workbook: Sample excel file.xls ( Using Excel 2007 32 bit )
    Row\Col P Q R S T U V W X Y
    2 15 16 17 18 19 20 21 27
    3 Su M T W Th F S Su M T
    4 05:44 05:47 05:39 06:10 05:49 07:13 06:01
    5 24 244 245 24 249 250 250
    6 137 13 13 1368 13 1381 13
    7
    8
    9 24 24 3 24 63 69 8 9
    10 18 5405 6114 6 6482 74 128 4
    11
    12
    Worksheet: Sheet1






    Share ‘Sample excel file.xls’ : https://app.box.com/s/hw4uxwjlm8t8zty17kc07xihq0bfhifs
    Share ‘Sample excel file.xlsm’ : https://app.box.com/s/ccmk5sgazueejb4dc0eqw6yex0zhjar2


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugz-pow-E8FDG8gFZ4l4AaABAg.9f8Bng22e5d9f8hoJGZY-5
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxev2gQt7BKZ0WYMfh4AaABAg.9f6hAjkC0ct9f8jleOui-u
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxg9iT7MPWGBWruIzR4AaABAg
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 07-12-2023 at 05:21 PM.

Similar Threads

  1. Replies: 116
    Last Post: 02-23-2025, 12:13 AM
  2. Replies: 4
    Last Post: 10-02-2022, 09:18 PM
  3. Replies: 4
    Last Post: 01-30-2022, 04:05 PM
  4. Replies: 0
    Last Post: 07-08-2020, 04:29 PM
  5. string manipulation
    By kylefoley76 in forum Excel Help
    Replies: 5
    Last Post: 02-20-2014, 12:10 AM

Posting Permissions

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