检查是目标目录存在,然后继续,如果不是然后创建它,然后进行VBA

编程入门 行业动态 更新时间:2024-10-11 21:25:02
本文介绍了检查是目标目录存在,然后继续,如果不是然后创建它,然后进行VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在其中一个工作表上有一个按钮,让用户继续他的任务,将他/她的模板作为文件夹中的单独的工作簿保存。

I have a button on one of the worksheets that lets user to continue with his task to save his/her template as a separate workbook in the folder.

这是我的代码

Private Sub ContinueButton_Click() Application.ScreenUpdating = 0 Sheets(cmbSheet.Value).Visible = True Application.Goto Sheets(cmbSheet.Value).[a22], True Application.ScreenUpdating = 1 Unload Me End Sub

现在我需要的是检查该文件夹存在,如果文件夹不存在,我的用户应该可以创建它。

Now what I need is to check if that folder exist, in case if the folder does not exist my user should be able to create it.

我的代码创建此文件夹在这里,但如何连接这2一起工作我根本不知道,因为我相当新的VBA

My code to create this folder is here below, but how to connect this 2 functions together I simply have no idea, since I am fairly new to VBA

Sub CreateDirectory() Dim sep As String sep = Application.PathSeparator 'sets the workbook's path as the current directory ChDir ThisWorkbook.Path MsgBox "The current directory is:" & vbCrLf & CurDir 'makes new folder in current directory MkDir CurDir & sep & Settings.Range("C45").Value MsgBox "The archive directory named " & Settings.Range("C45").Value & " has been created. The path to your directory " & Settings.Range("C45").Value & " is below. " & vbCrLf & CurDir & sep & Settings.Range("C45").Value End Sub

请帮助我

please help me

推荐答案

我将把您的代码模块化一点:

I am going to modularize your code a little bit:

在这里获取目录路径

Function getDirectoryPath() getDirectoryPath = ThisWorkbook.Path & Application.PathSeparator & Settings.Range("C45").Value End Function

您可以创建目录使用此功能

You can create the directory using this function

Sub createDirectory(directoryPath) MkDir directoryPath End Sub

您可以使用 Dir 函数检查目录是否存在/ p>

You can check if a directory exists or not using Dir function

Dir(directoryPath, vbDirectory) 'empty string means directoryPath doesn't exist

按钮上的最后一个功能点击:

The final function on button click:

Private Sub ContinueButton_Click() Application.ScreenUpdating = 0 Sheets(cmbSheet.Value).Visible = True directoryPath = getDirectoryPath 'Creating the directory only if it doesn't exist If Dir(directoryPath, vbDirectory) = "" Then createDirectory directoryPath End If Application.Goto Sheets(cmbSheet.Value).[a22], True Application.ScreenUpdating = 1 Unload Me End Sub

更多推荐

检查是目标目录存在,然后继续,如果不是然后创建它,然后进行VBA

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

发布评论

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

>www.elefans.com

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