Page 16 of 61 FirstFirst ... 6141516171826 ... LastLast
Results 151 to 160 of 604

Thread: Appendix-Thread-Evaluate-Range-(-Codes-for-other-Threads-HTML-Tables-etc-)

  1. #151
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Sub BubblesIndexIdeaWay As Sub Bubble , just replacing name in three places

    Code:
    '
    Sub Bubbles(ByVal CpyNo As Long, ByRef arsRef() As Variant, ByVal strRws As String, ByVal strKeys As String)
    Dim CopyNo As Long: Let CopyNo = CpyNo ' On every recurssion run this will be increased by the Rec Call. This will be a local variable indicating the level down in recursion - I increase it by 1 at every Rec Call, that is to say each Rec Call gets given CopyNo+1  -  during a long tunnel down, the number at this pint will keep increasing to reflect how far down we are.  This ensures we then have the correct value place at the start of any newly starting copy of the recursion routine, since the first copy of the recursion routine pauses and starts the second copy of the recursion routine, the second copy of the recursion routine pauses and starts the third copy of the recursion routine ….. etc…
                                                                                 If CopyNo = 1 Then Debug.Print "First procedure Call"
    Rem -1 from the supplied  arguments, get all data needed in current bubble sort
    Dim Keys() As String: Let Keys() = Split(Replace(Trim(strKeys), "  ", " ", 1, -1, vbBinaryCompare), " ", -1, vbBinaryCompare) ''      The extra replace allows for me seperating with one or two spaces  - the following would do if I only used one space always    Split(Trim(strKeys), " ", -1, vbBinaryCompare) ' this will be an array twice as big as the number of keys that we have ...... ' Column for this level sort , and Ascending or Descending - both determined from the supplied forth arguments and the column/ copy number
                                                                                 If (2 * CopyNo) > UBound(Keys()) + 1 Then Debug.Print "You need more than " & (UBound(Keys()) + 1) / 2 & " keys to complete sort": Exit Sub ' case we have less keys then we need to sort, are array has twice as many elements as we have supplied keys since we have a key and whether it needs to get bigger or smaller , and array is from  0   so must add 1 to ubound then half it get the key number we gave.  We come here if the last column we gave as a Key had duplicates in it
                                                                                                                                         'Dim GlLl As Long: If Keys((CopyNo * 2) - 1) = "Desc" Then Let GlLl = 1 ' (CopyNo * 2) - 1)  gives as we go down levels   1  3  5  7 etc  We're seeing if we had  Desc  for this column
    Dim Clm As Long: Let Clm = CLng(Keys((CopyNo * 2) - 2)) '              ' (CopyNo * 2) - 2)  gives as we go down levels   0  2  4  6 etc  We  are  picking  out  the  supplied   column to sort by for each level
    Rem 1 Bubble sort
    Dim rOuter As Long ' ========"Left Hand"=====================Outer Loop=====================================
        ' For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1 ' THIS WOULD ONLY WORK FOR Copy No 1      For first row indicie to last but one row indicie - I could do this for copy 1
        For rOuter = Left(Trim(strRws), InStr(1, Trim(strRws), " ", vbBinaryCompare) - 1) To Right(Trim(strRws), Len(Trim(strRws)) - InStrRev(Trim(strRws), " ", -1, vbBinaryCompare)) - 1 '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
        'For rOuter = 1 To 5 ' For first run
        Dim rInner As Long ' -------Inner Loop-------------"Right Hand"--------------------------
            For rInner = rOuter + 1 To Rs(UBound(Rs(), 1), 1) ' from just above left hand through all the rest
                If CDbl(arsRef(rOuter, Clm)) > CDbl(arsRef(rInner, Clm)) Then
                Dim Temp As Variant ' I want to Swap those 2 above - I cant easilly in any coding change two values simulataneosly. So one of them Element values will put in this temporary place. This Element Values will then be given the other. Finally the other Element will be given this temporary value
                Dim Clms As Long '-------| with the condition met  a loop is done for all columns in the array in which those two values used in the comparison are replaced at each column
                    For Clms = 1 To UBound(arsRef(), 2)
                     Let Temp = arsRef(rOuter, Clms): Let arsRef(rOuter, Clms) = arsRef(rInner, Clms): Let arsRef(rInner, Clms) = Temp
                    Next Clms '----------| for each column in the array at the two rows rOuter and rInner
                Else
                End If
            Next rInner ' -----------------------------------------------------------------------
        Next rOuter ' ==========================================================================================
    
    Rem 3 Preparation for possible recursion Call
    ' Catpains Blog
     Debug.Print " Running Copy No. " & CopyNo & " of routine." & vbCr & vbLf & "  Sorted rows " & strRws & " based on values in column " & Clm & vbCr & vbLf & "   Checking now for Dups in that last sorted list" & vbCr & vbLf
    Dim tempStr As String: Let tempStr = strRws ' Need this bodge because I set strRws="" below
     Let strRws = ""
        'For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1  ' Only valis for first Copy No 1
        For rOuter = Left(Trim(tempStr), InStr(1, Trim(tempStr), " ", vbBinaryCompare) - 1) To Right(Trim(tempStr), Len(Trim(tempStr)) - InStrRev(Trim(tempStr), " ", -1, vbBinaryCompare)) - 1                                   '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
            If strRws = "" Or InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then Let strRws = " " & rOuter & " " ' case starting again to get duplicates
            'If strRws = "" Or Trim(strRws) = rOuter - 1 Then Let strRws = " " & rOuter & " " ' alternative
            If Trim(UCase(CStr(arsRef(rOuter, Clm)))) = Trim(UCase(CStr(arsRef(rOuter + 1, Clm)))) Then ' case in duplicate rows
             Let strRws = strRws & rOuter + 1 & " " ' we building a list like   " 4 5 6 "  based on if the next is a duplicate value, which is determined by the last line
            Else '  without the last condition met, we might have the end of a group duplicate rows, in which case it would be time to organise a recursion run so ..... we check for this situation needing a recursion run
                If Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case we have at least 2 duplicates but have hit end of that list ( we have at least one space between indices, like " 3 4 " , " 6 7 8" etc..  ---- this , " 2 " , on the other hand, would would not have a space after trimming off the end spaces )
                 ' Now its time to organise a recursion run
                 Debug.Print "Found dups in last list column " & Clm & ",  " & strRws & " ,  so now main Rec Call " '     This is done for every duplicated
                 Call Bubbles(CopyNo + 1, arsRef(), strRws, strKeys) ' Rec Call 1  I need to sort the last duplicates
                 Let strRws = "" ' ready to try for another set of duplicates
                Else
                End If ' we did not have more than one indicie in strRws so usually that's it for this loop
            End If
            '+++*** this would be end of loop for most cases
            ' ...below section catches rows at the end that might need to be sorted. ......|
            If rOuter = UBound(arsRef(), 1) - 1 And Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case of duplicates in last row
             Debug.Print "Found dups in last rows of last list, so now Rec Call at end of loop (Dups at list end case)"  ' loop end rec call - only done for duplicates at end of list
             Call Bubbles(CopyNo + 1, arsRef(), strRws, strKeys)
            Else
            End If  '...   ................................................................|
        Next rOuter   '   **************************************************************************
     Debug.Print "Ending a copy, Copy level " & CopyNo & ""
    End Sub

  2. #152
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Sub BubblesIndexIdeaWay As Sub Bubble , just replacing name in three places

    This intermadiate routine is simply Sub Bubbles(), with the name changed to Sub BubblesIndexIdeaWay(), and the two recursion Calling code lines changed from
    Call Bubbles(CopyNo + 1, arsRef(), strRws, strKeys)
    to
    Call BubblesIndexIdeaWay(CopyNo + 1, arsRef(), strRws, strKeys)


    Code:
    '
    Sub BubblesIndexIdeaWay(ByVal CpyNo As Long, ByRef arsRef() As Variant, ByVal strRws As String, ByVal strKeys As String)
    Dim CopyNo As Long: Let CopyNo = CpyNo ' On every recurssion run this will be increased by the Rec Call. This will be a local variable indicating the level down in recursion - I increase it by 1 at every Rec Call, that is to say each Rec Call gets given CopyNo+1  -  during a long tunnel down, the number at this pint will keep increasing to reflect how far down we are.  This ensures we then have the correct value place at the start of any newly starting copy of the recursion routine, since the first copy of the recursion routine pauses and starts the second copy of the recursion routine, the second copy of the recursion routine pauses and starts the third copy of the recursion routine ….. etc…
                                                                                 If CopyNo = 1 Then Debug.Print "First procedure Call"
    Rem -1 from the supplied  arguments, get all data needed in current bubble sort
    Dim Keys() As String: Let Keys() = Split(Replace(Trim(strKeys), "  ", " ", 1, -1, vbBinaryCompare), " ", -1, vbBinaryCompare) ''      The extra replace allows for me seperating with one or two spaces  - the following would do if I only used one space always    Split(Trim(strKeys), " ", -1, vbBinaryCompare) ' this will be an array twice as big as the number of keys that we have ...... ' Column for this level sort , and Ascending or Descending - both determined from the supplied forth arguments and the column/ copy number
                                                                                 If (2 * CopyNo) > UBound(Keys()) + 1 Then Debug.Print "You need more than " & (UBound(Keys()) + 1) / 2 & " keys to complete sort": Exit Sub ' case we have less keys then we need to sort, are array has twice as many elements as we have supplied keys since we have a key and whether it needs to get bigger or smaller , and array is from  0   so must add 1 to ubound then half it get the key number we gave.  We come here if the last column we gave as a Key had duplicates in it
                                                                                                                                         'Dim GlLl As Long: If Keys((CopyNo * 2) - 1) = "Desc" Then Let GlLl = 1 ' (CopyNo * 2) - 1)  gives as we go down levels   1  3  5  7 etc  We're seeing if we had  Desc  for this column
    Dim Clm As Long: Let Clm = CLng(Keys((CopyNo * 2) - 2)) '              ' (CopyNo * 2) - 2)  gives as we go down levels   0  2  4  6 etc  We  are  picking  out  the  supplied   column to sort by for each level
    Rem 1 Bubble sort
    Dim rOuter As Long ' ========"Left Hand"=====================Outer Loop=====================================
        ' For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1 ' THIS WOULD ONLY WORK FOR Copy No 1      For first row indicie to last but one row indicie - I could do this for copy 1
        For rOuter = Left(Trim(strRws), InStr(1, Trim(strRws), " ", vbBinaryCompare) - 1) To Right(Trim(strRws), Len(Trim(strRws)) - InStrRev(Trim(strRws), " ", -1, vbBinaryCompare)) - 1 '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
        'For rOuter = 1 To 5 ' For first run
        Dim rInner As Long ' -------Inner Loop-------------"Right Hand"--------------------------
            For rInner = rOuter + 1 To Rs(UBound(Rs(), 1), 1) ' from just above left hand through all the rest
                If CDbl(arsRef(rOuter, Clm)) > CDbl(arsRef(rInner, Clm)) Then
                Dim Temp As Variant ' I want to Swap those 2 above - I cant easilly in any coding change two values simulataneosly. So one of them Element values will put in this temporary place. This Element Values will then be given the other. Finally the other Element will be given this temporary value
                Dim Clms As Long '-------| with the condition met  a loop is done for all columns in the array in which those two values used in the comparison are replaced at each column
                    For Clms = 1 To UBound(arsRef(), 2)
                     Let Temp = arsRef(rOuter, Clms): Let arsRef(rOuter, Clms) = arsRef(rInner, Clms): Let arsRef(rInner, Clms) = Temp
                    Next Clms '----------| for each column in the array at the two rows rOuter and rInner
                Else
                End If
            Next rInner ' -----------------------------------------------------------------------
        Next rOuter ' ==========================================================================================
    
    Rem 3 Preparation for possible recursion Call
    ' Catpains Blog
     Debug.Print " Running Copy No. " & CopyNo & " of routine." & vbCr & vbLf & "  Sorted rows " & strRws & " based on values in column " & Clm & vbCr & vbLf & "   Checking now for Dups in that last sorted list" & vbCr & vbLf
    Dim tempStr As String: Let tempStr = strRws ' Need this bodge because I set strRws="" below
     Let strRws = ""
        'For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1  ' Only valis for first Copy No 1
        For rOuter = Left(Trim(tempStr), InStr(1, Trim(tempStr), " ", vbBinaryCompare) - 1) To Right(Trim(tempStr), Len(Trim(tempStr)) - InStrRev(Trim(tempStr), " ", -1, vbBinaryCompare)) - 1                                   '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
            If strRws = "" Or InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then Let strRws = " " & rOuter & " " ' case starting again to get duplicates
            'If strRws = "" Or Trim(strRws) = rOuter - 1 Then Let strRws = " " & rOuter & " " ' alternative
            If Trim(UCase(CStr(arsRef(rOuter, Clm)))) = Trim(UCase(CStr(arsRef(rOuter + 1, Clm)))) Then ' case in duplicate rows
             Let strRws = strRws & rOuter + 1 & " " ' we building a list like   " 4 5 6 "  based on if the next is a duplicate value, which is determined by the last line
            Else '  without the last condition met, we might have the end of a group duplicate rows, in which case it would be time to organise a recursion run so ..... we check for this situation needing a recursion run
                If Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case we have at least 2 duplicates but have hit end of that list ( we have at least one space between indices, like " 3 4 " , " 6 7 8" etc..  ---- this , " 2 " , on the other hand, would would not have a space after trimming off the end spaces )
                 ' Now its time to organise a recursion run
                 Debug.Print "Found dups in last list column " & Clm & ",  " & strRws & " ,  so now main Rec Call " '     This is done for every duplicated
                 Call BubblesIndexIdeaWay(CopyNo + 1, arsRef(), strRws, strKeys) ' Rec Call 1  I need to sort the last duplicates
                 Let strRws = "" ' ready to try for another set of duplicates
                Else
                End If ' we did not have more than one indicie in strRws so usually that's it for this loop
            End If
            '+++*** this would be end of loop for most cases ... but Oh Fuck
    'Oh Fuck' ...below section catches rows at the end that might need to be sorted. ......|
            If rOuter = UBound(arsRef(), 1) - 1 And Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case of duplicates in last row
             Debug.Print "Found dups in last rows of last list, so now Rec Call at end of loop (Dups at list end case)"  ' loop end rec call - only done for duplicates at end of list
             Call BubblesIndexIdeaWay(CopyNo + 1, arsRef(), strRws, strKeys)
            Else
            End If  '...   ................................................................|
        Next rOuter   '   **************************************************************************
     Debug.Print "Ending a copy, Copy level " & CopyNo & ""
    End Sub

  3. #153
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Original test data range , ( B11:E16 )

    Row\Col
    A
    B
    C
    D
    E
    10
    11
    1
    5
    3
    a
    12
    9
    9
    9
    b
    13
    1
    4
    2
    c
    14
    8
    8
    8
    d
    15
    1
    3
    2
    e
    16
    7
    7
    7
    f
    Worksheet: Sorting







    Added initial row and column indicees

    Row\Col
    A
    B
    C
    D
    E
    10
    1
    2
    3
    4
    11
    1
    1
    5
    3
    a
    12
    2
    9
    9
    9
    b
    13
    3
    1
    4
    2
    c
    14
    4
    8
    8
    8
    d
    15
    5
    1
    3
    2
    e
    16
    6
    7
    7
    7
    f
    Worksheet: Sorting

  4. #154
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Sub Call_Sub_BubblesIndexIdeaWay()

    Calling routine required for previous Post and following Post


    Code:
    Sub Call_Sub_BubblesIndexIdeaWay() ' Partially hard coded for ease of explanation
    ' data range info
    Dim WsS As Worksheet: Set WsS = ThisWorkbook.Worksheets("Sorting")
    Dim RngToSort As Range: Set RngToSort = WsS.Range("B11:E16")
                                                                                  ' Set RngToSort = Selection '                          ' Selection.JPG : https://imgur.com/HnCdBt8
    
    Dim arrTS() As Variant ' This is somewhat redundant for this version and could be replaced by arrOrig()
     Let arrTS() = RngToSort.Value
    ' Index idea variables
     Let arrOrig() = arrTS()
     Let arrIndx() = arrTS()
     Let Cms() = Evaluate("=Column(A:D)") ' Convenient way to get
     Let Rs() = Evaluate("=Row(1:6)") ' Initial row indicies
    ' Add initial indicies
     Let RngToSort.Offset(-1, 0).Resize(1, 4).Value = Cms(): RngToSort.Offset(-1, 0).Resize(1, 4).Font.Color = vbRed
     Let RngToSort.Offset(0, -1).Resize(6, 1).Value = Rs(): RngToSort.Offset(0, -1).Resize(6, 1).Font.Color = vbRed
    ' Initial row indicies from full original range´of rows
    Dim strRows As String, Cnt As Long: Let strRows = " "
        For Cnt = 1 To 6
         Let strRows = strRows & Rs(Cnt, 1) & " "
        Next Cnt
    ' we should have now strRows = " 1 2 3 4 5 6 "
     Call BubblesIndexIdeaWay(1, arrIndx(), strRows, " 1 Asc 3 Asc 2 Asc ")
    ' Call BubblesIndexIdeaWay(1, arrIndx(), strRows, " 1 Asc ")
    ' Call BubblesIndexIdeaWay(1, arrIndx(), strRows, " 1 Asc 3 Asc ")
    ' Demo output
    Dim RngDemoOutput As Range: Set RngDemoOutput = WsS.Range("B31").Resize(RngToSort.Rows.Count, RngToSort.Columns.Count)
    ' Let WsS.Range("B31").Resize(RngToSort.Rows.Count, RngToSort.Columns.Count).Value = arrIndx()
     Let RngDemoOutput = arrIndx()
     Let RngDemoOutput.Offset(-1, 0).Resize(1, 4).Value = Cms(): RngToSort.Offset(-1, 0).Resize(1, 4).Font.Color = vbRed
     Let RngDemoOutput.Offset(0, -1).Resize(6, 1).Value = Rs(): RngToSort.Offset(0, -1).Resize(6, 1).Font.Color = vbRed
    End Sub

  5. #155
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Recursion routine for this post:
    http://www.excelfox.com/forum/showth...ll=1#post11078


    Code:
    Sub BubblesIndexIdeaWay(ByVal CpyNo As Long, ByRef arsRef() As Variant, ByVal strRws As String, ByVal strKeys As String)
    Dim CopyNo As Long: Let CopyNo = CpyNo ' On every recurssion run this will be increased by the Rec Call. This will be a local variable indicating the level down in recursion - I increase it by 1 at every Rec Call, that is to say each Rec Call gets given CopyNo+1  -  during a long tunnel down, the number at this pint will keep increasing to reflect how far down we are.  This ensures we then have the correct value place at the start of any newly starting copy of the recursion routine, since the first copy of the recursion routine pauses and starts the second copy of the recursion routine, the second copy of the recursion routine pauses and starts the third copy of the recursion routine ….. etc…
                                                                                 If CopyNo = 1 Then Debug.Print "First procedure Call"
    Rem -1 from the supplied  arguments, get all data needed in current bubble sort
    Dim Keys() As String: Let Keys() = Split(Replace(Trim(strKeys), "  ", " ", 1, -1, vbBinaryCompare), " ", -1, vbBinaryCompare) ''      The extra replace allows for me seperating with one or two spaces  - the following would do if I only used one space always    Split(Trim(strKeys), " ", -1, vbBinaryCompare) ' this will be an array twice as big as the number of keys that we have ...... ' Column for this level sort , and Ascending or Descending - both determined from the supplied forth arguments and the column/ copy number
                                                                                 If (2 * CopyNo) > UBound(Keys()) + 1 Then Debug.Print "You need more than " & (UBound(Keys()) + 1) / 2 & " keys to complete sort": Exit Sub ' case we have less keys then we need to sort, are array has twice as many elements as we have supplied keys since we have a key and whether it needs to get bigger or smaller , and array is from  0   so must add 1 to ubound then half it get the key number we gave.  We come here if the last column we gave as a Key had duplicates in it
                                                                                                                                         'Dim GlLl As Long: If Keys((CopyNo * 2) - 1) = "Desc" Then Let GlLl = 1 ' (CopyNo * 2) - 1)  gives as we go down levels   1  3  5  7 etc  We're seeing if we had  Desc  for this column
    Dim Clm As Long: Let Clm = CLng(Keys((CopyNo * 2) - 2)) '              ' (CopyNo * 2) - 2)  gives as we go down levels   0  2  4  6 etc  We  are  picking  out  the  supplied   column to sort by for each level
    Rem 1 Bubble sort
    Dim rOuter As Long ' ========"Left Hand"=====================Outer Loop=====================================
        ' For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1 ' THIS WOULD ONLY WORK FOR Copy No 1      For first row indicie to last but one row indicie - I could do this for copy 1
        For rOuter = Left(Trim(strRws), InStr(1, Trim(strRws), " ", vbBinaryCompare) - 1) To Right(Trim(strRws), Len(Trim(strRws)) - InStrRev(Trim(strRws), " ", -1, vbBinaryCompare)) - 1 '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
        'For rOuter = 1 To 5 ' For first run
        Dim rInner As Long ' -------Inner Loop-------------"Right Hand"--------------------------
            For rInner = rOuter + 1 To Rs(UBound(Rs(), 1), 1) ' from just above left hand through all the rest
                If CDbl(arsRef(rOuter, Clm)) > CDbl(arsRef(rInner, Clm)) Then
                Dim Temp As Variant ' I want to Swap those 2 above - I cant easilly in any coding change two values simulataneosly. So one of them Element values will put in this temporary place. This Element Values will then be given the other. Finally the other Element will be given this temporary value
                 Let Temp = arrIndx(rOuter, Clm): Let arrIndx(rOuter, Clm) = arrIndx(rInner, Clm): Let arrIndx(rInner, Clm) = Temp
                Dim TempRs As Long
                 Let TempRs = Rs(rOuter, 1): Let Rs(rOuter, 1) = Rs(rInner, 1): Let Rs(rInner, 1) = TempRs
                Else
                End If
            Next rInner ' -----------------------------------------------------------------------
        Next rOuter ' ==================End=Rem 1===============================================================
    Rem 2
     Let arrIndx() = Application.Index(arrOrig(), Rs(), Cms())
    Rem 3 Preparation for possible recursion Call
    ' Catpains Blog
     Debug.Print " Running Copy No. " & CopyNo & " of routine." & vbCr & vbLf & "  Sorted rows " & strRws & " based on values in column " & Clm & vbCr & vbLf & "   Checking now for Dups in that last sorted list" & vbCr & vbLf
    Dim tempStr As String: Let tempStr = strRws ' Need this bodge because I set strRws="" below
     Let strRws = ""
        'For rOuter = Rs(LBound(Rs(), 1), 1) To Rs(UBound(Rs(), 1), 1) - 1  ' Only valis for first Copy No 1
        For rOuter = Left(Trim(tempStr), InStr(1, Trim(tempStr), " ", vbBinaryCompare) - 1) To Right(Trim(tempStr), Len(Trim(tempStr)) - InStrRev(Trim(tempStr), " ", -1, vbBinaryCompare)) - 1                                   '   ImmediteWindowToHelpGetAtStuff.JPG : https://imgur.com/yGqmYSu : http://www.eileenslounge.com/viewtopic.php?f=27&t=16407&p=247121#p247121
            If strRws = "" Or InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then Let strRws = " " & rOuter & " " ' case starting again to get duplicates
            'If strRws = "" Or Trim(strRws) = rOuter - 1 Then Let strRws = " " & rOuter & " " ' alternative
            If Trim(UCase(CStr(arrIndx(rOuter, Clm)))) = Trim(UCase(CStr(arrIndx(rOuter + 1, Clm)))) Then ' case in duplicate rows
             Let strRws = strRws & rOuter + 1 & " " ' we building a list like   " 4 5 6 "  based on if the next is a duplicate value, which is determined by the last line
            Else '  without the last condition met, we might have the end of a group duplicate rows, in which case it would be time to organise a recursion run so ..... we check for this situation needing a recursion run
                If Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case we have at least 2 duplicates but have hit end of that list ( we have at least one space between indices, like " 3 4 " , " 6 7 8" etc..  ---- this , " 2 " , on the other hand, would would not have a space after trimming off the end spaces )
                 ' Now its time to organise a recursion run
                 Debug.Print "Found dups in last list column " & Clm & ",  " & strRws & " ,  so now main Rec Call " '     This is done for every duplicated
                 Call BubblesIndexIdeaWay(CopyNo + 1, arrIndx(), strRws, strKeys) ' Rec Call 1  I need to sort the last duplicates
                 Let strRws = "" ' ready to try for another set of duplicates
                Else
                End If ' we did not have more than one indicie in strRws so usually that's it for this loop
            End If
            '+++*** this would be end of loop for most cases ... but Oh Fuck
    'Oh Fuck' ...below section catches rows at the end that might need to be sorted. ......|
            If rOuter = UBound(arrIndx(), 1) - 1 And Not InStr(1, Trim(strRws), " ", vbBinaryCompare) = 0 Then ' case of duplicates in last row
             Debug.Print "Found dups in last rows of last list, so now Rec Call at end of loop (Dups at list end case)"  ' loop end rec call - only done for duplicates at end of list
             Call BubblesIndexIdeaWay(CopyNo + 1, arrIndx(), strRws, strKeys)
            Else
            End If  '...   ................................................................|
        Next rOuter   '   **************************************************************************
     Debug.Print "Ending a copy, Copy level " & CopyNo & ""
    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!!

  6. #156
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Some Killing File tests / What order does Dir(with wild cards) find stuff

    Some notes , tests in support of this
    https://www.excelforum.com/excel-pro...-the-file.html
    These are just some notes and tests into what order the Dir( with wild cards ) thing does stuff.


    Introduction
    VBA Dir Function thing ( https://docs.microsoft.com/en-us/off...p/dir-function )
    In the simplest form, ….._
    _____ Dir(Fullpath&FileName, __ )
    _............
    Code:
    Dim IsFileName As String
      IsFileName = Dir("C:\MyFolder\myFileName.xls", __ )
    this basically gives you the file name back if it exists, based on you giving it the full path and File name string, Fullpath&FileName.
    In the above example, if you had the file "myFileName.xls" in the foilder, "MyFolder", then the text "myFileName.xls" would be Placed in variable , IsFileName
    If that file does not exist, then it gives you back nothing, or rather an empty string of sorts “” ( I believe Dir is a throw back to older early computer days, when you typed something like Dir C:_____, and the result was that you got to go to that place which Dir C:_____ represented )
    It seem that in VBA the Dir is mostly used to loop through all files in a single folder*. ( *It does not suit too well for use in coding looking at all files in folders and sub folders ). The suitability of the Dir function for this is based on a couple of things.
    _(i) In Microsoft Windows, Dir supports the use of multiple character (*) and single character (?) wildcards to specify multiple files. ......
    You can use wild cards in the full path and file name string that you give it, so it will look for a file matching your given string. ( So typically you might give a string like “C:\myFolder\*.xl*”, which would look for any excel file: In this bit *.xl* , the first * allows for any file name, and the second * will allow for extensions such as .xls , .xlsm, .xlsx, .. etc… )
    __Dir _____ without arguments
    Code:
      IsFileName = Dir
    _(ii) After you use like Dir(Fullpath&FileName, __ ) once, then any use after of just _ Dir __ without any arguments, will give the next file it finds based on the criteria given by the wild carded full path and file name string you gave in the first use with arguments, or it returns "" if there are no further files meeting the criteria given by the wild carded full path and file name string you gave in the first use with arguments

    What this post is about:
    My interest was sparked by the reference thread ( https://www.excelforum.com/excel-pro...-the-file.html )
    I am interested in finding out which of the files Dir or Dir(Fullpath&FileName, __ )will choose if there are more than 1 file meeting the criteria of a string , Fullpath&FileName , containing wild cards




    Experiments so far
    I made a test folder , named "Folder"
    Folder.JPG : https://imgur.com/l9OwlQi
    Attachment 2213

    I created my files in this order
    _1 “wbCodes.xlsm” --- the main file with all the codes in it. This is in the same Folder as the folder which I named "Folder" ( The main Folder is called “Kill Stuff” : Kill Stuff Folder.JPG : https://imgur.com/hN26AoW )
    After making the main File, I created the folder, "Folder" , and created the following files in it. I created the following three files in the following order,
    _2 “SecondFirstAfterwb.xlsx” --- made first after making “wbCodes.xlsm”
    _3 “ThirdSecondAfterwb.xlsx” --- made second after making “wbCodes.xlsm”
    _4 “AForthThirdAfterwb.xlsx” --- made third after making “wbCodes.xlsm”

    I modified the codes from Alf and sintek from the referenced Thread, thus, ( I am mainly interested in the first part of the routines, as this deals with what the Dir chooses )

    Code:
    Sub zed369() ' sintek
    Dim Path As String, File As String, Cnt As Long
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    ' sintek's Dir Stuff
    'Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook: Set ws1 = wb1.Sheets("Sheet1")
    Path = ThisWorkbook.Path & "\Folder\": File = Dir(Path & "*.xl*") ' For this example, specific file is in a folder called Folder...same path as macro file...
    Debug.Print "First got by  Dir  is " & File
    Debug.Print
        For Cnt = 1 To 3 - 1 ' -1 because we have three files, but typically the first is got from the first use of Dir , which is typically outside the loop
         File = Dir: Debug.Print " use " & Cnt & " in loop of  Dir   gives " & File
        Next Cnt
    Debug.Print
    Debug.Print
    
    
    ' sintek's way to do stuff
    'Stop ' _____________________________________________________________________________
    'Set wb2 = Workbooks.Open(Path & File): Set ws2 = wb2.Sheets("Tabelle1")
    'With ws2
    '    .UsedRange.Copy ws1.Range("A1")
    'End With
    'wb2.Close
    'Kill Path & File
    'Application.ScreenUpdating = True
    End Sub
    '
    Sub CopyAndKill() ' Alf
    Dim aString As String, Cnt As Long, aStringToOpen As String
    ' Alf's Dir stuff
    'aString = Dir("N:\a_test\")
    aString = Dir("F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\")
    Debug.Print "First got by  Dir  is " & aString
    aStringToOpen = "F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\" & Dir("F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\")
    Debug.Print "First file will be opened using this string  " & vbCrLf & aStringToOpen
    Debug.Print
        For Cnt = 1 To 3 - 1 ' -1 because we have three files, but typically the first is got from the first use of Dir , which is typically outside the loop
         aString = Dir: Debug.Print " use " & Cnt & " in loop of  Dir   gives " & aString
        Next Cnt
    Debug.Print
    Debug.Print
    
    
    'Stop ' __________________________________________________________________
    ' Alf's way to do the stuff
    'Workbooks.Open ("N:\a_test\" & Dir("N:\a_test\"))
    'Sheets("Sheet1").Activate
    'ActiveSheet.UsedRange.Copy
    'ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlAll
    'Application.CutCopyMode = False
    'Windows(aString).Close
    'Kill ("N:\a_test\" & Dir("N:\a_test\"))
    End Sub
    I get this sort of output ( in the immediate window )
    Code:
     First got by  Dir  is AForthThirdAfterwb.xlsx
    
     use 1 in loop of  Dir   gives SecondFirstAfterwb.xlsx
     use 2 in loop of  Dir   gives ThirdSecondAfterwb.xlsx
    
    
    First got by  Dir  is AForthThirdAfterwb.xlsx
    First file will be opened using this string  
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\AForthThirdAfterwb.xlsx
    
     use 1 in loop of  Dir   gives SecondFirstAfterwb.xlsx
     use 2 in loop of  Dir   gives ThirdSecondAfterwb.xlsx
    Initially it appears that I get alphabetic order.
    But possibly there could be more to it than that.
    I will look again at this in a few days , possibly on some other computers and systems, and experiment with various settings , etc….




    In the next posts I will use this simplified routine which is only interested in looking at the order in which Dir chooses files.
    Rem 1 gives a few ways to get the string up to and including the Folder in which files are to be searched for, ( in the form below , ‘1b ) , is used to get the folder named “Folder” in the same folder as the workbook in which the routine is placed )
    Rem 2 : As before, an initial use of Dir(C:\somewhers\kjhfkhs.*sdfjkah,___) is made to set the search criteria, followed by the un argumented Dir in a loop which then looks for the next files
    Code:
    Sub DirOrder()
    Dim strWB As String
    Rem 1 get the full string, strWB, for a Folder to use in the  Dir(Fullpath&FileName, __ )        ( strWB=Fullpath&FileName - FileName )
    '1a) use the asking pop up thing, File dialogue folder picker
    '   With Application.FileDialog(msoFileDialogFolderPicker)
    '    .Title = "Folder Select"
    '    .AllowMultiSelect = False
    '        If .Show <> -1 Then
    '         Exit Sub
    '        Else
    '        End If
    '    Let strWB = .SelectedItems(1)  '  & "\"
    '   End With
    '
    '1b) Using a test Folder, named  Folder  in the same Folder as the workbook in which this code is
     Let strWB = ThisWorkbook.Path & "\Folder"
    '1c) Hard code instead
    'Let strWB = "F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder"
     Debug.Print "Folder used is" & vbCrLf & strWB & vbCrLf & "" & Right(strWB, (Len(strWB) - InStrRev(strWB, "\", -1, vbTextCompare)))
     Debug.Print
     Let strWB = strWB & "\"
    Rem 2 add last file bit for use in the  Dir(Fullpath&FileName, __ ) , but include wild cards...    http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11089&viewfull=1#post11089 :  _(i) You can use wild cards in the full path and file name string that you give it, so it will look for a file matching your given string. ( So typically you might give a string like “C:\myFolder\*.xl*”, which would look for any excel file: In this bit *.xl* , the first * allows for any file name, and the second * will allow for extensions such as .xls , .xlsm, .xlsx, .. etc… )      _(ii) After you use like Dir(Fullpath&FileName, __ ) once, then any use after of just _ Dir __ without any arguments, will give the next file it finds based on the string you gave in the first use with arguments
    '2a)  Excel files
     Let strWB = strWB & "*.xls*"
    Dim file As String: Let file = Dir(strWB)
     Debug.Print "First got by  Dir(" & strWB & ")" & vbCrLf & "is             " & file
     Debug.Print
    Dim Cnt As Long
        For Cnt = 1 To 3 - 1 ' -1 because we have three files, but typically the first is got from the first use of Dir , which is typically outside the loop
         file = Dir: Debug.Print "Use " & Cnt & " in loop of unargumented   Dir   gives   " & file
        Next Cnt
     Debug.Print
     Debug.Print
    End Sub
    This would be comparible output ( in the Immedite Window ( http://www.eileenslounge.com/viewtop...247121#p247121 ) ) to the test files anf folder used so far

    HTML Code:
    Folder used is
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder
    Folder
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   SecondFirstAfterwb.xlsx
    Use 2 in loop of unargumented   Dir   gives   ThirdSecondAfterwb.xlsx
    And here is what it looks like in the explorer window:
    ExpOrder1.JPG : https://imgur.com/OfQfHeH
    Attachment 2224
    Attached Images Attached Images

  7. #157
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Dir Order

    Here is where we left off in the last post
    HTML Code:
    Folder used is
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder
    Folder
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   SecondFirstAfterwb.xlsx
    Use 2 in loop of unargumented   Dir   gives   ThirdSecondAfterwb.xlsx
    here is what it looks like in the explorer window:
    ExpOrder1.JPG : https://imgur.com/OfQfHeH


    I can move the order pysically in the explorer window, by selecting and dragging the file position virtically, ( and I hit the refresh thing , just in case that should influence anything )
    ExpOrder2.JPG : https://imgur.com/AlV1MdB
    The routine, Sub DirOrder() , then seems to give the same results
    HTML Code:
    Folder used is
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder
    Folder
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   SecondFirstAfterwb.xlsx
    Use 2 in loop of unargumented   Dir   gives   ThirdSecondAfterwb.xlsx


    I can do this:
    ExpOrder3a.JPG : https://imgur.com/RBSa9Ou
    ExpOrder3a.JPG : https://imgur.com/2OVsguZ
    Once again I get the same alphabetical ordering in the Dir found order output

    i can play around with this:
    ExpOrder4.JPG : https://imgur.com/6FbYQgp
    or this
    Stack by change date.jpg : https://imgur.com/YIrTxpp , https://imgur.com/ht887FU , https://imgur.com/lHMcUjA
    Attachment 2226
    Once again I get the same alphabetical ordering in the Dir found order output


    I made this d_xlsm_file.xlsm , and this ,c_xls_file.xls , and pit it in the foilder, Folder
    A xlsm and xls.JPG : https://imgur.com/w9gyRxj
    Attachment 2225
    here is part of my Immediate window output
    HTML Code:
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   c_xls_file.xls
    Use 2 in loop of unargumented   Dir   gives   d_xlsm_file.xlsm
    I need to increase my loop count, Cnt , to 4 to getting total all 5 files. But doing this is likely to get a bit tedious as I comtinue experiments with a different number of files in various folders. So I will change my coding, at the loop section, to a more typical type of loop used iin such a Dir __ file finding code: Usually something like this is done, so that the loop keeps going as long as Dir __ finds files
    Code:
     
    ‘ First use of Dir  with full path and file name argument
    ‘  strWB = "F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*" ‘ Wild card to get all Excel Files
    ‘  File = Dir(strWB)‘ Loop for all files meeting search string criteria,  ( all Excel files in this example )
        Do '                  '_- I want to keep going in a Loop while I still get a file name returned by  Dir
        Dim Cnt As  Long: Let Cnt = Cnt + 1
         File = Dir: Debug.Print "Use " & Cnt & " in loop of unargumented   Dir   gives   """ & File & """"
        Loop While File <> "" '_- I want to keep going in a Loop while I still get a file name returned by  Dir
    Here is the full coding, http://www.excelfox.com/forum/showth...ll=1#post11108 , which gives for the last example:
    HTML Code:
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder
    Folder
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*.xls*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   "c_xls_file.xls"
    Use 2 in loop of unargumented   Dir   gives   "d_xlsm_file.xlsm"
    Use 3 in loop of unargumented   Dir   gives   "SecondFirstAfterwb.xlsx"
    Use 4 in loop of unargumented   Dir   gives   "ThirdSecondAfterwb.xlsx"
    Use 5 in loop of unargumented   Dir   gives   ""


    I can change the serach criteria from strWB & "*.xls*" to strWB & "*" and it has no effect

    i added a .jpg pic, ( Add a jpg.JPG : https://imgur.com/XkXskiL ) , and the listing had it in the aplhabetical order :
    Code:
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder\*)
    is             AForthThirdAfterwb.xlsx
    
    Use 1 in loop of unargumented   Dir   gives   "c_xls_file.xls"
    Use 2 in loop of unargumented   Dir   gives   "d_xlsm_file.xlsm"
    Use 3 in loop of unargumented   Dir   gives   "SecondFirstAfterwb.xlsx"
    Use 4 in loop of unargumented   Dir   gives   "Stack by change date  .JPG"
    Use 5 in loop of unargumented   Dir   gives   "ThirdSecondAfterwb.xlsx"
    Use 6 in loop of unargumented   Dir   gives   ""

    I use the last routine in the form to allow user selection of the folder to search for files
    http://www.excelfox.com/forum/showth...ll=1#post11108. I looked at some arbritrary folders, - once again alphabetical order seems to come out:
    Code:
    Folder used is
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery
    wbSheetMakerClsdWbADOMsQueery
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\*)
    is             83DB8900
    
    Use 1 in loop of unargumented   Dir   gives   "aaa.xlsm"
    Use 2 in loop of unargumented   Dir   gives   "Amar321.xls.xlsx"
    Use 3 in loop of unargumented   Dir   gives   "ApparantlyApparantIs_Change.JPG"
    Use 4 in loop of unargumented   Dir   gives   "Aufzeichnen.JPG"
    Use 5 in loop of unargumented   Dir   gives   "Book1.xls"
    Use 6 in loop of unargumented   Dir   gives   "Book1.xlsm.zip"
    Use 7 in loop of unargumented   Dir   gives   "CA930CD8.tmp"
    Use 8 in loop of unargumented   Dir   gives   "ClosedWorkbook.xlsm"
    Use 9 in loop of unargumented   Dir   gives   "CopyASheet.JPG"
    Use 10 in loop of unargumented   Dir   gives   "CresssieFiles.JPG"
    Use 11 in loop of unargumented   Dir   gives   "DB2IssJfürELProAbDec2014.xlsm"
    Use 12 in loop of unargumented   Dir   gives   "EFFldr.xlsm"
    Use 13 in loop of unargumented   Dir   gives   "EFldr1_1 Download.JPG"
    Use 14 in loop of unargumented   Dir   gives   "Eileens Fldr.zip"
    Use 15 in loop of unargumented   Dir   gives   "EileensFldr Contents Copy.JPG"
    Use 16 in loop of unargumented   Dir   gives   "EileensFldr Contents Paste.JPG"
    Use 17 in loop of unargumented   Dir   gives   "EileensFldr Make Empty Folder.JPG"
    Use 18 in loop of unargumented   Dir   gives   "EileensFldr zip  Download.JPG"
    Use 19 in loop of unargumented   Dir   gives   "EileensFldr.zip"
    Use 20 in loop of unargumented   Dir   gives   "EileensFolderExplainedOutput.JPG"
    Use 21 in loop of unargumented   Dir   gives   "Example Folder and Macro File in same Folder.JPG"
    Use 22 in loop of unargumented   Dir   gives   "FBandData.xlsm"
    Use 23 in loop of unargumented   Dir   gives   "FBandDataNorie.xlsm"
    Use 24 in loop of unargumented   Dir   gives   "FBandDataNorie.xlsx"
    Use 25 in loop of unargumented   Dir   gives   "FormulaBarClosedWB.JPG"
    Use 26 in loop of unargumented   Dir   gives   "GetData_ClosedBook+LINKS.xlsx"
    Use 27 in loop of unargumented   Dir   gives   "GetData_ClosedBook.xls"
    Use 28 in loop of unargumented   Dir   gives   "HimanshuktwCode.JPG"
    Use 29 in loop of unargumented   Dir   gives   "KissMyClosedWB.JPG"
    Use 30 in loop of unargumented   Dir   gives   "Mappe2.xlsm"
    Use 31 in loop of unargumented   Dir   gives   "MazanDikCollectionWonk.xlsm"
    Use 32 in loop of unargumented   Dir   gives   "mellowtangSummarySheets.xlsm"
    Use 33 in loop of unargumented   Dir   gives   "MsQueerOptions.JPG"
    Use 34 in loop of unargumented   Dir   gives   "myFileToClose.xlsm"
    Use 35 in loop of unargumented   Dir   gives   "MyNewWorkbook.xlsx"
    Use 36 in loop of unargumented   Dir   gives   "MySameFolder.JPG"
    Use 37 in loop of unargumented   Dir   gives   "NeuProAktuelleMakros.xlsm"
    Use 38 in loop of unargumented   Dir   gives   "NormalThisWorkbookCodeModule.JPG"
    Use 39 in loop of unargumented   Dir   gives   "NutritionalValues2016.xlsx"
    Use 40 in loop of unargumented   Dir   gives   "OnlyGets8810RowsInAQuerrListObjectTableThingyAnyways.JPG"
    Use 41 in loop of unargumented   Dir   gives   "Plop.xlsm"
    Use 42 in loop of unargumented   Dir   gives   "poo.xlsm"
    Use 43 in loop of unargumented   Dir   gives   "RudyMSRAllSubFldrsFndRep.xlsm"
    Use 44 in loop of unargumented   Dir   gives   "Sample.zip"
    Use 45 in loop of unargumented   Dir   gives   "SchemaIniErrorPipe.JPG"
    Use 46 in loop of unargumented   Dir   gives   "SrangeThisWorkbookCodeModule.JPG"
    Use 47 in loop of unargumented   Dir   gives   "StopClosing.xlsm"
    Use 48 in loop of unargumented   Dir   gives   "Summary sheet.xlsm"
    Use 49 in loop of unargumented   Dir   gives   "template test.xlsm"
    Use 50 in loop of unargumented   Dir   gives   "Top100MsQuery.JPG"
    Use 51 in loop of unargumented   Dir   gives   "ViskasVerticalsMaster dataMjoza.xlsm"
    Use 52 in loop of unargumented   Dir   gives   "wb2.csv"
    Use 53 in loop of unargumented   Dir   gives   "wb2.xlsm"
    Use 54 in loop of unargumented   Dir   gives   "wb2.xlsx"
    Use 55 in loop of unargumented   Dir   gives   "WBAccessTimeTestData.xlsx"
    Use 56 in loop of unargumented   Dir   gives   "WBAccestTimeTest.xlsm"
    Use 57 in loop of unargumented   Dir   gives   "wbCodes.xlsb"
    Use 58 in loop of unargumented   Dir   gives   "wbCodes.xlsm"
    Use 59 in loop of unargumented   Dir   gives   "WBOpenRenameKlaredog.xls"
    Use 60 in loop of unargumented   Dir   gives   "WBOpenRenameKlaredog.xlsm"
    Use 61 in loop of unargumented   Dir   gives   "Wb_with_5Sheets_4Worksheets.xlsm"
    Use 62 in loop of unargumented   Dir   gives   "WillyWonks.JPG"
    Use 63 in loop of unargumented   Dir   gives   "workbook2.xlsm"
    Use 64 in loop of unargumented   Dir   gives   "WorkbookOpenMsgBox.JPG"
    Use 65 in loop of unargumented   Dir   gives   "WorksheetNames.JPG"
    Use 66 in loop of unargumented   Dir   gives   "Worksheet_Change.JPG"
    Use 67 in loop of unargumented   Dir   gives   ""
    Note that a file named as a number comes first in the list, as is consitant with Excel regarding text as "larger" than a number in sorting things http://www.eileenslounge.com/viewtop...=32154#p249178


    Up until now, all tests were done on an old Lap top using Vista operating system. I rechecked on a newer machine uisng Windows 7. I get the same results














    "wbCodes.xlsm" : https://app.box.com/s/gfuintgifu1hgw5nap3jriz2x8mp911x ( Sub DirOrder() is here )
    folder, "Folder" : https://app.box.com/s/vmmzeboetkt07ocggbx6p8lkurmp5wca
    "wbCodes.xls" : https://app.box.com/s/gmdne53vehhuc6lvz3vfgyxqmwy07xlz ( Sub DirOrder() is here )
    Attached Images Attached Images

  8. #158
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    There is a second argument to Dir. It is not used much. https://docs.microsoft.com/en-us/off...ction#settings
    One option will make it return folder names as well. For our example we can change Dir(strWB) to any if these: Dir(strWB, vbDirectory) ; Dir(PathName:=strWB, Attributes:=vbDirectory) ; Dir(PathName:=strWB, Attributes:=16) ; Dir(strWB, 16)
    Running the routine with the previous example, seems to slip the folder names in the appropriate place to once again have everything in alphabetical order
    Code:
    Folder used is
    F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery
    wbSheetMakerClsdWbADOMsQueery
    
    First got by  Dir(F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\*)
    is             .
    
    Use 1 in loop of unargumented   Dir   gives   ".."
    Use 2 in loop of unargumented   Dir   gives   "83DB8900"
    Use 3 in loop of unargumented   Dir   gives   "aaa.xlsm"
    Use 4 in loop of unargumented   Dir   gives   "ACDC"
    Use 5 in loop of unargumented   Dir   gives   "Amar321.xls.xlsx"
    Use 6 in loop of unargumented   Dir   gives   "ApparantlyApparantIs_Change.JPG"
    Use 7 in loop of unargumented   Dir   gives   "Aufzeichnen.JPG"
    Use 8 in loop of unargumented   Dir   gives   "Bad Files"
    Use 9 in loop of unargumented   Dir   gives   "Book1.xls"
    Use 10 in loop of unargumented   Dir   gives   "Book1.xlsm.zip"
    Use 11 in loop of unargumented   Dir   gives   "CA930CD8.tmp"
    Use 12 in loop of unargumented   Dir   gives   "ClosedWorkbook.xlsm"
    Use 13 in loop of unargumented   Dir   gives   "ClsdWbs"
    Use 14 in loop of unargumented   Dir   gives   "CopyASheet.JPG"
    Use 15 in loop of unargumented   Dir   gives   "CressieFolder"
    Use 16 in loop of unargumented   Dir   gives   "CresssieFiles.JPG"
    Use 17 in loop of unargumented   Dir   gives   "DB2IssJfürELProAbDec2014.xlsm"
    Use 18 in loop of unargumented   Dir   gives   "EFFldr.xlsm"
    Use 19 in loop of unargumented   Dir   gives   "EFldr1_1"
    Use 20 in loop of unargumented   Dir   gives   "EFldr1_1 Download.JPG"
    Use 21 in loop of unargumented   Dir   gives   "Eileens Fldr.zip"
    Use 22 in loop of unargumented   Dir   gives   "EileensFldr"
    Use 23 in loop of unargumented   Dir   gives   "EileensFldr Contents Copy.JPG"
    Use 24 in loop of unargumented   Dir   gives   "EileensFldr Contents Paste.JPG"
    Use 25 in loop of unargumented   Dir   gives   "EileensFldr Make Empty Folder.JPG"
    Use 26 in loop of unargumented   Dir   gives   "EileensFldr zip  Download.JPG"
    Use 27 in loop of unargumented   Dir   gives   "EileensFldr.zip"
    Use 28 in loop of unargumented   Dir   gives   "EileensFolderExplainedOutput.JPG"
    Use 29 in loop of unargumented   Dir   gives   "Example Folder and Macro File in same Folder.JPG"
    Use 30 in loop of unargumented   Dir   gives   "FBandData.xlsm"
    Use 31 in loop of unargumented   Dir   gives   "FBandDataNorie.xlsm"
    Use 32 in loop of unargumented   Dir   gives   "FBandDataNorie.xlsx"
    Use 33 in loop of unargumented   Dir   gives   "FormulaBarClosedWB.JPG"
    Use 34 in loop of unargumented   Dir   gives   "GetData_ClosedBook+LINKS.xlsx"
    Use 35 in loop of unargumented   Dir   gives   "GetData_ClosedBook.xls"
    Use 36 in loop of unargumented   Dir   gives   "HimanshuktwCode.JPG"
    Use 37 in loop of unargumented   Dir   gives   "Kill Stuff"
    Use 38 in loop of unargumented   Dir   gives   "KissMyClosedWB.JPG"
    Use 39 in loop of unargumented   Dir   gives   "MacroRecording"
    Use 40 in loop of unargumented   Dir   gives   "Mappe2.xlsm"
    Use 41 in loop of unargumented   Dir   gives   "MazanDikCollectionWonk.xlsm"
    Use 42 in loop of unargumented   Dir   gives   "mellowtangSummarySheets.xlsm"
    Use 43 in loop of unargumented   Dir   gives   "MsQueerOptions.JPG"
    Use 44 in loop of unargumented   Dir   gives   "MsQueeryADO"
    Use 45 in loop of unargumented   Dir   gives   "myFileToClose.xlsm"
    Use 46 in loop of unargumented   Dir   gives   "MyNewWorkbook.xlsx"
    Use 47 in loop of unargumented   Dir   gives   "MySameFolder.JPG"
    Use 48 in loop of unargumented   Dir   gives   "Neuer Ordner"
    Use 49 in loop of unargumented   Dir   gives   "NeuProAktuelleMakros.xlsm"
    Use 50 in loop of unargumented   Dir   gives   "NormalThisWorkbookCodeModule.JPG"
    Use 51 in loop of unargumented   Dir   gives   "NutritionalValues2016.xlsx"
    Use 52 in loop of unargumented   Dir   gives   "OnlyGets8810RowsInAQuerrListObjectTableThingyAnyways.JPG"
    Use 53 in loop of unargumented   Dir   gives   "Plop.xlsm"
    Use 54 in loop of unargumented   Dir   gives   "poo.xlsm"
    Use 55 in loop of unargumented   Dir   gives   "RudyMSRAllSubFldrsFndRep.xlsm"
    Use 56 in loop of unargumented   Dir   gives   "Sample.zip"
    Use 57 in loop of unargumented   Dir   gives   "SchemaIniErrorPipe.JPG"
    Use 58 in loop of unargumented   Dir   gives   "SrangeThisWorkbookCodeModule.JPG"
    Use 59 in loop of unargumented   Dir   gives   "StopClosing.xlsm"
    Use 60 in loop of unargumented   Dir   gives   "Summary sheet.xlsm"
    Use 61 in loop of unargumented   Dir   gives   "template test.xlsm"
    Use 62 in loop of unargumented   Dir   gives   "Top100MsQuery.JPG"
    Use 63 in loop of unargumented   Dir   gives   "ViskasVerticalsMaster dataMjoza.xlsm"
    Use 64 in loop of unargumented   Dir   gives   "wb2.csv"
    Use 65 in loop of unargumented   Dir   gives   "wb2.xlsm"
    Use 66 in loop of unargumented   Dir   gives   "wb2.xlsx"
    Use 67 in loop of unargumented   Dir   gives   "WBAccessTimeTestData.xlsx"
    Use 68 in loop of unargumented   Dir   gives   "WBAccestTimeTest.xlsm"
    Use 69 in loop of unargumented   Dir   gives   "wbCodes.xls"
    Use 70 in loop of unargumented   Dir   gives   "wbCodes.xlsb"
    Use 71 in loop of unargumented   Dir   gives   "wbCodes.xlsm"
    Use 72 in loop of unargumented   Dir   gives   "WBOpenRenameKlaredog.xls"
    Use 73 in loop of unargumented   Dir   gives   "WBOpenRenameKlaredog.xlsm"
    Use 74 in loop of unargumented   Dir   gives   "Wb_with_5Sheets_4Worksheets.xlsm"
    Use 75 in loop of unargumented   Dir   gives   "WillyWonks.JPG"
    Use 76 in loop of unargumented   Dir   gives   "WonkBook"
    Use 77 in loop of unargumented   Dir   gives   "workbook2.xlsm"
    Use 78 in loop of unargumented   Dir   gives   "WorkbookOpenMsgBox.JPG"
    Use 79 in loop of unargumented   Dir   gives   "WorksheetNames.JPG"
    Use 80 in loop of unargumented   Dir   gives   "Worksheet_Change.JPG"
    Use 81 in loop of unargumented   Dir   gives   "XYT"
    Use 82 in loop of unargumented   Dir   gives   ""
    Here is the folder used for the last two tests: wbFolder.JPG : https://imgur.com/MMydq7n
    ….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!!

  9. #159
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10

    Deafault item to use if empty column

    In support of answer to this excelfox Thread:
    http://www.excelfox.com/forum/showth...ll=1#post11090


    Code:
    Option Explicit
    Sub DefaultItem()
    Rem 1 data range info
    Dim rngIn As Range, Lr As Long, ClmCnt As Long
     Let ClmCnt = 3 '                                                                                                     : Let ClmCnt = Worksheets("Sheet2").Range("A1").CurrentRegion.Columns.Count
     Let Lr = Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count
     Set rngIn = Worksheets("Sheet2").Range("A1:C" & Lr & "")
    Rem 2 Data to array
    Dim arrDtaIn() As Variant '  I need Variant type as the  .Value  in the next line returns a field of Variant type elements
     Let arrDtaIn() = rngIn.Value
    Rem 3 Determine default values
    ' 3a) Number of groups
    Dim arrGp() As Variant: Let arrGp() = Application.Index(rngIn, 0, 1).Value ' http://www.excelfox.com/forum/showthread.php/1111-VBA-Trick-of-the-Week-Slicing-an-Array-Without-Loop-%E2%80%93-Application-Index         Highlight arrGp and Hit F9.JPG : https://imgur.com/PZF0oXE
    Dim strGps As String: Let strGps = " " ' For a string like " 1 2 3 "
    Dim cnt As Long
        For cnt = 2 To Lr ' looking at all rows from the second in our input data
            If InStr(1, strGps, " " & arrGp(cnt, 1) & " ") = 0 Then ' This looks for the positiopn along ( starting from character 1  ,  in  strGps  ,  of  each row element arrGp(cnt, 1)  )    if it is not found then Instr retourns  0   as a n indication that it was not there
             Let strGps = strGps & arrGp(cnt, 1) & " "              ' Because it is not there, we now put it in
            Else
            End If
        Next cnt
    ' At this point we should have like  strGps = " 1 2 3 "
    ' 3b) Array of unique groups
     Let strGps = Trim(strGps) ' This takes off the first and last trailing spaces
    Dim arrGps() As String     ' The string split function below returns a fiels of String elements : Highlight arrGps  Hit F9.JPG : https://imgur.com/LT9dgHk
     Let arrGps() = Split(strGps, " ", -1, vbBinaryCompare)      ' this splits the (  strgps  ,  using  " " as denominator  ,  and returns all elemants in an array,  using exact binary computer match on the " "  )
    ' 3c) Array for output
    Dim arrOut() As String ' A dynamic array is needed as I can only use variables in the  ReDim  method - I cannot use varable in the declaration (Dim) statement
     ReDim arrOut(1 To UBound(arrGps()) + 2, 1 To 2) ' I  want +1 rows for the header   I also need +1 because split retouns a 1 dimensional array stating at indicie 0 - so the Ubound of arrGps() will give a numbe 1 less than I might expect - in our example we have 3 elements with  indicies of 0 1 2, ( and values in our example of 1 2 3 - for example  arrGps(0)=1  )  so the Ubound returns 2 - but we want 3 elements
    ' 3d) fill my arrOut()
    Dim Stear As Variant ' I want to use a  For ´Each  loop below   VBA must have an object varaible or a variable of variant type to hold each  item  in a collection of something. Our arrGps() can be considered a collection of numbers  1 2 3
    Dim ArrOutRw As Long: Let ArrOutRw = 1 ' Our row number in the outout array  : I use 1 initially, for the header
     Let arrOut(ArrOutRw, 1) = arrDtaIn(1, 1): Let arrOut(ArrOutRw, 2) = "Deafault item"
        For Each Stear In arrGps() ' This outer loop goes throug each unique group number =============== - For each number in  { 1, 2, 3 }
            For cnt = 2 To Lr ' An Inner loop to go through all data rows ' -----------------------------
                If CStr(arrDtaIn(cnt, 1)) = CStr(Stear) Then ' This will catch the first use of our group number, Stear is our group number taken from the array  1 2 3
                 Let ArrOutRw = ArrOutRw + 1                 ' Our next row to fill in arrOut()
                 Let arrOut(ArrOutRw, 1) = Stear             ' First column in our output array
                 Let arrOut(ArrOutRw, 2) = arrDtaIn(cnt, 2)  ' Second column in our output array will be given the first item in column B of our data for this group number, Stear
                 Exit For                                    ' I only want to get the first item for a group number
                Else
                End If
            Next cnt ' ----------------------------------------------------------------------------------
        Next Stear ' ====================================================================================
    ' at this point we have an array for output of default : Select ArrOut   then Hit F9.JPG : https://imgur.com/CNMeYV9
    Rem 4 Demo Output
    Let rngIn.Offset(0, ClmCnt).Resize(UBound(arrOut(), 1), 2).Value = arrOut() ' In the range which offset to the right of the input, of the dimension size of the output array, I paste my values out
     
    End Sub
    Attached Files Attached Files

  10. #160
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Coding in suport of these excelfox Threads and posts:
    http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11089&viewfull=1#post11089
    https://www.excelforum.com/excel-pro...-the-file.html


    Code:
    Sub DirOrder()  '  http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11092&viewfull=1#post11092
    Dim strWB As String
    Rem 1 get the full string, strWB, for a Folder to use in the  Dir(Fullpath&FileName, __ )        ( strWB=Fullpath&FileName - FileName )
    '1a) use the asking pop up thing, File dialogue folder picker
    '   With Application.FileDialog(msoFileDialogFolderPicker)
    '    .Title = "Folder Select"
    '    .AllowMultiSelect = False
    '        If .Show <> -1 Then
    '         Exit Sub
    '        Else
    '        End If
    '    Let strWB = .SelectedItems(1)  '  & "\"
    '   End With
    '
    '1b) Using a test Folder, named  Folder  in the same Folder as the workbook in which this code is
     Let strWB = ThisWorkbook.Path & "\Folder"
    '1c) Hard code instead
    'Let strWB = "F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder"
     Debug.Print "Folder used is" & vbCrLf & strWB & vbCrLf & "" & Right(strWB, (Len(strWB) - InStrRev(strWB, "\", -1, vbTextCompare)))
     Debug.Print
     Let strWB = strWB & "\"
    Rem 2 add last file bit for use in the  Dir(Fullpath&FileName, __ ) , but include wild cards...    http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11089&viewfull=1#post11089 :  _(i) You can use wild cards in the full path and file name string that you give it, so it will look for a file matching your given string. ( So typically you might give a string like “C:\myFolder\*.xl*”, which would look for any excel file: In this bit *.xl* , the first * allows for any file name, and the second * will allow for extensions such as .xls , .xlsm, .xlsx, .. etc… )      _(ii) After you use like Dir(Fullpath&FileName, __ ) once, then any use after of just _ Dir __ without any arguments, will give the next file it finds based on the string you gave in the first use with arguments
    '2a)  Excel files
     Let strWB = strWB & "*.xls*"
    Dim File As String: Let File = Dir(strWB)
     Debug.Print "First got by  Dir(" & strWB & ")" & vbCrLf & "is             " & File
     Debug.Print
        Do '                  '_- I want to keep going in a Loop while I still get a file name returned by  Dir
        Dim Cnt As Long: Let Cnt = Cnt + 1
         Let File = Dir: Debug.Print "Use " & Cnt & " in loop of unargumented   Dir   gives   """ & File & """"
        Loop While File <> "" '_- I want to keep going in a Loop while I still get a file name returned by  Dir
     Debug.Print
     Debug.Print
    End Sub
    Here last routine in form to allow user selection of folder to search for files
    Code:
    Sub DirOrder()  '   http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11093&viewfull=1#post11093
    Dim strWB As String
    Rem 1 get the full string, strWB, for a Folder to use in the  Dir(Fullpath&FileName, __ )        ( strWB=Fullpath&FileName - FileName )
    '1a) use the asking pop up thing, File dialogue folder picker
       With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Folder Select"
        .AllowMultiSelect = False
            If .Show <> -1 Then
             Exit Sub
            Else
            End If
        Let strWB = .SelectedItems(1)  '  & "\"
       End With
    
    '1b) Using a test Folder, named  Folder  in the same Folder as the workbook in which this code is
    'Let strWB = ThisWorkbook.Path & "\Folder"
    '1c) Hard code instead
    'Let strWB = "F:\Excel0202015Jan2016\ExcelForum\wbSheetMakerClsdWbADOMsQueery\Kill Stuff\Folder"
     Debug.Print "Folder used is" & vbCrLf & strWB & vbCrLf & "" & Right(strWB, (Len(strWB) - InStrRev(strWB, "\", -1, vbTextCompare)))
     Debug.Print
     Let strWB = strWB & "\"
    Rem 2 add last file bit for use in the  Dir(Fullpath&FileName, __ ) , but include wild cards...    http://www.excelfox.com/forum/showthread.php/2056-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11089&viewfull=1#post11089 :  _(i) You can use wild cards in the full path and file name string that you give it, so it will look for a file matching your given string. ( So typically you might give a string like “C:\myFolder\*.xl*”, which would look for any excel file: In this bit *.xl* , the first * allows for any file name, and the second * will allow for extensions such as .xls , .xlsm, .xlsx, .. etc… )      _(ii) After you use like Dir(Fullpath&FileName, __ ) once, then any use after of just _ Dir __ without any arguments, will give the next file it finds based on the string you gave in the first use with arguments
    '2a)  Excel files
     Let strWB = strWB & "*"
    Dim File As String: Let File = Dir(strWB)
     Debug.Print "First got by  Dir(" & strWB & ")" & vbCrLf & "is             " & File
     Debug.Print
        Do '                  '_- I want to keep going in a Loop while I still get a file name returned by  Dir
        Dim Cnt As Long: Let Cnt = Cnt + 1
         Let File = Dir: Debug.Print "Use " & Cnt & " in loop of unargumented   Dir   gives   """ & File & """"
        Loop While File <> "" '_- I want to keep going in a Loop while I still get a file name returned by  Dir
     Debug.Print
     Debug.Print
    End Sub

Similar Threads

  1. Testing Concatenating with styles
    By DocAElstein in forum Test Area
    Replies: 2
    Last Post: 12-20-2020, 02:49 AM
  2. testing
    By Jewano in forum Test Area
    Replies: 7
    Last Post: 12-05-2020, 03:31 AM
  3. Replies: 18
    Last Post: 03-17-2019, 06:10 PM
  4. Concatenating your Balls
    By DocAElstein in forum Excel Help
    Replies: 26
    Last Post: 10-13-2014, 02:07 PM
  5. Replies: 1
    Last Post: 12-04-2012, 08:56 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
  •