查找目录,在不存在时创建目录

时间:2016-02-24 13:51:19

标签: excel vba excel-vba directory mkdir

我正在尝试在Excel中创建一个宏,删除工作区中的所有信息并创建一个新文件(一个用于新一周的信息)。这个任务对我来说似乎很简单,但由于某种原因,我的目录发现和代码的创建部分返回“我错了”的msgbox我已添加以表明没有满足任何条件。我已经检查了语句中目录的拼写和位置,一切似乎都是正确的。我只需要一双新鲜的眼睛,因为我确信我现在缺少一些明显的东西。

$('.alert').on('keydown', function(event) {
  event.preventDefault();
  event.stopPropagation();
  window.alert('keydown intercepted');
  return false;
});

$('.noalert').on('keydown', function(event) {
  event.preventDefault();
  event.stopPropagation();
  return false;
});

感谢您提供的任何帮助!

编辑:我已经改变了一些东西并且已经超越了原来的问题。显示更改后,我将返回路径/文件访问错误(75)。

Sub DerpDate()
'--------------------------------------------------------------------------------------------------
'Subroutine that creates necessary directories, places new workbook in those directories and clears
'out old data before terminating
'--------------------------------------------------------------------------------------------------

'--------------------------------------------------------------------------------------------------
'                                           Declarations
'--------------------------------------------------------------------------------------------------
Dim NxtWk As Range, YrFind As Range, MonFind As Range, MonName As Range, _
LngName As String, DerpName As String, DelProd As Range, DelMold As Range, ProdSheet As Sheets, _
MoldSheet As Sheets, WindSheets As Sheets

    Set NxtWk = Sheets("Data").Range("B53")
    Set YrFind = Sheets("Data").Range("C53")
    Set MonFind = Sheets("Data").Range("D53")
    Set MonName = Sheets("Data").Range("E53")
    LngName = MonFind.Value & "-" & MonName.Value & "-" & YrFind.Value
    DerpName = "\\Jupiter\Production\2 Production Schedules\"

        'Production Ranges

        Set DelProd = Application.Union( _
        Sheets("Production Schedule").Range("H5:AB9"), Sheets("Production Schedule").Range("H15:AB23"), _
        Sheets("Production Schedule").Range("H29:AB30"), Sheets("Production Schedule").Range("H36:AB39"), _
        Sheets("Production Schedule").Range("H45:AB54"), Sheets("Production Schedule").Range("H60:AB62"), _
        Sheets("Production Schedule").Range("H68:AB73"), Sheets("Production Schedule").Range("H79:AB84"), _
        Sheets("Production Schedule").Range("H90:AB94"), Sheets("Production Schedule").Range("H100:AB101"), _
        Sheets("Production Schedule").Range("H107:AB112"), Sheets("Production Schedule").Range("H118:AB119"), _
        Sheets("Production Schedule").Range("H125:AB126"), Sheets("Production Schedule").Range("H132:AB133"), _
        Sheets("Production Schedule").Range("H139:AB140"), Sheets("Production Schedule").Range("H146:AB147"), _
        Sheets("Production Schedule").Range("H153:AB156"), Sheets("Production Schedule").Range("H162:AB166"), _
        Sheets("Production Schedule").Range("H172:AB175"), Sheets("Production Schedule").Range("H181:AB185"), _
        Sheets("Production Schedule").Range("H186:AB186"), Sheets("Production Schedule").Range("H192:AB193"))

        'Molding Ranges

        Set DelMold = Application.Union( _
        Sheets("Molders").Range("B5:W8"), Sheets("Molders").Range("B14:W20"), _
        Sheets("Molders").Range("B26:W31"), Sheets("Molders").Range("B37:W38"), Sheets("Molders").Range("B44:W45"), _
        Sheets("Molders").Range("B51:W54"), Sheets("Molders").Range("B60:W63"), Sheets("Molders").Range("B69:W72"), _
        Sheets("Molders").Range("C78:W93"))

        'Winding Ranges

        Set DelWind = Application.Union( _
        Sheets("Winders").Range("H5:AB6"), Sheets("Winders").Range("H8:AB9"), Sheets("Winders").Range("H11:AB12"), _
        Sheets("Winders").Range("H14:AB15"), Sheets("Winders").Range("H17:AB18"), Sheets("Winders").Range("H20:AB21"), _
        Sheets("Winders").Range("H23:AB24"), Sheets("Winders").Range("H26:AB27"), Sheets("Winders").Range("H29:AB30"), _
        Sheets("Winders").Range("H32:AB33"), Sheets("Winders").Range("H35:AB36"), Sheets("Winders").Range("H38:AB39"), _
        Sheets("Winders").Range("H41:AB42"), Sheets("Winders").Range("H44:AB45"), Sheets("Winders").Range("H47:AB48"), _
        Sheets("Winders").Range("H50:AB51"), Sheets("Winders").Range("H53:AB54"))

