如何保持 worksheet.visible = false 并仍然获取活动表名称和单元格地址等数据

问题描述 投票:0回答:0

我在 2 列中有一个 excel 文件名列表和这些文件的路径。使用代码,我一次打开每个工作表,在打开的工作簿中搜索特定关键字,只要找到匹配项。我必须得到那个工作簿名称(已经有),匹配的找到的单元格地址,单元格中的活动工作表名称和值。

查询 1: 现在,我已经有了代码,它可以正常工作,但是当我隐藏 excel 工作表以便用户看不到它们时,ActiveSheet 不能用于获取工作表名称,简单来说,如果我使用 .visible = false 隐藏文件它不起作用,它在宏中搜索值。请帮助修复它

查询 2: 我认为在宏中粘贴值的代码不是 100% 准确的,因为我刚才提到的是工作表而不是工作簿,请帮助使其更可靠

查询 3: 如果我想搜索 10k 个文件,如何让它更快

我试过下面的代码

  Dim lastvalueoffiles As Integer  


  lastvalueoffiles = Sheet4.Range("A" & Rows.Count).End(xlUp).Row


For EachFileinPath = 2 To lastvalueoffiles

    Dim wb As Workbook
    'wb.Visible = False

    Set wb = Workbooks.Open(Sheet4.Cells(EachFileinPath, 2).Value)
   ActiveWindow.Visible = True
    
          Dim SearchString As String
          Dim SearchRange As Range, cl As Range
          Dim FirstFound As String
          Dim sh As Worksheet
 
     '''Set Search value
     SearchString = Sheet1.Cells(6, 8).Value
    
           Application.FindFormat.clear
    
           '''loop through all sheets
    
    For Each sh In ActiveWorkbook.Worksheets
        
        ''' Find first instance on sheet
        Set cl = sh.Cells.Find(What:=SearchString, After:=sh.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
        sh.Activate
        
        If Not cl Is Nothing Then
            ' if found, remember location
            FirstFound = cl.Address
            ' format found cell
            
            Do
                        
               LastValueoftheresultfound = Sheet4.Cells(EachFileinPath, ActiveSheet.Columns.Count).End(xlToLeft).Column
                   
                momo = ActiveCell.Address
                
                ActiveSheet.Select
                
                
                Sheet4.Cells(EachFileinPath, LastValueoftheresultfound + 1).Value = ActiveSheet.Name
                
                Sheet4.Cells(EachFileinPath, LastValueoftheresultfound + 2).Value = cl.Address
                
                Sheet4.Cells(EachFileinPath, LastValueoftheresultfound + 3).Value = cl.Value
                
                '''cl.Interior.ColorIndex = 3
                
                Set cl = sh.Cells.FindNext(After:=cl)
                ' repeat until back where we started
            Loop Until FirstFound = cl.Address
        End If
    Next

ActiveWorkbook.Close SaveChanges:=False

koko = Sheet1.Cells(6, 8).Value
    
Next EachFileinPath

excel vba hide
© www.soinside.com 2019 - 2024. All rights reserved.