简体   繁体   English

Excel单元格自动打开/关闭文件窗口,并将文件名和路径作为单元格值

[英]Excel cell auto open/close file window and put filename and path as cell value

I am newbie in Excel. 我是Excel的新手。 I need to something like below. 我需要像下面这样的东西。

When user click on a cell or enter to cell: 当用户单击单元格或输入单元格时:

  1. It should automatically open/close file window. 它应该自动打开/关闭文件窗口。

  2. When user select a file, it should pick up path/filename and put into the cell, like c:\\folder1\\file1.ext 当用户选择一个文件时,它应该选择路径/文件名并放入单元格,如c:\\folder1\\file1.ext

  3. If user select more than one file, it should pick up all path/filenames into cell,with | 如果用户选择多个文件,它应该使用|来获取所有路径/文件名到单元格中 as delimiter. 作为分隔符。 like c:\\folder1\\file1.ext|d:\\folder2\\file2.ext 比如c:\\folder1\\file1.ext|d:\\folder2\\file2.ext

  4. If user click on a cell or enter to cell for a second time, it should keeps existing path/filenames and let to add other path/filnames to them like in number 3 如果用户单击一个单元格或第二次进入单元格,它应该保留现有的路径/文件名,并允许添加其他路径/文件名,如3号

This is similar to Sid's, just lets you double click any single cell to open the file dialog. 这类似于Sid,只需双击任何单个单元格即可打开文件对话框。

In a Module 在一个模块中

该图显示了粘贴getList代码的位置

Public Function getList(Optional ByVal Target As Range = Nothing) As String
Dim Dialog As FileDialog
Dim File As Integer
Dim Index As Integer

Dim List() As String
Dim Item As Integer
Dim Skip As Boolean

Set Dialog = Application.FileDialog(msoFileDialogFilePicker)

File = Dialog.Show

If File = -1 Then
    ' Get a list of any pre-existing files and clear the cell
    If Not Target Is Nothing Then
        List = Split(Target.Value, "|")
        Target.Value = ""
    End If
    ' Loop through all selected files, checking them against any pre-existing ones to prevent duplicates
    For Index = 1 To Dialog.SelectedItems.Count
        Skip = False
        For Item = LBound(List) To UBound(List)
            If List(Item) = Dialog.SelectedItems(Index) Then
                Skip = True
                Exit For
            End If
        Next Item
        If Skip = False Then
            If Result = "" Then
                Result = Dialog.SelectedItems(Index)
            Else
                Result = Result & "|" & Dialog.SelectedItems(Index)
            End If
        End If
    Next Index
    ' Loop through the pre-existing files and add them to the result
    For Item = UBound(List) To LBound(List) Step -1
        If Not List(Item) = "" Then
            If Result = "" Then
                Result = List(Item)
            Else
                Result = List(Item) & "|" & Result
            End If
        End If
    Next Item
    Set Dialog = Nothing
    ' Set the target output if specified
    If Not Target Is Nothing Then
        Target.Value = Result
    End If
    ' Return the string result
    getList = Result

End If
End Function

In Your Worksheet's Code 在您的工作表代码中

图像显示粘贴工作表代码的位置

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then getList Target
End Sub

Update I've changed the getList function (it wasn't broken, just made it do more) 更新我已经更改了getList函数(它没有被破坏,只是让它做得更多)

  • It will allow you to double click any cell, which will open a file dialog. 它将允许您双击任何单元格,这将打开文件对话框。
  • You can select 1 (or more) files 您可以选择1个(或更多)文件
  • The file names will be joined with the "|" 文件名将与“|”连接 character and put in the target cell 字符并放入目标单元格
  • If any pre-existing files are in the cell, the new ones will be appended to them 如果单元格中有任何预先存在的文件,则新的文件将附加到它们中

It does not however support pressing enter to open the file dialog, you must double-click the cell. 但是它不支持按Enter键打开文件对话框,必须双击该单元格。

