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"")"
4

3 回答 3

0

来自:如何从路径中提取文件名?

这取自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
于 2013-09-15T00:15:19.057 回答
0

我可能会将它包装在一个友好的 API 中,如下所示:

 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
于 2013-09-14T16:40:16.170 回答
0

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

这显示了如何使用文件对话框并获取文件名

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

结束子

于 2013-09-14T16:29:47.203 回答