gpt4 book ai didi

vba - 从给定单元格右侧的范围中选择值,并从中迭代创建子文件夹

转载 作者:行者123 更新时间:2023-12-02 10:34:30 26 4
gpt4 key购买 nike

希望你一切都好。

我正在尝试使用 Excel 和 vba 创建一个批量文件夹创建器。这是我第一次使用 VBA,因为我通常专注于基于网络的语言,所以请原谅我事先缺乏知识。我已经有了一些代码,它只是完成了我正在努力完成的最后工作。

当前,用户在给定单元中指定目录,并在另一个单元中指定父文件的名称。单击按钮后,宏将使用父文件单元中的目录和名称创建父文件夹。然后,它使用受访者在运行宏时选择的任何单元格的值创建子文件夹。

我目前正在努力完成该项目的下一阶段,即在子文件夹中创建子文件夹(我将其称为“孙子”)。如果所有子文件夹都有相同的孙子文件夹,这将很容易,但事实并非如此。我想做的是获取每个单元格右侧的 3 个值,这些值定义了子文件夹的名称,并使用它们来创建孙子,但是我目前使用我当前使用的代码收到“无效限定符”消息(见下文)。

BasePath = Range("folder_path")

'Check if the project folder already exists and if so raise and error and exit
If Dir(BasePath, vbDirectory) <> "" Then
MsgBox BasePath & " already exists", , "Error"
Else

'Create the project folder
MkDir BasePath

MsgBox "Parent folder creation complete"

'Loop through the 1st tier subfolders and create them
For Each c In ActiveWindow.RangeSelection.Cells
'create new folder path
NewFolder = BasePath & "\" & c.Value
'create folder

If fs.folderexists(NewFolder) Then
'do nothing
Else
MkDir NewFolder
End If

Next c

'Create GrandChildren
For Each d In ActiveWindow.RangeSelection.Cells
'Offset the selection to the right

For Each e In d.Offset(0, 1).Resize(1, 3).Cells

Test = e.Value
GrandChild = BasePath & "\" & d.Value & "\" & Test

If fs.folderexists(GrandChild) Then
'do nothing
Else
MkDir GrandChild
End If

Next e
Next d

MsgBox "Sub-folder creation complete"

End If

End Sub

如果您需要任何进一步的信息,请告诉我。

干杯,

杰森

最佳答案

我认为你的问题就在这里

测试 = d.Offset(0, 1).Select

Test 是一个字符串,您正在选择一个单元格。你应该尝试这个:

Test = d.Offset(0,1).Value

关于vba - 从给定单元格右侧的范围中选择值,并从中迭代创建子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com