简体   繁体   English

想知道如何在Excel 2010中使用VBA创建文件夹

[英]Wondering about creating folders with VBA in Excel 2010

I'm a newbie here and really nice to meet you, all. 我是这里的新手,很高兴认识您。 And hope I can get some lessons here and I'll be the helpful as well. 希望我能从这里学到一些教训,并且也会对我有所帮助。

Ok, then, let's proceed the question directly. 好的,那么,让我们直接继续问题。 When I'm working, I should create some folder structures with many sub folders. 在工作时,我应该创建一些包含许多子文件夹的文件夹结构。 To do so conveniently, our manager created some vba scripts using VBA in Excel 2010, but he quit now. 为方便起见,我们的经理在Excel 2010中使用VBA创建了一些vba脚本,但他现在退出了。 In most cases, I created the folder structure using the file. 在大多数情况下,我使用该文件创建了文件夹结构。 And when I do this, I select 'Mobile' or'Monitor' from the dropdown list in the excel sheet, and got the result following. 然后,我从excel工作表的下拉列表中选择“移动”或“显示器”,然后得到以下结果。

1_Query

2_File

3_INI

5_Reference

6_TM

7_Log

8_PO

The script for creating the above folder structure is like following. 创建上述文件夹结构的脚本如下。

Dim Fieldname As String

Sub Load_Click()

Dim Y_Field As Integer
Dim B_strPath As String

    If ActiveSheet.Cells(1, 8).Value <> "" Then
    B_strPath = ActiveSheet.Cells(1, 8)
    MkDir (B_strPath & "\1_From_Client")
    MkDir (B_strPath & "\1_From_Client\3_TM")
    MkDir (B_strPath & "\1_From_Client\4_Log")
    MkDir (B_strPath & "\2_To_TR")
    MkDir (B_strPath & "\3_query")
    MkDir (B_strPath & "\4_revised")
    MkDir (B_strPath & "\5_From_TR")
    MkDir (B_strPath & "\6_To_Client")
    MkDir (B_strPath & "\7_TM")
    MkDir (B_strPath & "\8_PO")
    MkDir (B_strPath & "\9_Invoice")

        Worksheets("Make DIR").Activate
        CellV1 = Cells(5, 5).Value

        For X = 3 To 4000
            If Worksheets("Project").Cells(X, 3).Value = CellV1 Then
                cellv = Worksheets("Project").Cells(X, 7).Offset(0, 0).Value
                'MsgBox cellv
                Fieldname = Worksheets("Project").Cells(X, 6).Offset(0, 0).Value
                TTT

            End If

        Next X
        Else
        MsgBox "select folder first"

    End If


End Sub

