很抱歉这个愚蠢的问题,但是我看过多个VBA代码,却无法弄清楚如何操纵代码使其达到我的期望。
基本上,我有一个寄存器,可根据需要填写数据。我要做的基本上是选择A列中的单元格,运行一个宏,然后该Macro会在特定位置创建一个文件夹,并将单元格值作为名称。
在此文件夹中,我要另外两个具有特定名称的文件夹。
例如:
Shubhankit
任何人都可以帮助处理此请求,如果已经提出请求,我会提前道歉,但是我似乎无法在网络上找到它?
答案 0 :(得分:0)
我不理解要在 P18-457 文件夹中创建的子文件夹,但是可以完成 P18-457 文件夹和超链接的创建这样。
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim FolderPath As String
On Error GoTo errh:
If Not Application.Intersect(Target, Range("A:A")) Is Nothing Then
'Main folder path. - This need to exist already
FolderPath = "C:\Users\" & Environ("Username") & "\Desktop\Quotes\"
'Make the directory assuming the Quotes folder is already existing
MkDir FolderPath & Target.value
'Make the sub directory Costings
MkDir FolderPath & Target.value & "\Costings"
'Make the sub directory Reference
MkDir FolderPath & Target.value & "\Reference"
'Create hyperlink
ActiveSheet.Hyperlinks.Add Anchor:=Target, Address:=FolderPath & Target.value
End If
Exit Sub
'error handling
errh:
MsgBox "Error in creating subfolder with hyperlink" & vbCrLf & "Error no. " & Err.Number
End Sub
将其粘贴到您正在处理的工作表中,它应该可以工作。要运行此宏,请双击A列中的单元格
要运行它,请在复制以下代码到该命令按钮对象中时手动创建一个命令按钮。
Private Sub CommandButton1_Click()
Dim FolderPath As String
If Not Application.Intersect(ActiveCell, Range("A:A")) Is Nothing Then
If ActiveCell.Hyperlinks.Count = 0 Then
'Main folder path
FolderPath = "C:\Users\" & Environ("Username") & "\Desktop\Quotes\"
'Make the directory assuming the Quotes folder is already existing
MkDir FolderPath & ActiveCell.value
'Make the sub directory Costings
MkDir FolderPath & ActiveCell.value & "\Costings"
'Make the sub directory Reference
MkDir FolderPath & ActiveCell.value & "\Reference"
'Create hyperlink
ActiveSheet.Hyperlinks.Add Anchor:=ActiveCell, Address:=FolderPath & ActiveCell.value
End If
End If
End Sub
希望这对您有所帮助。