【问题标题】:Want to check if there is a worksheet with the same name as the file想检查是否有与文件同名的工作表
【发布时间】:2017-04-15 10:34:45
【问题描述】:

要检查是否有与文件同名的工作表。

目前我让 filnam 将文件作为数组打开,但希望它循环浏览代码并查看是否有同名的工作表。

我已使用拆分来删除路径名和扩展名,但无法对其进行检查。

对于代码的混乱,我深表歉意。一直在努力整理,然后我会整理一下。还有更多代码,但这不是必需的,因为如果没有匹配项,我希望它运行该代码。

你能帮忙吗?

Sub sort_it_out()

 Dim wb1 As Workbook
 Dim wb2 As Workbook
 Dim Sheet As Worksheet
 Dim filnam As Variant
 On Error GoTo errorhandler

 Set wb1 = ActiveWorkbook

 ChDir Application.ActiveWorkbook.path

    'get files
    filnam = Application.GetOpenFilename(FileFilter:="2D Table Formats (*.htm;*.xlsm;*.html),*.htm;*.xlsm;*.html", Title:="Select 2D Table", MultiSelect:=True)

    'set the array
    If IsArray(filnam) Then 'if at least one file is selected, this will be an Array

    'define j as the array
    For j = LBound(filnam) To UBound(filnam)

    'remove path and extension
    Dim s As String, a() As String, p As String
    s = filnam(j)
    a() = Split(s, "\")
    p = Split(a(UBound(a)), ".")(0)

    MsgBox "p " & p

    'check if worksheet exists
    For Each ws_check In ThisWorkbook.Worksheets()

        If ws_check.Name = p Then
            MsgBox "Its there"
            Exit Sub
            Else
        End If
   Next ws_check

   'continue code from here

然后运行代码......但由于某种原因它没有循环数组。一次只有一个文件。你能帮忙吗?

【问题讨论】:

    标签: arrays excel vba loops


    【解决方案1】:

    遵循您的代码有点困难,但这是否符合您的要求?

    我认为您将文件名存储在 p 变量中,因此我下面的代码将检查工作簿中的每个工作表,以查看它们是否与 p 变量具有相同的名称。

    Public Sub CompareWorksheetNamesToFiles()
    
        Dim file_name As String
        file_name = ActiveWorkbook.Name
    
        Dim ws_check As Worksheet
        For Each ws_check In ThisWorkbook.Worksheets()
    
            If ws_check.Name = p Then
                Debug.Print ("Do Something")
            End If
    
        Next ws_check
    
    End Sub
    

    【讨论】:

    • 谢谢。当我进入时我会检查它。 filnam 是将文件打开到数组中的东西。 Filnam(j) 调用数组然后我不得不用 p 缩短它以删除所有额外的路径信息。这会检查每个打开的文件还是只检查 p 中的一个文件?
    • 如果有意义的话,我想我需要将 p 定义为来自 filnam 的数组
    • 你的 p 似乎在一个 for 循环中,所以我想你正在遍历数组中的每个文件并用文件名填充 p 。但我可能是错的。
    • 您好,检查效果很好,谢谢,但它不是一次只检查一个数组。是不是我做错了什么?
    【解决方案2】:

    我现在已经解决了所有问题。

    现在这将打开文件位置,将它们的路径缩短为仅文件名减去扩展名,然后 ws check 根据文件名检查工作表,然后 looper 跳转到下一个。

    感谢 alwaysdata 帮助我。

    Sub sort_it_out()
    
     Dim filnam As Variant
    
     'open file locations
     filnam = Application.GetOpenFilename(FileFilter:="2D Table Formats (*.htm;*.xlsm;*.html),*.htm;*.xlsm;*.html", Title:="Select 2D Table", MultiSelect:=True)
    
     'if at least one file is selected, this will be an Array 
        If IsArray(filnam) Then 
    
        For j = LBound(filnam) To UBound(filnam)
    
     'remove pathway and extension from entire filename and path. ie C:\open.txt becomes open.
    
        Dim s As String, a() As String, p As String
        s = filnam(j)
        a() = Split(s, "\")
        p = Split(a(UBound(a)), ".")(0)
    
    
     'check if worksheet exists against p ... ie if theres a worksheet called open it will goto the next option if not it will continue through code
        For Each ws_check In ThisWorkbook.Worksheets()
    
            If ws_check.Name = p Then
                MsgBox p & " has already been transfered across. ", vbExclamation 'lets the user know this is already there.
    
                GoTo looper
                Else
            End If
       Next ws_check
    
     'do something here with the code if not found. IE MSGBOX " NOT FOUND "
    
     'jump to this point if there is a match. 
    looper:
    Next
        Else
        Exit Sub
    
    End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多