+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    09-02-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    15

    User input for folder location

    I need help adding this command to my script. Right now my script loops through a folder, opens each workbook, sorts it, saves it, then closes it.

    I would like to add a user input for which folder to perform the loop on.

    Any help would be appreciated.


    Thanks

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    612

    Re: User input for folder location

    Welcome to the forum.

    See the Application.FileDialog property and FileDialog Object in the VBA help for starters. If you are still having problems, check back here.
    Bob
    Tip my scale if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: User input for folder location

    This is my standard "folder picker" method:
    Code:
    Dim fPath as String
    
    'Folder selection
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\"
        End With
    Then use the fPath in the rest of your code.
    Last edited by JBeaucaire; 09-02-2010 at 03:25 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    09-02-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: User input for folder location

    Thanks for the advice. I am new to writing code. Here is my code so far. I think what you told me will work, I just cant get it to integrate with what I already have.


    Any help would be appreciated.


    Thanks again.


    Code:



    Sub RunCodeOnAllXLSFiles()
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    On Error Resume Next
    Set wbCodeBook = ThisWorkbook
    With Application.FileSearch
    .NewSearch
    'Change Path to Appropriate Location

    .LookIn = ("H://Excel/Test")
    .FileType = msoFileTypeExcelWorkbooks
    'Optional filter with wildcard
    .Filename = "*.xls"
    If .Execute > 0 Then 'Workbooks in folder
    For lCount = 1 To .FoundFiles.Count 'Loop through all

    'Open Workbook x and Set a Workbook variable to it
    Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
    'Select Range, sort and save workbook
    Range("A1:F6000").Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range _
    ("B2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
    :=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
    DataOption2:=xlSortNormal
    ActiveWorkbook.Save

    wbResults.Close SaveChanges:=False
    Next lCount
    End If
    End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    End Sub

  5. #5
    Registered User
    Join Date
    09-02-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: User input for folder location

    Sorry, this might be easier to read.
    Attached Images Attached Images

  6. #6
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: User input for folder location

    We have code tags to make the code easier to read!

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  7. #7
    Registered User
    Join Date
    09-02-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: User input for folder location

    Sorry about that. Today was the first time I have posted on here. I think I figured it out anyways.

    Thanks for all your help guys.

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