![Excel:创建文件夹 VBA 脚本 - 从选定的单元格获取值?](https://linux22.com/image/1419080/Excel%EF%BC%9A%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6%E5%A4%B9%20VBA%20%E8%84%9A%E6%9C%AC%20-%20%E4%BB%8E%E9%80%89%E5%AE%9A%E7%9A%84%E5%8D%95%E5%85%83%E6%A0%BC%E8%8E%B7%E5%8F%96%E5%80%BC%EF%BC%9F.png)
我正在使用 VBA 代码在目录中创建新文件夹。我想要做的是从选定的单元格中获取新文件夹名称。有什么想法可以做到这一点吗?
这是我目前拥有的代码。
If Target.Column = Range("B1").Column Then
If Target.Row > 7 Then
'Variable definitions
Dim FolderListRange As Range
Dim FolderRange As Variant
Dim FolderName As String
Dim ParentFolderPath As String
On Error GoTo Handle
' Set the Folder where the individual folders should be created
ParentFolderPath = "\\UKSH000-FILE06\purchasing\New Supplier Set-Ups"
Set FolderListRange = Range("B" & Target.Row).SpecialCells(xlCellTypeConstants)
For Each FolderRange In FolderListRange
FolderName = ParentFolderPath & "\" & FolderRange.Value
If FileSystem.Dir(FolderName, vbDirectory) = vbNullString Then
FileSystem.MkDir FolderName
End If
Continue:
Next
Handle:
End If
End If
答案1
我会尝试这样的事情:
Dim folderNameCell As Range
Dim newFolderName As String
Set folderNameCell = Application.Selection
newFolderName = folderNameCell.Value