+ Reply to Thread
Results 1 to 3 of 3

Modify existing code to select file instead of select folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Modify existing code to select file instead of select folder

    Hi All,

    I have some code i'm using for another macro but trying to modify it for a similar different macro.

    The below macro asked the user to select a folder which contained multiple project files and then consolidate data within all those files.

    I now need the code to allow the user to select the specific file rather than a group of files in a folder.


    Sub CONSOLIDATE()
    
    Dim Path As String          ' Set parameters
    Dim FileName As String
    Dim MyArray(3) As Variant
    Dim Sh As Worksheet
    Dim i As Integer
    Const MainSh As String = "Projects"
    Dim LR As Long
    
    With Application.FileDialog(msoFileDialogFolderPicker) ' Select folder where project files are saved
        .ButtonName = "OK"
        .Title = "Select folder containing files to consolidate"
        .AllowMultiSelect = False
        .Show
        On Error Resume Next
        Path = .SelectedItems(1)
        Err.Clear
        On Error GoTo 0
    End With
    
    If Path = Empty Then MsgBox "Macro cancelled.": Exit Sub       ' If message box is cancelled exit macro
    
    If Right(Path, 1) <> "\" Then Path = Path & "\"
    
    FileName = Dir(Path & "*.xlsx")
    Do While FileName <> ""
        Workbooks.Open FileName:=Path & FileName, ReadOnly:=True
        For Each Sh In ActiveWorkbook.Worksheets
        If Sh.Range("A3").Value = "Project Title" Then               'If cell A3 contains the text "Project Title" then copy data from that sheet
            MyArray(0) = Sh.Range("F4").Value
            MyArray(1) = Sh.Range("C3").Value
            MyArray(2) = Sh.Range("F3").Value
            
            LR = ThisWorkbook.Sheets(MainSh).Range("A" & Rows.Count).End(xlUp).Row + 1 ' calculate the last row of the data range
            For i = 0 To 3     ' Number of columns in the range
                ThisWorkbook.Sheets(MainSh).Cells(LR, i + 1).Value = MyArray(i)
            Next i
            
        ThisWorkbook.Sheets(MainSh).Hyperlinks.Add _
        Anchor:=ThisWorkbook.Sheets(MainSh).Range("A" & LR), Address:=Path & FileName    ' Add hyperlinks to column A to link to project files
    
        End If
        Next Sh
        ActiveWorkbook.Close False
        FileName = Dir()
    Loop
    
    End Sub
    Last edited by Gti182; 04-04-2014 at 09:42 AM.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Modify existing code to select file instead of select folder

    Instead of the folderpicker stuff:
    With Application.FileDialog(msoFileDialogFolderPicker) ' Select folder where project files are saved
        .ButtonName = "OK"
        .Title = "Select folder containing files to consolidate"
        .AllowMultiSelect = False
        .Show
        On Error Resume Next
        Path = .SelectedItems(1)
        Err.Clear
        On Error GoTo
    you can use:
    path = application.getopenfilename()

  3. #3
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: Modify existing code to select file instead of select folder

    thats great thanks!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro asking to select the folder and open file.
    By lalaarif1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-04-2013, 04:03 PM
  2. [SOLVED] Add Code to select a folder to save in
    By afpPaul in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-18-2012, 08:09 PM
  3. [SOLVED] Modify this code to select different column
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-17-2012, 11:52 AM
  4. let user select input file, modify, save
    By alanders in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-25-2010, 06:37 PM
  5. vb code to select all files from a folder without manual intervention
    By ravivaliya in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 12-08-2009, 04:12 AM

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.6.0 RC 1