英文:
how to keep worksheet.visible = false and still get data like activesheet name and cell address
问题
Query 1:
现在,已经有代码,它正常工作,但当我隐藏Excel工作表以使用户看不到它们时,ActiveSheet 无法使用,代码不会进入打开的工作簿,而是在宏文件中搜索。简单来说,如果我使用.visible = false 隐藏文件,它不起作用,它在宏中搜索。请帮助修复它。
Query 2:
我认为将值粘贴到宏中的代码不是100%准确,因为我只提到了工作表而没有提到工作簿,请帮助加固它。
Query 3:
如果我要搜索10,000个文件,如何使它更快?
我已经尝试了下面的代码。
英文:
I have a list of excel file names and those file's path in 2 columns. Using the code , I open each worksheet at a time , search for a specific keyword in opened workbook , wherever match is found. I have to get that workbook name (already have) , matched found cell address , that active sheet name and value in cell.
Query 1:
Now , already have code with me it works properly but when I hide the excel sheets so that user can't see them , the ActiveSheet can't be used code does not go to opened workbook instead it search in macro file , In simple words if I hide files using .visible = false it does not work , it search values in macro. Please help to fix it
Query 2:
I think the code to paste value in macro is not 100% accurate , because I have just mentioned sheets not workbook , please help to make it solid
Query 3:
How to make it faster if I want to search 10k files
I have tried the below code
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
答案1
得分: 1
If I understand you correctly....
Before running the macro, sheet4 looks something like this:<br>
test1.xlsx sheet1 cell H6 value is aaa<br>
test1.xlsx sheet2 cell A1 value is aaa<br>
test1.xlsx sheet3 cell B1:B2 value is aaa<br>
test1.xlsx sheet4 cell C1:C3 value is aaa<br>
test2.xlsx, the cells are the same, but the value is bbb<br>
test3.xlsx, the cells are the same, but the value is ccc.
Expected result (please ignore the fill color)<br>
Sub test()
Dim shTrg As Worksheet, rg As Range, FL As String, OpenedWB As Workbook
Dim SearchString As String, sh As Worksheet, cl As Range
Application.ScreenUpdating = False
Set shTrg = ThisWorkbook.Sheets("Sheet4") 'change if needed
Set rg = shTrg.Range("B2", shTrg.Range("B" & Rows.Count).End(xlUp)) 'change if needed
Application.FindFormat.Clear
For Each FL In rg
Set OpenedWB = Workbooks.Open(FL)
SearchString = OpenedWB.Sheets("Sheet1").Cells(6, 8).Value
For Each sh In OpenedWB.Worksheets
Set cl = sh.Cells.Find(What:=SearchString, After:=sh.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then
FirstFoundAddress = cl.Address
Do
With shTrg.Cells(FL.Row, shTrg.Columns.Count).End(xlToLeft).Offset(0, 1)
.Resize(1, 3).Value = Array(sh.Name, cl.Address, cl.Value)
End With
Set cl = sh.Cells.FindNext(cl)
Loop Until FirstFoundAddress = cl.Address
End If
Next sh
OpenedWB.Close SaveChanges:=False
Next FL
Application.ScreenUpdating = False
End Sub
Please note that the sub may throw an error if the count of the found cells in the OpenedWorkbook exceeds the last column of the shTrg.
英文:
If I understand you correctly....
Before running the macro, sheet4 look something like this:<br>
test1.xlsx sheet1 cell H6 value is aaa<br>
test1.xlsx sheet2 cell A1 value is aaa<br>
test1.xlsx sheet3 cell B1:B2 value is aaa<br>
test1.xlsx sheet4 cell C1:C3 value is aaa<br>
test2.xlsx, the cells are the same, but value is bbb<br>
test3.xlsx, the cells are the same, but value is ccc.
Expected result (please ignore the fill color)<br>
Sub test()
Dim shTrg As Worksheet, rg As Range, FL As String, OpenedWB As Workbook
Dim SearchString As String, sh As Worksheet, cl As Range
Application.ScreenUpdating = False
Set shTrg = ThisWorkbook.Sheets("Sheet4") 'change if needed
Set rg = shTrg.Range("B2", shTrg.Range("B" & Rows.Count).End(xlUp)) 'change if needed
Application.FindFormat.Clear
For Each FL In rg
Set OpenedWB = Workbooks.Open(FL)
SearchString = OpenedWB.Sheets("Sheet1").Cells(6, 8).Value
For Each sh In OpenedWB.Worksheets
Set cl = sh.Cells.Find(What:=SearchString, After:=sh.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then
FirstFoundAddress = cl.Address
Do
With shTrg.Cells(FL.Row, shTrg.Columns.Count).End(xlToLeft).Offset(0, 1)
.Resize(1, 3).Value = Array(sh.Name, cl.Address, cl.Value)
End With
Set cl = sh.Cells.FindNext(cl)
Loop Until FirstFoundAddress = cl.Address
End If
Next sh
OpenedWB.Close SaveChanges:=False
Next FL
Application.ScreenUpdating = False
End Sub
Please note that the sub may throw an error if the count of the found cells in the OpenedWorkbook exceed than the last column of the shTrg.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论