'--------------------------------------------------------------------------------------------------
'Booleans to determine what (if any) directories need to be created before a new workbook can be
'created
'--------------------------------------------------------------------------------------------------

    'See if the Year AND Month folder exist yet--save the new spreadsheet
    If Dir(DerpName & YrFind.Value & "\" & LngName) <> "" Then
    ActiveWorkbook.SaveAs Filename:=DerpName & YrFind.Value & "\" & LngName & "\" & NxtWk.Value & ".xlsm"

    'If the Year AND Month Folder don't exist, see if just the Year folder does--create Month folder
    'and save the new spreadsheet in it
    ElseIf Dir(DerpName & YrFind.Value) <> "" Then
    MkDir (DerpName & YrFind.Value & "\" & LngName)
    ActiveWorkbook.SaveAs Filename:=DerpName & YrFind.Value & "\" & LngName & "\" & NxtWk.Value & ".xlsm"

    'If the Year and Month Folder don't exist, create Year and Month folder and save the
    'new spreadsheet in it
    ElseIf Dir(DerpName) <> "" Then
    MkDir (DerpName & YrFind.Value)
    MkDir (DerpName & YrFind & "\" & LngName)
    ActiveWorkbook.SaveAs Filename:=DerpName & YrFind.Value & "\" & LngName & "\" & NxtWk.Value & ".xlsm"

    Else

    MsgBox ("Something is wrong with the file location operation in the DerpDate Subroutine")

    End If

'--------------------------------------------------------------------------------------------------
'Portion of the sub that removes old data from the new workbook
'--------------------------------------------------------------------------------------------------

    DelProd.ClearContents

    DelMold.ClearContents

    DelWind.ClearContents

End Sub

我唯一能想到的是,这是因为我正在尝试更改共享网络目录,并且有一个我没有看到的用户设置。

1 个答案:

答案 0 :(得分:0)

对于遇到此问题的任何人,我通过一些基于主题的谷歌搜索找到了它。 MkDir并不喜欢UNC文件路径。因此,无论我尝试使用格式化和连接,都不会起作用。为了使用UNC-pathed网络位置执行相同的工作,您需要一个单独的API函数。我在这里发现了一篇很棒的文章:

http://www.devhut.net/2011/09/15/vba-create-directory-structurecreate-multiple-directories/

只需将API放在一个单独的模块中,然后在宏中使用UNC文件路径调用它。 API:

Public Sub MakeFullDir(strPath As String)
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\" 'Optional depending upon intent
    MakeSureDirectoryPathExists strPath
End Sub

宏示例:

Sub Example()
Dim filepath As String

filepath = "\\Server\Directory\SubDirectory\FolderYouWantToCreate"

Call MakeFullDir(filepath)

End Sub

API实际上替换了布尔值和MkDir,因为它执行这两个函数。

希望这有助于某人!