Excel:创建文件夹 VBA 脚本 - 从选定的单元格获取值?

Excel:创建文件夹 VBA 脚本 - 从选定的单元格获取值?

我正在使用 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

相关内容