Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 40

Thread: Notes tests. Excel VBA Folder File Search

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
    _................................................. .........

    Rudi's code from Rem 5R)

    This works differently in a way I have never seen before. There is no need to call a "recursion Routine"

    Initially The main Folder is "put" into a "Queue" ( at the " back " of it )
    https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx
    https://msdn.microsoft.com/de-de/lib...(v=vs.90).aspx
    ( I expect in the "Queue" is just holding the Pointer to the actual Folder Object
    The code the does a similar For Next as in my Code. One major difference is that the first thing it does is at each Folder is to go through every Sub Folder therein and "Put" it at the "back" of the queue. It takes the current folder being "looked" at "out" of the Queue from the "front".

    It effectively then "stacks up all" the Folders in the next level down. Eventually after it goes through every Folder in the current level it will reach the point where it starts on the next level. So effectively it does not go "back and forth" like mine does. Rather it has "looking up" or "back from the front" first all the first level Folders, then all the next level Folders , then all the next, and so on.
    If you look at the difference in the output that I get from mine and Rudi's code, you will soon see the corresponding difference.

    Again the tricky bit for me was to get a Variable to indicate the "level" or "column to the left".

    What I do in this case is count every time a Sub Folder is put in the back of the Queue.
    NxtLvlCnt
    This will finally give an indication of the Number of Sub folders at the next level.
    I have second count variable
    CurrentLvlCnt
    Which is originally set to the last level count ( set initially to one for the original main folder ), which is successively decreased each time a Folder is "taken out" of the queue. When it reaches zero it is an indication that we have reached the next series of Next level Sub Folders. When that occurs it is given the value of the next level Count, and the next level Cont is then reset to Zero.

    _................................................. .......

    For both code I finally added a bit of Error handling. I did this as when I tested with many real files , I often had an error if , for example the "doing stuff" involved opening a file. If this happens you are told what error occurred and to which file, then you go on to the next. ( I assume that errors do not occur in the original code that just Prints out the "explorer type" Listing. If it did I expect the output could go a bit out if step !! ) )

    _...............................................

    So I give here some typical output from a run of both codes. To make it a bit easier I include the example set of Folders I used. ( I hope they all come up. By me only a few Folders come up, although the are "indicted as all there " ? ? )
    https://app.box.com/s/onj6ntvwkxbo1088x7e0tca2gst45hnq
    ( Edit : Here is another Folder to try https://app.box.com/s/9e6xnb65fijjhl7bk0q6gzzriihkzibw )


    words I have a main
    EileensFldr

    That has three sub folders in it. Therein are files and further sub Folders and files etc…. as seen in the listing the Code gives.

    Initially the code is set to run from a file in the same directory as folder EileensFldr
    Last edited by DocAElstein; 01-23-2020 at 02:44 PM.
    ….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
    Output Given from my code

    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    1
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr EileensFldr
    2
    3
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_1 Fldr1_1
    4
    File1_1a.xlsx
    5
    File1_1b.xlsx
    6
    7
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2 Fldr1_2
    8
    File1_2a.xlsx
    9
    File1_2b.xlsx
    10
    File1_2c.xlsx
    11
    12
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1 Fldr1_2_1
    13
    File1_2_1a.xlsx
    14
    File1_2_1b.xlsx
    15
    16
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_1 Fldr1_2_1_1
    17
    File1_2_1_1a.xlsx
    18
    19
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_2 Fldr1_2_1_2
    20
    File1_2_1_2a.xlsx
    21
    File1_2_1_2b.xlsx
    22
    23
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3 Fldr1_3
    24
    25
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1 Fldr1_3_1
    26
    27
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1\Flsr1_3_1_1 Flsr1_3_1_1
    28
    29
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1\Flsr1_3_1_1\Fldr1_3_1_1 _1 Fldr1_3_1_1_1
    EFFldr
    Last edited by DocAElstein; 01-23-2020 at 02:45 PM.
    ….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!!

  3. #3
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Correspondoing Output given by Rudi's code

    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    1
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr EileensFldr
    2
    3
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_1 Fldr1_1
    4
    File1_1a.xlsx
    5
    File1_1b.xlsx
    6
    7
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2 Fldr1_2
    8
    File1_2a.xlsx
    9
    File1_2b.xlsx
    10
    File1_2c.xlsx
    11
    12
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3 Fldr1_3
    13
    14
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1 Fldr1_2_1
    15
    File1_2_1a.xlsx
    16
    File1_2_1b.xlsx
    17
    18
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1 Fldr1_3_1
    19
    20
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_1 Fldr1_2_1_1
    21
    File1_2_1_1a.xlsx
    22
    23
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_2 Fldr1_2_1_2
    24
    File1_2_1_2a.xlsx
    25
    File1_2_1_2b.xlsx
    26
    27
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1\Flsr1_3_1_1 Flsr1_3_1_1
    28
    29
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\Eil eensFldr\Fldr1_3\Fldr1_3_1\Flsr1_3_1_1\Fldr1_3_1_1 _1 Fldr1_3_1_1_1
    EFFldr
    Last edited by DocAElstein; 01-23-2020 at 02:58 PM.
    ….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!!

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Codes:
    Rudi's Code
    Sub ReplaceInAllSubFoldersRudisQing()

    '' http://www.excelforum.com/excel-prog...ubfolders.html



    Code:
    '  Rudi     http://www.eileenslounge.com/viewtopic.php?f=27&t=22499
    Sub ReplaceInAllSubFoldersQing()
    Rem 1Q) Some Worksheets and General Variables Info
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets.Item(1) 'Worksheets("RudyMSRQueue") 'CHANGE TO SUIT YOUR WORKSHEET
    Dim strDefpath As String: Let strDefpath = ThisWorkbook.Path ' Any Path to Folder to test this code! here we simply use the Path where the File with this code in is
    Dim strDefFldr As String: Let strDefFldr = "EileensFldr" 'Just for an initial suggestion
    Rem 2Q) Get Folder Info ( Using VBA Application.FileDialog(msoFileDialogFolderPicker) Property )
    Dim strWB As String
      With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Folder Select "
        .AllowMultiSelect = False
            If .Show <> -1 Then
            Exit Sub
            End If
        Let strWB = .SelectedItems(1) & "\"
      End With
    
    Rem 3Q) Microsoft Scripting Runtime Library
    'Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")'Late Binding
    Dim FSO As Scripting.FileSystemObject 'Early Binding alternative  activate a reference to the Microsoft Scripting Runtime Library in the Tools > References menu of VBE.
    Set FSO = New Scripting.FileSystemObject
    
    Rem 4Q)'Some variables for Positon of Things
    Dim rCnt As Long, clmLvl As Long: Let clmLvl = 1: Let rCnt = -1 'rowCount is genaraly increase for a new entry, Column "level" is intended to give an indication of how far down ( to he right ) you are in the Folder chain. Ste to 1 for the first mainn Initial Folder.
    Dim CurrentLvlCnt As Long: CurrentLvlCnt = 1 'Count of the Number of Folders in the Folder level currently beig run through.
    Dim NxtLvlCnt As Long 'Count of the Number of Folders in the next level
    Dim queue As Collection
    Set queue = New Collection
    queue.Add FSO.GetFolder(strWB) 'Main Folder Put at position 1 of Queue'''''
    Dim celTL As Range: Set celTL = ws.Range("A1") 'Top left of where Listing should go
    'Application.ScreenUpdating = False
    
    Rem 5Q) Main loop. Do While Queue is not Empty effectivelly goes through all Folders
    Dim oFile As Variant, oFolder As Variant, oSubfolder As Variant '                                                                 Can also be variant Types or Objects. - Must be for Late Binding
        Do While queue.Count > 0 'Main Loop. Does as many times as there are things ( Folders here ) stacked in the Queue========
        Set oFolder = queue(1) 'Next Folder .... effectively
        queue.Remove 1 'de-queue'......"taken" from start of Queue. ( Actually it is assigned to a variable, then removed from the Queue, which probably just has the Pointer to it.
        CurrentLvlCnt = CurrentLvlCnt - 1 'de-the count for numbers in in this current Folder level
        ''''''''Doing Stuff For the Folder
        rCnt = rCnt + 2 'Move on a line and a spare Line for every Folder Entry
        celTL.Cells(rCnt, 1).Value = oFolder.Path: celTL.Cells(rCnt, clmLvl).Offset(0, 1).Value = oFolder.Name 'Cell poroperty of Top Left Cell Range Object uset to position output.
        ''''''''End Doing Stuff for each Folder
        '5Qa) Add any Sub Folders from current Folder at end of queue
            For Each oSubfolder In oFolder.SubFolders 'For as many ( if any ) Sub Folders In the Current Folder
            queue.Add oSubfolder 'en-queue.. add the Sub Folder on at the end of the Queue
            NxtLvlCnt = NxtLvlCnt + 1 'en-the count of the Folders in the next Level..Increase our count of the Folders in the Next folder level
            Next oSubfolder
            '5b) Doing Stuff for every file in current folder
                For Each oFile In oFolder.Files
                '''''''Doing Stuff for Each File here
                    If InStr(1, oFile.Name, ".xls") > 0 Then 'Option to select only if .xls ( or .xlsx or .xlsm ) type files
                    rCnt = rCnt + 1
                    celTL.Cells(rCnt, clmLvl).Offset(0, 1).Value = oFile.Name
                    On Error GoTo ErrHdlr 'In case problem opening file for example
                    'Set wbk = Workbooks.Open(oFile)
                    'wbk.Close SaveChanges:=True
                    Else: End If
                '''''''End Doing Stuff for Each File
    NxtoFile:   Next oFile ' Spring Point after error handler so as to go on to next File after the File action that errored
            '5Qc) should we have reached the end of the current level of Folders, we reset the level Column for output, and make the new Current Folders in Folder level Count equel to the next one, as we go ion now to Folders from the next level.
            If CurrentLvlCnt = 0 Then
            clmLvl = clmLvl + 1 'Set column position 1 to the left "down" the Folder Level Chain.
            Let CurrentLvlCnt = NxtLvlCnt 'So the current Folder Level count of Folders becomes that last counted.
            NxtLvlCnt = 0 'Next level of Folders currently are not in the Queue. This will be re counted for the next Folders as Sub Folders are added to the back of the Queue
            Else
            End If
            
        Loop 'queue.Count > 0 main loop for all Folders=====================================================================
    Application.ScreenUpdating = True
    MsgBox "All Excel Files processed", vbInformation
    ws.Columns("A:H").AutoFit
    Exit Sub 'Normal End for no Erriors
    Rem 6) 'Error handler section just put here for convenience
    ErrHdlr: 'Hopefully we know why we are here, and after informing can continue ( to next file )
    MsgBox prompt:="Error " & Err.Description & " with File " & oFile & ""
    On Error GoTo -1 'This needs to be done to reset the VBA exceptional error state of being. Otherwise VBA "thinks"" Errors are being handeled and will not respond again to the Error handler.
    On Error GoTo 0 ' Swiches off the current error handler. I do not really need to do this. But it is good practice so the error handler is only in place at the point where i next am expecting an error
    GoTo NxtoFile
    End Sub
    Last edited by DocAElstein; 01-23-2020 at 02:57 PM.
    ….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!!

  5. #5
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    My codes ( again in color!! )
    Main Code
    Sub DoStuffInFoldersInFolderRecursion()
    And called Routine
    Sub LoopThroughEachFolderAndItsFile(

    Code:
    Option Explicit
    '
    'http://excelpoweruser.blogspot.de/2012/04/looping-through-folders-and-files-in.html     http://www.excelforum.com/excel-programming-vba-macros/1126751-get-value-function-loop-through-all-files-in-folder-and-its-subfolders.html#post4316662    http://www.excelfox.com/forum/f5/loop-through-files-in-a-folder-using-vba-1324/
    Sub DoStuffInFoldersInFolderRecursion() 'Main Procedure to call the Function  LoopThroughEachFolderAndItsFile(
    Rem 1A) Some Worksheets and General Variables Info
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets.Item(1) 'Worksheets("EFFldr") 'CHANGE TO SUIT YOUR WORKSHEET
    Rem 2A) Get Folder Info ( Using VBA Application.FileDialog(msoFileDialogFolderPicker) Property )
    Dim strWB As String
      With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Folder Select "
        .AllowMultiSelect = False
            If .Show <> -1 Then
            Exit Sub
            End If
        Let strWB = .SelectedItems(1) & "\"
      End With
    
    Rem 3A )
    Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject") 'Late Binding
    'Dim FSO As Scripting.FileSystemObject 'Early Binding alternative  activate a reference to the Microsoft Scripting Runtime Library ( MSRL ) in the Tools References menu of VB Editor Options.
    'Set FSO = New Scripting.FileSystemObject 'Create an Instance of the Class Scripting FileSystemObject
    Dim myFolder As Object 'An Object from myFolder, can be an declared as Dim myFolder As Folder also for Early Binding
    Set myFolder = FSO.GetFolder(strWB) 'Set the selected Folder to the Object Folder using this Method which takes as arbument the Full String Path
    
    Rem 4A )
    Dim rCnt As Long, CopyNumber As Long: Let rCnt = 1: Let CopyNumber = 1 '"Run progressin ( "down vertical" ) axis ( Row count for output ), "Down Folder chain to the right", The Count of the Copy of the called Procedue
    Dim celTL As Range: Set celTL = ws.Range("A1") 'Top left of where Licting should go
    celTL.Value = myFolder.Path: celTL.Offset(0, 1).Value = myFolder.Name: ws.Columns("A:C").AutoFit 'First output Row
    Call LoopThroughEachFolderAndItsFile(myFolder, celTL, rCnt, CopyNumber) 'Up until now we just got the initial Folder. Now we go to all sub folders  then all subfolders   then all subfolders.......
    Application.ScreenUpdating = True
    MsgBox "All Excel Files processed", vbInformation
    ws.Columns("A:H").AutoFit
    End Sub
    Rem 5A)
    Sub LoopThroughEachFolderAndItsFile(ByVal fldFldr As Object, ByRef celTL As Range, ByRef rCnt As Long, ByVal CopyNumberFroNxtLvl As Long)  'In below function we have a nested loop to iterate each files also
    Dim myFldrs As Object ''This is used continuously as the "steering" thing, that is to say each Sub Folder in Folder loops, in loops, in loops......etc   ....can be Dim myFldrs As Folder for early bindingDim CopyNumber As Long 'equivalent to clmLvl in Rudis Q code
    Dim CopyNumber As Long 'equivalent to clmLvl in Rudis Q code
    If CopyNumber = 0 Then CopyNumber = CopyNumberFroNxtLvl 'If this variable in this Copy of the Routine has not been set then we have reached the next Copy for the First time, so set the variable so we have an indication ( number to the right or "down" Folder Chain
        '5Ab) Doing stuff for current Folder
        For Each myFldrs In fldFldr.SubFolders 'SubFolders collection used to get at all Sub Folders
        ''''''''Doing stuff for each Folder
        Let rCnt = rCnt + 1 + 1 ''At each folder we always move down a line, and a dd amm extra line  as a space between Folders ( The indication of the "column" or "down" to the right comes from the Copy Number of the Sub Procedure
        celTL.Cells(rCnt, 1).Value = myFldrs.Path: celTL.Cells(rCnt, CopyNumber).Offset(0, 2).Value = myFldrs.Name:   'Print out current Folder Path and Name in next free row.
        ''''''''End doing stuff for each Folder
        '5Ac) Doing stuff for current file.
        Dim oFile As Object '  ... for early binding can Dim oFile As file
                For Each oFile In myFldrs.Files 'Looking at all Files types initially '#####
                ''''''''Doing Stuff for Each File
        '            Dim Extension As String: Let Extension = Right(oFile.Name, (Len(oFile.Name) - (InStrRev(oFile.Name, ".")))) 'To get the bit just after the . dot.
        '                If Left(Extension, 3) = "xls" Then 'Check for your required File Type    #####
                    Let rCnt = rCnt + 1
                    celTL.Cells(rCnt, CopyNumber).Offset(0, 2).Value = oFile.Name ' Do your stuff here
        '                Dim wkb As Workbook
                         On Error GoTo ErrHdlr 'In case problem opening file for example
        '                Set wkb = Workbooks.Open(oFile)
        '                wkb.Close SaveChanges:=True
        '                Else 'Do not do stuff for a Bad Extension
        '                End If
                ''''''''End Doing Sttuff for Each File
    NxtoFile:   Next oFile ' Spring Point after error handler so as to go on to next File after the File action that errored
        Call LoopThroughEachFolderAndItsFile(myFldrs, celTL, rCnt, CopyNumber + 1) 'This is an example of recursion. It is actually very simple once you understand it. But it is just incredibly difficult to put in words. It is basically a Procedure that keeps calling itself as much as necessary as it goes "along",  "down", or "to the right" of the Path "roots". Every time it goes off calling itself VBA runs a copy of that Procedure. It "Stacks" all info carefully for each "Copy" Run and continues to do this "drilling" down as far as it must, in this case finding the Next Folder, and then the next Folder in that, then the next Folder in that, then the next Folder in that...I think you get the point! Each time VBA makes a copy of the Routine and you go into that. The calling Routine then "freezes at its current state and all variable keep there values. The "Frozen" Routine then re starts when the copy finishes
        Next
    Exit Sub 'Normal End for no Errors
    Rem 6 ) Error handler section just put here for convenience
    ErrHdlr: 'Hopefully we know why we are here, and after informing can continue ( to next file )
    MsgBox prompt:="Error " & Err.Description & " with File " & oFile & ""
    On Error GoTo -1 'This needs to be done to reset the VBA exceptional error state of being. Otherwise VBA "thinks" Errors are being handeled and will not respond again to the Error handler.
    On Error GoTo 0 ' Swiches off the current error handler. I do not really need to do this. But it is good practice so the error handler is only in place at the point where i next am expecting an error
    GoTo NxtoFile
    End Sub
    Last edited by DocAElstein; 01-23-2020 at 02:56 PM.
    ….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. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Here is another Folder to try
    https://app.box.com/s/9e6xnb65fijjhl7bk0q6gzzriihkzibw

    Results from that for my Code:

    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    1
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1 EFldr1_1
    2
    3
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_1 EFldr1_1_1
    4
    File1_1_1a.xlsx
    5
    File1_1_2b.xlsx
    6
    7
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2 EFldr1_1_2
    8
    File1_1_2a.xlsx
    9
    File1_1_2b.xlsx
    10
    11
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1 Fldr1_1_2_1
    12
    File1_1_2_1a.xlsx
    13
    File1_1_2_1b.xlsx
    14
    15
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1\Fldr1_1_2_1_1 Fldr1_1_2_1_1
    16
    File1_1_2_1_1a.xlsx
    17
    18
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1\Fldr1_1_2_1_2 Fldr1_1_2_1_2
    19
    File1_1_2_1_2a.xlsx
    20
    File1_1_2_1_2b.xlsx
    21
    22
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3 EFldr1_1_3
    23
    24
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3\Fldr1_1_3_1 Fldr1_1_3_1
    25
    26
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3\Fldr1_1_3_1\Flsr1_1_3_1_1 Flsr1_1_3_1_1
    27
    File1_1_3_1_1a.xlsx
    28
    EFFldr
    Last edited by DocAElstein; 01-23-2020 at 02:52 PM.
    ….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!!

  7. #7
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Results using this main Folder
    https://app.box.com/s/9e6xnb65fijjhl7bk0q6gzzriihkzibw
    using Rudi's code:


    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    1
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1 EFldr1_1
    2
    3
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_1 EFldr1_1_1
    4
    File1_1_1a.xlsx
    5
    File1_1_2b.xlsx
    6
    7
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2 EFldr1_1_2
    8
    File1_1_2a.xlsx
    9
    File1_1_2b.xlsx
    10
    11
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3 EFldr1_1_3
    12
    13
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1 Fldr1_1_2_1
    14
    File1_1_2_1a.xlsx
    15
    File1_1_2_1b.xlsx
    16
    17
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3\Fldr1_1_3_1 Fldr1_1_3_1
    18
    19
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1\Fldr1_1_2_1_1 Fldr1_1_2_1_1
    20
    File1_1_2_1_1a.xlsx
    21
    22
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_2\Fldr1_1_2_1\Fldr1_1_2_1_2 Fldr1_1_2_1_2
    23
    File1_1_2_1_2a.xlsx
    24
    File1_1_2_1_2b.xlsx
    25
    26
    H:\Excel0202015Jan2016\ExcelForum\wbSheetMaker\EFl dr1_1\EFldr1_1_3\Fldr1_1_3_1\Flsr1_1_3_1_1 Flsr1_1_3_1_1
    27
    File1_1_3_1_1a.xlsx
    28
    EFFldr
    Last edited by DocAElstein; 01-23-2020 at 02:53 PM.
    ….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!!

  8. #8
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Second Code for nelson
    Post 9
    http://www.excelfox.com/forum/showth...0070#post10070





    [Code]Sub IJAdjustKAddTotalAllWorksheet() ' http://www.excelfox.com/forum/showthread.php/2144-Code-Required-to-calculate-number-of-days-worked-normal-overtime-and-holiday-overtime?p=10060#post10060
    Rem 1) Workbooks Info.
    Dim Wb As Workbook ' Dim: For Object variabls: Address location to a "pointer". That has all the actual memory locations (addresses) of the various property values , and it holds all the instructions what / how to change them , should that be wanted later. That helped explain what occurs when passing an Object to a Call ed Fucntion or Sub Routine By Val ue. In such an occurance, VBA actually passes a copy of the pointer. So that has the effect of when you change things like properties on the local variable , then the changes are reflected in changes in the original object. (The copy pointer instructs how to change those values, at the actual address held in that pointer). That would normally be the sort of thing you would expect from passing by Ref erence. But as that copy pointer "dies" after the called routine ends, then any changes to the Addresses of the Object Properties in the local variable will not be reflected in the original pointer. So you cannot actually change the pointer.)
    Set Wb = ActiveWorkbook ' Set: Fill or partially Fill: Setting to a Class will involve the use of an extra New at this code line. I will then have an Object referred to as an instance of a Class. At this point I include information on my Pointer Pigeon hole for a distinct distinguishable usage of an Object of the Class. For the case of something such as a Workbook this instancing has already been done, and in addition some values are filled in specific memory locations which are also held as part of the information in the Pigeon Hole Pointer. We will have a different Pointer for each instance. In most excel versions we already have a few instances of Worksheets. Such instances Objects can be further used., - For this a Dim to the class will be necessary, but the New must be omitted at Set. I can assign as many variables that I wish to the same existing instance
    Dim wsStear As Worksheet ' Used for each Worksheet counting Tabs from left from 1 To Total
    Rem 2) varables for some totals
    Const TDays As Long = 30 'Total days just taken as 30 ' Long is very simple to handle, - final memory "size" type is known (123.456 and 000.001 have same "size" computer memory ) , and so a Address suggestion can be given for the next line when the variable is filled in. '( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. )
    Dim Dte As Date, DteNo As Long ' I am hoping Dte will sort out getting a date in a format that I can use the Weekday function to see what week day it is and get that as a nuumber to check for..
    Rem 3) Loop through worksheets and give some Totals
    Dim Cnt As Long ' Loop Bound variable count for going through all worksheets
    '3a) main Loop start============================================= ========
    For Cnt = 1 To Wb.Worksheets.Count ' The Worksheets collection Object Property returns the number of worksheet items in the Workbook
    Set wsStear = Wb.Worksheets.Item(Cnt) ' At each loop the variable is set to the current Worksheet counting from the Cnt'ths tab from left
    Dim lr As Long ' Used for last row number in column E
    Let lr = wsStear.Range("E" & Rows.Count & "").End(xlUp).Row ' The Range Object ( cell ) that is the last cell in the column of interest (CHOOSE a column typically that will always have a last Entry in any Data) ,( Row Number given by .Count Property applied to ( any Worksheet would do, so leaving unqualified is OK here, ) Spreadsheet Range Rows Property) has the Property .End ( argument "Looking back up" ) appled to it. This Returns a new Range ( cell ) object which is that of the first Range ( cell ) with something in it "looking back up" in the XL spreadsheet from that last Cell. Then the .Row Property is applied to return a long number equal to the Row number of that cell: Rows.Count is the very last row number in your Worksheet. It is different for earlier versions of Excel. The End(xlUp) is the same as pressing a Ctrl+UpArrow key combination. The final ".Row" returns the row where the cursor stops after moving up.
    Dim FstDtaCel As Range: Set FstDtaCel = wsStear.Range("A1") ' Worksheets Range(" ") Property used to return Range object of first cell in second row
    '3b) Data arrays from worksheet. We need columns E H I J .... Date ( Column E ) and Total hrs ( Column H ) are required to use in calculations
    Dim arrInNorm() As Variant, arrInOver() As Variant ' In the next lines the .Value2 or .value Property is applied a Range object which presents the Value or Value2 value or values in a single variable of appropriate type or a field of member Elements of varaint types.We are expecting the latter, so declare ( Dim ) a dynamic Array variable appropriately. It must be dynamic as its size will be defined at that assignment
    Let arrInNorm() = FstDtaCel.Offset(0, 8).Resize(lr, 1).Value2 ' I ' Normal Hrs ( Column I ) are needed as they must be set to zero for Holy ?? Holidays ?? Friday ??
    Let arrInOver() = FstDtaCel.Offset(0, 9).Resize(lr, 1).Value2 ' J ' Overtime ( Column J ) is needed as it will be changed and then used in calculations
    Dim arrTotHrs() As Variant ' ,' ## ' arrDteClr() As Variant
    Let arrTotHrs() = FstDtaCel.Offset(0, 7).Resize(lr, 1).Value [color=darkgreen]' H ' ' One thing you pick up when learning VBA programming is that referring to cells from one to another via an offset is both fundamental and efficient. That makes sense as Excel is all about using the offsets mentioned above. So like if you use them you can "cut out the middle man". ( The middle man here might be considered as, for example, in VBA, using extra variables for different Range objects: A fundamental thing to do with any cell ( or strictly speaking the Range object associated to a cell ) is the Range Item Property of any range Object, through which you can "get at" any other Range object. http://www.excelforum.com/showthread.php?t=1154829&page=13&p=4563838&highlight=#post4563838 ( It is often quicker than using a separate variable for each Range object
    Last edited by DocAElstein; 01-23-2020 at 02:53 PM.
    ….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. #9
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    In support of this Thread Question
    https://excel.tips.net/T008233_Findi...e_Desktop.html
    https://excel.tips.net/T008233_Finding_the_Path_to_the_Desktop.html




    …………………… I tried this one
    sPath = Environ("USERPROFILE") & "\Desktop"

    It didn't work for me, because I had moved my desktop to another location.
    Is there a way to find the 'actual' placement?....
    ...................................


    [FONT=Arial][size=3] [color="#3E0000"]

    Demo Example

    Download both files. Put both in the same folder. ( Unzip the zipped file
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by DocAElstein; 01-31-2020 at 11:54 PM.
    ….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!!

  10. #10
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    _____ Workbook: wbCodes.xlsm ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    1
    2
    3
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr EileensFldr
    4
    5
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_1 Fldr1_1
    6
    File1_1a.xlsx
    7
    File1_1b.xlsx
    8
    9
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_1\Fldr1_1_1 Fldr1_1_1
    10
    11
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_2 Fldr1_2
    12
    File1_2a.xlsx
    13
    File1_2b.xlsx
    14
    File1_2c.xlsx
    15
    16
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_2\Fldr1_2_1 Fldr1_2_1
    17
    File1_2_1a.xlsx
    18
    File1_2_1b.xlsx
    19
    20
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_1 Fldr1_2_1_1
    21
    File1_2_1_1a.xlsx
    22
    23
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_2 Fldr1_2_1_2
    24
    File1_2_1_2a.xlsx
    25
    File1_2_1_2b.xlsx
    26
    27
    F:\Excel0202015Jan2016\OffenFragensForums\AllenWya tt\EileensFldr\Fldr1_2\Fldr1_2_1\Fldr1_2_1_2\Deskt op Desktop
    28
    Worksheet: Sheet1
    Last edited by DocAElstein; 01-23-2020 at 04:54 PM.
    ….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!!

Similar Threads

  1. Tests and Notes on Range Referrencing
    By DocAElstein in forum Test Area
    Replies: 70
    Last Post: 02-20-2024, 01:54 AM
  2. Tests and Notes for EMail Threads
    By DocAElstein in forum Test Area
    Replies: 29
    Last Post: 11-15-2022, 04:39 PM
  3. Replies: 1
    Last Post: 02-14-2013, 12:09 PM
  4. List File name in folder to excel with images
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 01-15-2013, 11:37 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
  •