나는 각각 많은 시트를 포함하는 여러 개의 거대한 Excel 파일을 가지고 있습니다. 각 문자열에서 특정 문자열을 수동으로 검색하고 해당 문자열이 어딘가에 있는지 확인해야 합니다.
다음 코드를 찾았습니다.여기지금까지는 작동합니다.
Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
'Change as desired
strPath = "c:\MyFolder"
strSearch = "Specific text"
Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "\" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4) = rFound.Value
Else
Exit Do
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
하지만 문자열을 검색하려면 매번 코드를 열고 경로와 검색어를 변경해야 합니다.
strPath = "c:\MyFolder"
strSearch = "Specific text"
코드를 열고 매번 경로를 변경하는 대신 첨부된 사진에 검색 필드나 이와 유사한 것이 있으면 좋겠습니다. 가능합니까?
아니면 결과가 포함된 시트의 두 셀에 코드를 열지 않고도 경로와 검색어를 입력할 수 있습니까?
답변1
strPath = ActiveSheet.Range("A1").value
strSearch = ActiveSheet.Range("B1").value
버튼을 만들려면 개발자 리본을 사용하여 양식 컨트롤을 추가하세요.