+ Reply to Thread
Results 1 to 10 of 10

Thread: macro to select path to collect data from

  1. #1
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    macro to select path to collect data from

    Hi , i have a macro , done with the help of this forum that colects data from seeveral files in one folder that is hard coded in the macro, what i would like now is to have the possibility to select the working folder to work
    attatched the files i have now

    The macro i have is

    
    Sub CollectInfo()
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/21/2010
    'Summary:   Collect specific data from all workbooks in a single folder
    Dim fPath As String:        fPath = "C:\2010\Test\"      'where files are found
    Dim fName As String
    Dim wbData As Workbook
    Dim wsDest As Worksheet:    Set wsDest = ThisWorkbook.Sheets("stock")
    Dim NR As Long:             NR = wsDest.Range("B" & Rows.Count).End(xlUp).Row + 1
    Dim LR As Long
    
    Application.ScreenUpdating = False      'speed up macro
    fName = Dir(fPath & "*.xls")            'filter for files to open
    
    Do While Len(fName) > 0
        Set wbData = Workbooks.Open(fPath & fName)  'open found file
        With wbData.Sheets("Resumo")
            .Rows(10).AutoFilter
            .Rows(10).AutoFilter Field:=6, Criteria1:=">0.5"
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            If LR > 10 Then
                wsDest.Range("A" & NR).Value = .[A5]
                wsDest.Range("E" & NR).Value = .[D2]
                .Range("A11:A" & LR & ",F11:F" & LR & ",K11:K" & LR).Copy
                wsDest.Range("B" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                wsDest.Range("F" & NR).Value = .[C*E]
                
            End If
        End With
        wbData.Close False
        NR = Range("B" & Rows.Count).End(xlUp).Row + 1
        fName = Dir
    Loop
    
    LR = Range("B" & Rows.Count).End(xlUp).Row
    With Range("A1:E" & LR)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
    
    Run [teorico()]
    Run [real()]
    
    Columns.AutoFit
    Application.ScreenUpdating = True
    
    
    
    End Sub
    Attached Files Attached Files
    Last edited by clixo; 10-26-2010 at 11:44 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: macro to select path to collect data from

    You can use this code to prompt someone to select a folder:

        Dim folderName As String
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\"
            .Show
            If .SelectedItems.Count > 0 Then
                folderName = .SelectedItems(1)
            Else
                MsgBox "Folder selection cancelled", vbInformation, Title:="Process Cancelled"
                Exit Sub
            End If
        End With

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    Re: macro to select path to collect data from

    Thanks Dom, but how can i join the code you sent to my macro ? ... i am really a noob in vba .....

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: macro to select path to collect data from

    Not tested but think this should do it:

    Sub CollectInfo()
    
    Dim fPath As String
    Dim fName As String
    Dim wbData As Workbook
    Dim wsDest As Worksheet:    Set wsDest = ThisWorkbook.Sheets("stock")
    Dim NR As Long:             NR = wsDest.Range("B" & Rows.Count).End(xlUp).Row + 1
    Dim LR As Long
       
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "C:\"
        .Show
        If .SelectedItems.Count > 0 Then
            fPath = .SelectedItems(1)
        Else
            MsgBox "Folder selection cancelled", vbInformation, Title:="Process Cancelled"
            Exit Sub
        End If
    End With
    
    Application.ScreenUpdating = False      'speed up macro
    fName = Dir(fPath & "*.xls")            'filter for files to open
    
    Do While Len(fName) > 0
        Set wbData = Workbooks.Open(fPath & fName)  'open found file
        With wbData.Sheets("Resumo")
            .Rows(10).AutoFilter
            .Rows(10).AutoFilter Field:=6, Criteria1:=">0.5"
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            If LR > 10 Then
                wsDest.Range("A" & NR).Value = .[A5]
                wsDest.Range("E" & NR).Value = .[D2]
                .Range("A11:A" & LR & ",F11:F" & LR & ",K11:K" & LR).Copy
                wsDest.Range("B" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                wsDest.Range("F" & NR).Value = .[C*E]
                
            End If
        End With
        wbData.Close False
        NR = Range("B" & Rows.Count).End(xlUp).Row + 1
        fName = Dir
    Loop
    
    LR = Range("B" & Rows.Count).End(xlUp).Row
    With Range("A1:E" & LR)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
    
    Run [teorico()]
    Run [real()]
    
    Columns.AutoFit
    Application.ScreenUpdating = True
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    Re: macro to select path to collect data from

    Thanks Dom , i tested and it did not worked... seams not be opening the folder i choose.

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: macro to select path to collect data from

    Small but important correction to this line:

    fName = Dir(fPath & "\*.xls")            'filter for files to open

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    Re: macro to select path to collect data from

    Hi again .. its almost there , the sub folders are not stored in the path, i. e if i select c:\2010\test\*.xls, the macro gets c:\2010\*.xls .

  8. #8
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: macro to select path to collect data from

    It should either select the displayed folder or if you highlight one include that.

    Noticed this will need changing as well though:

        Set wbData = Workbooks.Open(fPath & "\" & fName)  'open found file

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  9. #9
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    Re: macro to select path to collect data from

    Now it works !!!
    Thanks !

  10. #10
    Registered User
    Join Date
    09-14-2004
    Posts
    14

    Re: macro to select path to collect data from

    this is how it looks at the end :

    
    Sub CollectInfoFinal()
    
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/21/2010
    'Summary:   Collect specific data from all workbooks in a single folder
    
    Dim fPath As String
    Dim fName As String
    Dim wbData As Workbook
    Dim wsDest As Worksheet:    Set wsDest = ThisWorkbook.Sheets("stock")
    Dim NR As Long:             NR = wsDest.Range("B" & Rows.Count).End(xlUp).Row + 1
    Dim LR As Long
       
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "C:\"
        .Show
        If .SelectedItems.Count > 0 Then
            fPath = .SelectedItems(1)
        Else
            MsgBox "Folder selection cancelled", vbInformation, Title:="Process Cancelled"
            Exit Sub
        End If
    End With
    
    Application.ScreenUpdating = False      'speed up macro
    fName = Dir(fPath & "\*.xls")            'filter for files to open
    
    Do While Len(fName) > 0
        Set wbData = Workbooks.Open(fPath & "\" & fName)  'open found file
        With wbData.Sheets("Resumo")
            .Rows(10).AutoFilter
            .Rows(10).AutoFilter Field:=6, Criteria1:=">0.5"
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            If LR > 10 Then
                wsDest.Range("A" & NR).Value = .[A5]
                wsDest.Range("E" & NR).Value = .[D2]
                .Range("A11:A" & LR & ",F11:F" & LR & ",K11:K" & LR).Copy
                wsDest.Range("B" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                wsDest.Range("F" & NR).Value = .[C*E]
                
            End If
        End With
        wbData.Close False
        NR = Range("B" & Rows.Count).End(xlUp).Row + 1
        fName = Dir
    Loop
    
    LR = Range("B" & Rows.Count).End(xlUp).Row
    With Range("A1:E" & LR)
        .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
    
    Run [teorico()]
    Run [real()]
    
    Columns.AutoFit
    Application.ScreenUpdating = True
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0