VBA检查目录是否存在,如果存在退出子,否则不存在,创建

编程入门 行业动态 更新时间:2024-10-27 17:18:37
本文介绍了VBA检查目录是否存在,如果存在退出子,否则不存在,创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,所以我有以下vba代码,用于检查目录是否存在,如果不存在,则创建文件夹结构,如下所示:

Ok so I have the following vba code which I am using to check if a directory exists and if not create the folder structure like so:

If Dir("S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\" & Range("M" & ActiveCell.Row).Value & "\" & Range("Z" & ActiveCell.Row).Value, vbDirectory) = "" Then MkDir Path:="S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\" & Range("M" & ActiveCell.Row).Value & "\" & Range("Z" & ActiveCell.Row).Value MsgBox "Done" Else MsgBox "found it" End If

所以我的目标路径是我的 S:\ 驱动器

So my destination path is my S:\ drive

然后根据单元格c中的值,我希望它检查该文件夹是否存在,因此,如果单元格c中包含单词"tender",则目录如下所示:

then depending on the value in cell c I want it to check if that folder exists, so if cell c had the word 'tender' in it then the directory would look like:

'S:\Tender'

如果不存在,则创建,否则,继续并在该文件夹中创建另一个文件夹,并在其中保存单元格M中的值,如下所示:

If this does not exist, then create, else if this exists then move on and create another folder within this folder with the value in cell M like so:

Cell M = Telecoms 'S:\Tender\Telecoms'

然后,最后检查'S:\ Tender \ Telecoms'中是否存在具有Z单元格中值的文件夹,如果没有,则创建它.

Then finally, check if a folder with the value in cell Z exists within 'S:\Tender\Telecoms' and if not create it.

Cell Z = 12345

所以我们最终会得到:

'S:\Tender\Telecoms\12345\'

由于某些原因,我一直找不到错误消息路径.请有人能告诉我我要去哪里错吗?预先感谢

Fore some reason I keep getting the error message path not found. Please can someone show me where I am going wrong? Thanks in advance

推荐答案

我不久前写了我保存在库中的小东西:

I wrote some time ago this little thing that I keep in my library:

Function CreateFolder(ByVal sPath As String) As Boolean 'by Patrick Honorez - www.idevlop 'create full sPath at once, if required 'returns False if folder does not exist and could NOT be created, True otherwise 'sample usage: If CreateFolder("C:\toto\test\test") Then debug.print "OK" 'updated 20130422 to handle UNC paths correctly ("\\MyServer\MyShare\MyFolder") Dim fs As Object Dim FolderArray Dim Folder As String, i As Integer, sShare As String If Right(sPath, 1) = "\" Then sPath = Left(sPath, Len(sPath) - 1) Set fs = CreateObject("Scripting.FileSystemObject") 'UNC path ? change 3 "\" into 3 "@" If sPath Like "\\*\*" Then sPath = Replace(sPath, "\", "@", 1, 3) End If 'now split FolderArray = Split(sPath, "\") 'then set back the @ into \ in item 0 of array FolderArray(0) = Replace(FolderArray(0), "@", "\", 1, 3) On Error GoTo hell 'start from root to end, creating what needs to be For i = 0 To UBound(FolderArray) Step 1 Folder = Folder & FolderArray(i) & "\" If Not fs.FolderExists(Folder) Then fs.CreateFolder (Folder) End If Next CreateFolder = True hell: End Function

更多推荐

VBA检查目录是否存在,如果存在退出子,否则不存在,创建

本文发布于:2023-10-28 18:50:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   是否存在   目录   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!