将macros应用于整个列

我有一个macros,运行时,打开文件资源pipe理器,当用户select特定的path,它粘贴在活动单元格上的path。 这是代码。

Sub CommandButton1_Click() With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = IIf(ActiveCell.Value = "", ActiveWorkbook.Path, ActiveCell.Value) .Title = "Please choose a folder" .AllowMultiSelect = False If .Show = -1 Then ActiveCell.Value = .SelectedItems(1) End With End Sub 

现在,我不想把这个模块分配给一个button,而是分配给一个整列,比如A列,所以无论用户何时点击A列中的任何一行,macros都会被激活。 有什么办法可以做到这一点?

把它放在你想要触发文件夹select的工作表的代码模块中:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.CountLarge = 1 And Target.Cells(1).Column = 1 Then With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = IIf(target.Value = "", _ thisWorkbook.Path, target.Value) .Title = "Please choose a folder" .AllowMultiSelect = False If .Show = -1 Then Target.Value = .SelectedItems(1) End With End If End Sub 

双击:

 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Cells.CountLarge = 1 And Target.Cells(1).Column = 1 Then With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = IIf(ActiveCell.Value = "", ActiveWorkbook.Path, ActiveCell.Value) .Title = "Please choose a folder" .AllowMultiSelect = False If .Show = -1 Then Target.Value = .SelectedItems(1) End With Cancel = True '<< prevents going into edit mode End If End Sub