我现在想在excel同级的目录下建立一个文件夹,不知道路径该如何表示,用相对路径,谢谢!
试试下面的代码:在当前目录下依次创建指定文件夹
Sub test()Dim arr, arrTemp
Dim strPath As String
Dim strTemp
Dim i As Long, j As Long
On Error Resume Next
strPath = ThisWorkbook.Path & Application.PathSeparator
arr = Sheet1.Range("a1").CurrentRegion
For i = LBound(arr) + 1 To UBound(arr)
strTemp = strPath
arrTemp = Split(arr(i, 1), "")
For j = LBound(arrTemp) To UBound(arrTemp)
strTemp = strTemp & arrTemp(j) & Application.PathSeparator
MkDir strTemp
Next
Next
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答 2018-03-29
excel获取当前工作簿路径
ThisWorkbook.Pathexcel新建文件夹
MkDir "c:\temp"第2个回答 2018-03-29
是可以的。请用以下代码进行测试:
hMkDir ("D:\1\2\3\4\") ‘调用测试
’===================
Sub hMkDir(fPath As String)
Dim sp() As String, k%, strP$
If fPath = "" Then Exit Sub
strP$ = IIf(Right(fPath, 1) = "\", Mid(fPath, 1, Len(fPath) - 1), fPath)
sp = Split(fPath, "\"): strP$ = ""
Do While k < UBound(sp) + 1
strP$ = IIf(strP$ = "", sp(k), strP$ & "\" & sp(k))
If Dir(strP$, vbDirectory) = "" Then MkDir strP$
k = k + 1
Loop
End Sub
hMkDir ("D:\1\2\3\4\") ‘调用测试
’===================
Sub hMkDir(fPath As String)
Dim sp() As String, k%, strP$
If fPath = "" Then Exit Sub
strP$ = IIf(Right(fPath, 1) = "\", Mid(fPath, 1, Len(fPath) - 1), fPath)
sp = Split(fPath, "\"): strP$ = ""
Do While k < UBound(sp) + 1
strP$ = IIf(strP$ = "", sp(k), strP$ & "\" & sp(k))
If Dir(strP$, vbDirectory) = "" Then MkDir strP$
k = k + 1
Loop
End Sub