首页 > 解决方案 > 将路径更改为单元格值

问题描述

我在这个页面上找到了下面的代码,它工作正常。但我希望它根据我的单元格的值将其保存到一个文件夹中,在本例中为 C1

例如。根文件位于:C:\Users\john 根据我在 C1 中的值,我希望将它保存在另一个地方,到我在 C1 中C:\Users\john\games\new 放置的任何路径,这就是文件应该保存的位置。希望您能理解,并期待任何帮助或建议。

Sub MakeFolders()
    Dim Rng As Range, rw As Range, c As Range
    Dim p As String, v As String

    Set Rng = Selection

    'process each selected row
    For Each rw In Rng.Rows
        p = ActiveWorkbook.Path & "\" 'set initial root path for this row
        'process each cell in this row
        For Each c In rw.Cells
            v = Trim(c.Value) 'what's in the cell?
            If Len(v) > 0 Then
                If Len(Dir(p & v, vbDirectory)) = 0 Then MkDir (p & v) 'create if not already there
                p = p & v & "\" 'append to path (regardless of whether it needed to be created)
            End If
        Next c
    Next rw

End Sub

标签: excelvba

解决方案


推荐阅读