Макрос поиска и вывода строк, содержащих значение поиска - VBA
Формулировка задачи:
Здравствуйте!
Есть макрос для поиска значения из ячейки А1 по всему листу и копированием строк из всех листов, содержащих это значение.
Но есть и проблема: макрос поиска ищет только цифровые значения из указанной ячейки. Текстовые или смешанные не находит.
Если знаете как подправить, помогите плз!!!
Вот код:
Листинг программы
- Sub SearchPN()
- Dim iCopi As Range
- Dim iPast As Range
- AR = Range("A1") 'значение для поиска
- SZ = 15
- PS = Cells(Rows.Count, 1).End(xlUp).Row
- Rows("15:" & PS).Delete Shift:=xlUp
- For I = 3 To 9
- IL = Cells(I, 5) 'номер листа
- KL = Cells(I, 6) 'номер столбца
- Cells(SZ, 2) = IL
- SZ = SZ + 1
- Set iCopi = Worksheets(IL).Range("A1:AD1")
- Set iPast = Worksheets("SEARCH").Range("A" & SZ)
- iCopi.Copy iPast
- SZ = SZ + 1
- PS = Sheets(IL).Cells(Rows.Count, KL).End(xlUp).Row
- For J = 2 To PS
- R = Val(Sheets(IL).Cells(J, KL))
- If Val(Sheets(IL).Cells(J, KL)) = AR Then
- Set iCopi = Worksheets(IL).Range("A" & J & ":AD" & J)
- Set iPast = Worksheets("SEARCH").Range("A" & SZ)
- iCopi.Copy iPast
- SZ = SZ + 1
- End If
- Next J
- SZ = SZ + 1
- Next I
- End Sub
- Sub Add_line()
- '
- ' Add_line Macro
- '
- ' Keyboard Shortcut: Ctrl+q
- '
- With ThisWorkbook.ActiveSheet
- Set iDiapazon = .UsedRange
- With iDiapazon
- nREnd = .Row + .Rows.Count - 1
- nCEnd = .Column + .Columns.Count - 1
- End With
- Set iDiapazon = Nothing
- If nREnd < 3 Then: MsgBox "ГЌГҐ ïðîâåäåГ*Г® Г*ГЁ îäГ*îé îïåðГ*öèè.", vbInformation + vbOKOnly, "ÑîîáùåГ*ГЁГҐ ñèñòåìû": Exit Sub
- ' MsgBox " - ñòðîêГ* " & nREnd & Chr(10) & " - ñòîëáåö " & nCEnd, vbInformation + vbOKOnly, "ГЉГ°Г*Г©Г*ГЁГҐ:"
- Rows(nREnd + 1).Insert Shift:=xlUp, CopyOrigin:=xlFormatFromLeftOrAbove
- Range(.Cells(nREnd, 1), .Cells(nREnd, nCEnd)).Copy
- Range(.Cells(nREnd + 1, 1), .Cells(nREnd + 1, 1)).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
- SkipBlanks:=False, Transpose:=False
- End With
- End Sub
Решение задачи: «Макрос поиска и вывода строк, содержащих значение поиска»
textual
Листинг программы
- Option Explicit
- Sub Ïîèñê_ГўГ®_ГўГ±ГҐГµ_ГґГ*éëГ*Гµ()
- Dim iShtName$, iPath$, iFileName$, firstAddress$
- Dim iSheet As Worksheet, iFoundSht As Worksheet
- Dim iTempWB As Workbook, iBazaWB As Workbook
- Dim TextToFind As Variant, iFoundRng As Range
- Dim FD As FileDialog, iLastRow&
- Dim FoundAny As Boolean
- TextToFind = Application.InputBox("Ââåäèòå ГІГҐГЄГ±ГІ äëÿ ïîèñêГ*:", "Ïîèñê")
- If TextToFind = "" Or TextToFind = False Then Exit Sub
- TextToFind = Trim(TextToFind)
- Set FD = Application.FileDialog(msoFileDialogFilePicker)
- With FD
- .AllowMultiSelect = False
- .Title = "ÓêГ*æèòå ëþáîé ГґГ*éë Гў ГЇГ*ГЇГЄГҐ"
- .ButtonName = "ÂûáðГ*ГІГј ГЇГ*ГЇГЄГі"
- If .Show = False Then Exit Sub Else iPath = Mid(.SelectedItems(1), 1, InStrRev(.SelectedItems(1), "\"))
- End With
- Set FD = Nothing
- Workbooks.Add
- Sheets.Add.Name = "Ïîèñê"
- Set iFoundSht = ActiveSheet
- iFoundSht.Cells(1, 1) = "Èùåì: " & TextToFind
- iFoundSht.Cells(1, 1).Font.Bold = True
- With Application
- .ScreenUpdating = False
- .Calculation = xlManual
- .StatusBar = "Èä¸ò ïîèñê..."
- .ShowWindowsInTaskbar = False
- iFileName = Dir(iPath & "*.xls")
- Do While iFileName$ <> ""
- Set iTempWB = Workbooks.Open(Filename:=iPath & iFileName, UpdateLinks:=False, ReadOnly:=True)
- For Each iSheet In iTempWB.Sheets
- If iSheet.FilterMode = True Then iSheet.ShowAllData
- Set iFoundRng = iSheet.Cells.Find(What:=TextToFind, LookIn:=xlFormulas, LookAt:=xlPart)
- If Not iFoundRng Is Nothing Then
- FoundAny = True
- firstAddress = iFoundRng.Address
- Do
- With iFoundSht
- iLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
- If iLastRow = 1 Then iLastRow = 2
- If iShtName <> iSheet.Name Then 'åñëè Г*îâûé ГґГ*éë
- With .Cells(iLastRow + 2, 1)
- .Value = "Г”Г*éë: " & iTempWB.Name & ", Ëèñò: " & iSheet.Name
- .Font.Bold = True
- End With
- End If
- iFoundRng.EntireRow.Copy Destination:=.Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1) 'êîïèðóåì âñþ ñòðîêó
- iShtName = iSheet.Name
- End With
- Set iFoundRng = iSheet.Cells.FindNext(iFoundRng)
- Loop While iFoundRng.Address <> firstAddress
- Else
- End If
- Next
- iTempWB.Close SaveChanges:=False
- iFileName = Dir
- Loop
- .StatusBar = False
- .ShowWindowsInTaskbar = True
- .EnableEvents = True
- .Calculation = xlCalculationAutomatic
- .ScreenUpdating = True
- End With
- If FoundAny = False Then
- MsgBox "Г’ГҐГЄГ±ГІ '" & TextToFind & "' Г*ГЁ Гў îäГ*îì ГЁГ§ ГґГ*éëîâ Гў ГЇГ*ГЇГЄГҐ:" & Chr(10) & iPath & Chr(10) & " Г*ГҐ áûë Г*Г*éäåГ*!", 48, "ГЋГІГ·ВёГІ"
- iFoundSht.Parent.Close SaveChanges:=False
- Exit Sub
- End If
- MsgBox "Ïîèñê " & TextToFind & " Г§Г*âåðø¸Г*!", 64, "Ïîèñê"
- End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д