此代码打开两个excel文件,从每个文件中取出一列的总和,然后将它们放在原始文件的两个单元格中。我的代码打开已定义的文件,但我希望脚本询问我想要选择哪些文件。
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"")"
答案 0 :(得分: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
End Sub
答案 1 :(得分: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
答案 2 :(得分:0)
来自:How to extract file name from path?
这取自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