Sub TTT()
Dim strPath As String
Dim strPath_Division As String
Dim SrceFile
Dim DestFile

 'MsgBox Fieldname
 strPath = ActiveSheet.Cells(1, 8)
 strPath_Division = ActiveSheet.Cells(8, 5)


 MkDir (strPath & "\2_To_TR\" & Fieldname)
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\_Query")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\2_File")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\3_INI")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\5_Reference")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\6_TM")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\7_Log")
 MkDir (strPath & "\2_To_TR\" & Fieldname & "\8_PO")
 MkDir (strPath & "\6_To_Client\" & Fieldname)

  If strPath_Division = "Mobile" Then
    MkDir (strPath & "\2_To_TR\" & Fieldname & "\4_Term")
    SrceFile = "D:\_Project\_Term\_Mobile\Mobile_Common_Term_130115_" & Fieldname & ".xlsx"
    DestFile = strPath & "\2_To_TR\" & Fieldname & "\4_Term\Mobile_Common_Term_130115_" & Fieldname & ".xlsx"
    FileCopy SrceFile, DestFile
    Else
    MkDir (strPath & "\2_To_TR\" & Fieldname & "\4_Term")
  End If

But recently, I should rearrange the scripts to add some other folder structures with the 'BOX' from drop down list, like the following. 但是最近,我应该重新排列脚本,以使用下拉列表中的“ BOX”添加一些其他文件夹结构,如下所示。

2_File 2_文件

8_PO 8_PO

To do so, I added some scripts by myself, but it doesn't working correctly. 为此,我自己添加了一些脚本,但是无法正常工作。 The script I added is like following. 我添加的脚本如下。

Sub BOX()
Dim strPath As String
Dim strPath_Division As String
Dim SrceFile
Dim DestFile

 'MsgBox Fieldname
 strPath = ActiveSheet.Cells(1, 8)
 strPath_Division = ActiveSheet.Cells(8, 5)

 If strPath_Division = "BOX" Then
   MkDir (strPath & "\2_To_TR\" & Fieldname)
   MkDir (strPath & "\2_To_TR\" & Fieldname & "\2_File")
   MkDir (strPath & "\2_To_TR\" & Fieldname & "\8_PO")
   MkDir (strPath & "\6_To_Client\" & Fieldname)
 End If


End Sub

Well, as I don't have any reps here, I just copy and paste the script here. 好吧,由于我这里没有任何代表,所以我只需要在此处复制并粘贴脚本。 And hope it won't cause any inconveniences to you and hope I can take great lessons from all of you. 希望不会给您带来任何不便,并希望我能从大家中学到很多教训。

I do appreciate in advance for your understanding and adivse. 预先感谢您的理解和协助。

Not sure if this is what you asked but I cleaned it up a bit, and made it easier to add more sub-folders: 不知道这是否是您要的内容,但我对其进行了一些整理,使添加更多子文件夹变得更加容易:

Option Explicit

Dim fieldName As String

Sub makeForders()
    Dim i As Long, wsDir As Worksheet, wsPrj As Worksheet, mainPath As String

    Set wsDir = Worksheets("Make DIR")
    Set wsPrj = Worksheets("Project")
    If Len(ActiveSheet.Cells(1, 8).Value) > 0 Then
        mainPath = ActiveSheet.Cells(1, 8)
        If Len(Dir(mainPath & "1_From_Client", vbDirectory)) = 0 Then
            MkDir mainPath & "1_From_Client"
            MkDir mainPath & "1_From_Client\3_TM"
            MkDir mainPath & "1_From_Client\4_Log"
            MkDir mainPath & "2_To_TR"
            MkDir mainPath & "3_query"
            MkDir mainPath & "4_revised"
            MkDir mainPath & "5_From_TR"
            MkDir mainPath & "6_To_Client"
            MkDir mainPath & "7_TM"
            MkDir mainPath & "8_PO"
            MkDir mainPath & "9_Invoice"
        End If
        For i = 3 To 4000
            If wsPrj.Cells(i, 3).Value = wsDir.Cells(5, 5).Value Then
                makeSubForders wsPrj.Cells(i, 6).Value
            End If
        Next
    Else
        MsgBox "Select folder first"
    End If
End Sub

Sub makeSubForders(ByVal fieldName As String)
    Const MDL       As String = "\2_To_TR\"
    Const D_PATH    As String = "D:\_Project\_Term\_Mobile\Mobile_Common_Term_130115_"
    Const TERMS     As String = "4_Term\Mobile_Common_Term_130115_"
    Const EXT       As String = ".xlsx"
    Dim pathIni As String, pathDiv As String
    Dim srceFile As String, destFile As String, mainPath As String
    mainPath = ActiveSheet.Cells(1, 8).Value2
    If Len(Dir(mainPath, vbDirectory)) = 0 Then
        MkDir mainPath: MkDir mainPath & "\" & MDL
    End If
    pathIni = mainPath & MDL & fieldName
    If Len(Dir(pathIni, vbDirectory)) = 0 Then
        MkDir pathIni: pathIni = pathIni & "\"
        MkDir pathIni & "_Query"
        MkDir pathIni & "2_File"
        MkDir pathIni & "3_INI"
        MkDir pathIni & "4_Term"
        MkDir pathIni & "5_Reference"
        MkDir pathIni & "6_TM"
        MkDir pathIni & "7_Log"
        MkDir pathIni & "8_PO"
        MkDir pathIni & "6_To_Client\"
        MkDir pathIni & "6_To_Client\" & fieldName
    End If
    pathDiv = ActiveSheet.Cells(8, 5).Value     '"Mobile" or "BOX"
    Select Case pathDiv
        Case "Mobile"
            If Len(Dir(pathIni & "\Mobile_Path", vbDirectory)) = 0 Then
                MkDir pathIni & "\Mobile_Path"
            End If
        Case "BOX"
            If Len(Dir(pathIni & "\BOX_Path", vbDirectory)) = 0 Then
                MkDir pathIni & "\BOX_Path"
            End If
    End Select
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM