我想找出一种自动执行的方法
我的Excel工作表上的过程如下
此时此刻,我想实现目标1&以上2,无需每次都运行宏,具有以下可交付成果:
到目前为止,我已经达到了
的目的我不能把它提升到一个新的水平 - 我真正想做的是,当我在C列中输入标题时,工作簿会自动检测到C列的更改/数据输入和
OPTIONAL Nice-to-have(s)将是
我怀疑这可能太复杂了 如果有人可以帮忙解决这个问题,我将非常感激 - 或者如果你确实认为我对此过于雄心勃勃,请告诉我。
有什么想法吗?
答案 0 :(得分:0)
试试这个:
将所有以下代码粘贴到
中Public blnFolderFound As Boolean
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function gUsername() As String
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then gUsername = Left$(strBuffer, lngLen - 1)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim endRow As Long
Dim rng As Range, c As Range
Dim currPath As String
endRow = Cells(ActiveSheet.Rows.Count, 3).End(xlUp).Row ''Find end row in column C
Set rng = Range(Cells(1, 3), Cells(endRow, 3)) ''check each used cell in column C
For Each c In rng '' For each cell in range
If c.Value <> vbNullString And c.Hyperlinks.Count = 0 Then ''test to see if cell not empty and no hyperlink to speed loop up
Cells(c.Row, 1).Value = Cells(c.Row, 3).Value & "_" & Cells(c.Row, 2).Value ''concatenate the two values
''Test to see if file exists and create on if it doesn't
currPath = ThisWorkbook.Path
If currPath = vbNullString Then currPath = "C:\Users\" & gUsername & "\Desktop" ''save folder to desktop if file isn't saved
folderExists currPath, Cells(c.Row, 1).Value
''if the folder is found, move on to the next cell to check
If blnFolderFound = True Then GoTo nextCellToCheck
''if the folder wasn't found and one was created in the folderExists function, add a hyperlink
ActiveSheet.Hyperlinks.Add Anchor:=c, Address:=currPath & "\" & Cells(c.Row, 1).Value, TextToDisplay:=c.Value
Else: End If
nextCellToCheck:
blnFolderFound = False
Next c
Set rng = Nothing
End Sub
Function folderExists(s_directory As String, s_folderName As String)
Dim obj_fso As Object, obj_dir As Object, obj_folder As Object
Set obj_fso = CreateObject("Scripting.FileSystemObject") '' create a filesystem object
Set obj_dir = obj_fso.GetFolder(s_directory) ''create a folder object
For Each obj_folder In obj_dir.SubFolders '' for each folder in the active workbook's folder
If obj_fso.folderExists(s_directory & "\" & s_folderName) = True Then blnFolderFound = True: Exit For ''see if the file exists
Next
If blnFolderFound = False Then obj_fso.CreateFolder (s_directory & "\" & s_folderName) ''if it doesn't exist create one
Set obj_fso = Nothing
Set obj_dir = Nothing
End Function
如果文件尚未保存,我添加了保存到用户桌面的条件。输入要在列b中连接的值,然后在列c中输入另一个值。您可能需要稍微修改一下以满足您的需求,但它应该让您指向正确的方向。