Update To help VMO (commenter) 更新以帮助VMO(评论者)

The code in the worksheet module: 工作表模块中的代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
        If Target.Address = "$A$1" Then ' See Notes Below
            Target.Value = getList(Target)
        End If
    End If
End Sub

To restrict what cell(s) are double-click'able you will need to use something like that. 要限制哪些单元格可以双击,你需要使用类似的东西。 You can change $A$1 to whatever you want or find a way to determine the target range's name (not too difficult) 您可以将$A$1更改$A$1您想要的任何内容,或者找到确定目标范围名称的方法(不太难)

If your worksheet is not locked the cell that is clicked will keep focus, and be in edit-mode which is a little annoying. 如果您的工作表未锁定,则单击的单元格将保持焦点,并处于编辑模式,这有点烦人。 Locking the cell, in previous versions of excel fixed this (i think it doesn't work in v.2010+ though) 锁定单元格,在以前版本的excel中修复了这个(我认为它在v.2010 +中不起作用)

The code in the module (getList) can remain almost exactly the same (although you might want to remove all the code that deals with multiple files, not required though). 模块中的代码(getList)可以保持几乎完全相同(尽管您可能希望删除处理多个文件的所有代码,但不是必需的)。 All you need to do is add one line of code. 您需要做的就是添加一行代码。

.......
Dim Skip As Boolean

Set Dialog = Application.FileDialog(msoFileDialogFilePicker)

Dialog.AllowMultiSelect = False ' This will restrict the dialogue to a single result

File = Dialog.Show

If File = -1 Then
......

Hope this helps and I've understood what you were asking! 希望这有帮助,我明白你的要求!

This should do the trick. 这应该可以解决问题。 The first subroutine is the event that is triggered on the user clicking on a cell. 第一个子例程是用户单击单元格时触发的事件。 Change the row and column numbers in the first if statement to change the target cell. 更改第一个if语句中的行号和列号以更改目标单元格。 You can put all of this code in the code module for the worksheet you want it to work on. 您可以将所有这些代码放在代码模块中,以用于您希望它工作的工作表。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim filenames() As String
    Dim filename As Variant
    Dim filelist As String

    ' Make sure the user clicked our target cell

    If Target.Row = 2 And Target.Column = 2 Then

        ' Get a list of filenames

        filenames = GetFileNames

        ' Make sure we got some filenames

        If UBound(filenames) > 0 Then

            ' Go through the filenames, adding each to the output string

            For Each filename In filenames
                filelist = filelist & CStr(filename) & "|"
            Next filename

            ' Remove the final delimiter

            filelist = Left(filelist, Len(filelist) - 2)

            ' Apply the output string to the target cell (adding another
            ' delimiter if there is already text in there)

            If Not Target.Value = "" Then
                Target.Value = Target.Value & "|"
            End If

            Target.Value = Target.Value & filelist

        End If

    End If

End Sub

The following function is that which is called to open the file dialogue and retrieve the filenames. 以下函数是调用打开文件对话框并检索文件名的函数。

Private Function GetFileNames() As String()

    Dim dlg As FileDialog
    Dim filenames() As String
    Dim i As Integer

    ' Open a file dialogue

    Set dlg = Application.FileDialog(msoFileDialogFilePicker)

    With dlg
        .ButtonName = "Select"                  ' Text of select/open button
        .AllowMultiSelect = True                ' Allows more than one file to be selected
        .Filters.Add "All Files", "*.*", 1      ' File filter
        .Title = "Select file(s)"               ' Title of dialogue
        .InitialView = msoFileDialogViewDetails
        .Show

        ' Redimension the array with the required number of filenames

        ReDim filenames(.SelectedItems.Count)

        ' Add each retrieved filename to the array

        For i = 1 To .SelectedItems.Count
            filenames(i - 1) = .SelectedItems(i)
        Next i

    End With

    ' Clean up and return the array

    Set dlg = Nothing
    GetFileNames = filenames

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM