0

This code is opening two excel files, takes from each one the sum of a column and then put them in two cells in the original file. My code opens the files as they are already defined, but I want the script to ask me what files I want to choose.

Workbooks.Open Filename:="G:\Users\K.os\Desktop\roxana\m.xls"
Windows("roxana.xlsm").Activate
Workbooks.Open Filename:="G:\Users\K.os\Desktop\roxana\p.xls"
Windows("roxana.xlsm").Activate
Range("A4").Select
ActiveCell.FormulaR1C1 = "=SUM([m.xls]All!C25)"
Range("A5").Select
ActiveCell.FormulaR1C1 = "=SUM([m.xls]All!C28)"
Range("B4").Select
ActiveCell.FormulaR1C1 = "=SUM([p.xls]Treiro!C21)"
Range("B5").Select
ActiveCell.FormulaR1C1 = "=SUM([p.xls]Treiro!C23)"
Windows("p.xls").Activate
ActiveWindow.Close
Windows("m.xls").Activate
ActiveWindow.Close
Range("C4").Select
ActiveCell.FormulaR1C1 = "=IF(EXACT(RC[-2],RC[-1]),""identice"",""greseala"")"
Range("C5").Select
ActiveCell.FormulaR1C1 = "=IF(EXACT(RC[-2],RC[-1]),""identice"",""greseala"")"
Kara
  • 6,115
  • 16
  • 50
  • 57
user2015552
  • 115
  • 1
  • 2
  • 12

3 Answers3

0

From: http://msdn.microsoft.com/en-us/library/office/aa219843(v=office.11).aspx

This shows how to use a file dialog and get the filename

Sub Test()

'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd

    'Use the Show method to display the File Picker dialog box and return the user's action.
    'The user pressed the action button.
    If .Show = -1 Then

        'Step through each string in the FileDialogSelectedItems collection.
        For Each vrtSelectedItem In .SelectedItems

            'vrtSelectedItem is a String that contains the path of each selected item.
            'You can use any file I/O functions that you want to work with this path.
            'This example simply displays the path in a message box.
            MsgBox "The path is: " & vrtSelectedItem

        Next vrtSelectedItem
    'The user pressed Cancel.
    Else
    End If
End With

'Set the object variable to Nothing.
Set fd = Nothing

End Sub

Derek
  • 7,615
  • 5
  • 33
  • 58
0

I'd probably wrap it in a friendly API like so:

 Sub TestBrowseForFileName()
    Dim filename As String
    filename = BrowseForFileName
    If (Len(filename) > 0) Then
        MsgBox (filename)
    Else
        MsgBox ("nothing returned")
    End If
End Sub


Function BrowseForFileName() As String
    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = False

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Use the Show method to display the File Picker dialog box and return the user's action.
        If .Show = -1 And .SelectedItems.Count = 1 Then
            'The user pressed the action button.
            BrowseForFileName = .SelectedItems(1)
        Else
            'The user pressed the cancel button
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing

End Function
Derek
  • 7,615
  • 5
  • 33
  • 58
  • i know how to make an openfiledialog work, the thing is that the formula uses the file name, without it's path. this is where i fail. – user2015552 Sep 14 '13 at 19:52
  • I added an answer below that shows how to do that. Googled: "vba filename from path" – Derek Sep 15 '13 at 00:16
0

From: How to extract file name from path?

This is taken from snippets.dzone.com:

Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'

    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function
Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58