【问题标题】:File path when & "\" - Runtime Error 76 Path not found&“\”时的文件路径 - 运行时错误 76 找不到路径
【发布时间】:2021-03-10 13:04:15
【问题描述】:

文件路径在以下情况下不起作用:

Path = Sheets("Sheet1").Range("C2").Value & "\"

C2中的文件路径末尾不包含\

Sub Make_Folders_And_SubFolders()

Dim GPath, GName, UName, UGroup As String
Dim UserID, Groups, G, U As Range
Dim Gcounter, Ucounter As Integer

GPath = Sheets("Sheet1").Range("C2").Value & "\"

On Error GoTo Finish
If Len(GPath) = 0 Or Right(GPath, 1) <> "\" Then
Finish:
    MsgBox "Please, check if:" & vbNewLine _
    & "1- Folder Path is empty." & vbNewLine _
    & "2- or "" \ "" is missing at the end of the path." & vbNewLine _
    & "3- or Path does not exist.", vbCritical
    Exit Sub
End If

Set Groups = Sheets("Sheet1").Range(Cells(5, "P"), Cells(Rows.Count, "P").End(xlUp))
Set UserID = Sheets("Sheet1").Range(Cells(5, "Q"), Cells(Rows.Count, "Q").End(xlUp))

For Each G In Groups
    GName = Trim(G.Value) & "_Group"
    If Len(Dir(GPath & GName, vbDirectory)) > 0 Then
        GoTo Nxt1
    Else
        MkDir GPath & GName
        Gcounter = Gcounter + 1
    End If

Nxt1:
Next G

For Each U In UserID
    UName = Trim(U.Value)
    UGroup = Trim(U.Offset(0, 1).Value) & "_Group"
    If Len(Dir(GPath & UGroup & "\" & UName, vbDirectory)) > 0 Then
        GoTo Nxt2
    Else
        MkDir GPath & UGroup & "\" & UName
        Ucounter = Ucounter + 1
    End If

Nxt2:

Next U

If Gcounter + Ucounter = 0 Then
    MsgBox "All Folders exist, " & vbNewLine & "No folder to be created"
Else
    MsgBox "Job Done !!" & vbNewLine _
      & "Group Folders created: = " & Gcounter & vbNewLine _
      & "User ID Folders created: = " & Ucounter, _
      Title:="Foders Created Count"
End If
End Sub

【问题讨论】:

  • 我们可以查看C2 中的值以及您是如何使用它的吗?
  • @VBasic2008 C:\Users\myuser\Documents\OneDrive - mycompany\txt\folder1
  • @VBasic2008 已添加! (:
  • 错误是什么? 70、76、53?
  • @VBasic2008 错误 76

标签: excel vba


【解决方案1】:

我可以建议文件操作和路径构建使用FileSystemObject。它绝对不需要在路径末尾添加\,因为它足够聪明,可以在需要时添加它。具体来说,fso.BuildPath() 方法就是为满足这一确切需求而设计的。

Public Sub CombinePathTest()

    Dim path As String, fn As String
    
    path = fso.GetParentFolderName(Sheets("Sheet1").Range("C2").Value)
    
    fn = fso.BuildPath(path, Uname)

End Sub

documentation,您可以看到有很多有用的功能可以减轻构建路径、打开文件、创建或删除或检查是否存在的压力。

通过定义为引用并添加全局 fso 对象,您可以在编写代码时访问 Intellisense

【讨论】:

    【解决方案2】:

    试试下面的。请注意,我从For Each G In Groups 到最后都没有碰过任何东西。

    Sub FileOperation()
        
        Dim GPath As String, GName As String, UName As String, UGroup As String
        Dim UserID As Range, Groups As Range, G As Range, U As Range
        Dim Gcounter As Long, Ucounter As Long
        Dim pSep As String: pSep = Application.PathSeparator
        Dim wb As Workbook: Set wb = ThisWorkbook
            
        Dim ws As Worksheet
        Set ws = wb.Worksheets("Sheet1")
        GPath = ws.Range("C2").Value
    
        If Len(GPath) = 0 Then
            MsgBox "Path is empty.", vbCritical
            Exit Sub
        End If
        If Right(GPath, 1) <> pSep Then
            GPath = GPath & pSep
        End If
        
        Set Groups = ws.Range(ws.Cells(5, "P"), ws.Cells(Rows.Count, "P").End(xlUp))
        Set UserID = ws.Range(ws.Cells(5, "Q"), ws.Cells(Rows.Count, "Q").End(xlUp))
        
        For Each G In Groups
                GName = Trim(G.Value) & "_Group"
                If Len(Dir(GPath & GName, vbDirectory)) > 0 Then
                        GoTo Nxt1
                Else
                        MkDir GPath & GName
                        Gcounter = Gcounter + 1
                End If
        
    Nxt1:
        Next G
        
        For Each U In UserID
                UName = Trim(U.Value)
                UGroup = Trim(U.Offset(0, 1).Value) & "_Group"
                If Len(Dir(GPath & UGroup & "\" & UName, vbDirectory)) > 0 Then
                        GoTo Nxt2
                Else
                        MkDir GPath & UGroup & "\" & UName
                        Ucounter = Ucounter + 1
                End If
        
    Nxt2:
        
        Next U
        
        If Gcounter + Ucounter = 0 Then
                MsgBox "All Folders exist, " & vbNewLine & "No folder to be created"
        Else
                MsgBox "Job Done !!" & vbNewLine _
                & "Group Folders created: = " & Gcounter & vbNewLine _
                & "User ID Folders created: = " & Ucounter, _
                Title:="Foders Created Count"
        End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 2020-05-26
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多