Find function in VBA

manoj_arsul

Board Regular
Joined
Jun 27, 2018
Messages
61
Dear All ,

Can you please Help me to find function in VBA.

Conditions:
Enter any value in cell A10 - Sheet 1 (already fixed button - for click)
Find that value in each workbook like Sheet 3,Sheet 4, sheet 5, sheet 6......etc .
eg. if value is found in sheet 3 and sheet 4 then out put will be in sheet 2
ie. copied entire that row (multiple rows- if the value found multiple time) and 1st row of the sheet 3 & sheet 4 and paste in Sheet 2.

Eg. I am searching word America. - (clicked on Button)

Sheet 3.
CountryStudent CountPassedFailed
China1000800200
America50048020
Japan50047030

Sheet 4.
CapitalCountryStudent CountFailedPass
TokyoJapan40050350
WashingtonAmerica1001585
LondonEngland700100600
CapetownSouth Africa50050450
WashingtonAmerica40020380

Output in Sheet 2.

CountryStudent CountPassedFailed
America50048020
CapitalCountryStudent CountFailedPass
WashingtonAmerica1001585
WashingtonAmerica40020380


If the Value/word is not found the Popup Mssg "Enter Value not present in File etc"


Thanks in Advance!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about:

VBA Code:
Sub find_multiple_rows()
  Dim sh As Worksheet, i As Long, n As Long, iRow As Long
  Dim c As Range, r As Range, f As Range, cell As Long
  '
  Set sh = Sheets("Sheet2")
  sh.Cells.ClearContents
  n = 2
  '
  For i = 3 To Sheets.Count
    Set r = Sheets(i).Cells
    Set f = r.Find(Sheets("Sheet1").Range("A10"), , xlValues, xlPart)
    If Not f Is Nothing Then
      cell = f.Row
      iRow = 0
      Do
        If f.Row <> iRow Then
          f.EntireRow.Copy sh.Range("A" & n)
          n = n + 1
          iRow = f.Row
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Row <> cell
    End If
  Next i
  If iRow = 0 Then MsgBox "Enter Value not present in File"
End Sub
 
Upvote 0
@DanteAmor : But it is not copied first row like
CountryStudent Coun
CapitalCountry

Is it possible, at same time copied first row if data text/value found in that sheet only.
 
Upvote 0
copied first row
Try this
VBA Code:
Sub find_multiple_rows()
  Dim sh As Worksheet, i As Long, n As Long, iRow As Long
  Dim c As Range, r As Range, f As Range, cell As Long
  '
  Set sh = Sheets("Sheet2")
  sh.Cells.ClearContents
  n = 1
  '
  For i = 3 To Sheets.Count
    Set r = Sheets(i).Cells
    Set f = r.Find(Sheets("Sheet1").Range("A10"), , xlValues, xlPart)
    If Not f Is Nothing Then
      cell = f.Row
      iRow = 0
      Sheets(i).Rows(1).Copy sh.Range("A" & n)
      n = n + 1
      Do
        If f.Row <> iRow Then
          f.EntireRow.Copy sh.Range("A" & n)
          n = n + 1
          iRow = f.Row
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Row <> cell
    End If
  Next i
  If iRow = 0 Then MsgBox "Enter Value not present in File"
End Sub
 
Upvote 0
@DanteAmor : Sorry but its not getting accurate result. I want first row upside from that sheet where we have found the value/ text.
Like we found America in sheet4 then in sheet2 if result amrica ... then in upside row we have to copy paste First row (Country, Student count...)
 
Upvote 0
That is what the macro does:
Libro6
AB
9
10America
11
Sheet1


Libro6
ABCD
1CountryStudent CountPassedFailed
2China1000800200
3America50048020
4Japan50047030
Sheet3

Libro6
ABCDE
1CapitalCountryStudent CountFailedPass
2TokyoJapan40050350
3WashingtonAmerica1001585
4LondonEngland700100600
5CapetownSouth Africa50050450
6WashingtonAmerica40020380
Sheet4


Result:
Libro6
ABCDE
1CountryStudent CountPassedFailed
2America50048020
3CapitalCountryStudent CountFailedPass
4WashingtonAmerica1001585
5WashingtonAmerica40020380
Sheet2


What does the macro do, it gives you more records?
then change this line:
Set f = r.Find(Sheets("Sheet1").Range("A10"), , xlValues, xlPart)
For this:
Set f = r.Find(Sheets("Sheet1").Range("A10"), , xlValues, xlWhole)
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top