find text string on worksheet

Rasberry

Board Regular
Joined
Aug 11, 2002
Messages
195
Office Version
  1. 365
I am looking at past questions, but can't figure out how to structure my code. I want to look through the worksheet for a specific test string: ROIC. Once I find it, then I want to copy the cell below it and paste it in another workbook.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Here is some code that should do that. Adjust your workbook/sheet/range references as necessary.
Code:
Sub Macro1()

'   Error handling if it can't find value
    On Error GoTo err_chk
    
'   Find value and copy cell under it
    Cells.Find(What:="ROIC", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Offset(1, 0).Copy
        
'   Go to new location and paste
    Windows("Book2.xls").Activate
    Sheets("Sheet2").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    On Error GoTo 0
    Exit Sub
    
'   Error handling
err_chk:
    If Err.Number = 91 Then
        MsgBox "Cannot find the value ROIC on active sheet"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
    
End Sub
 
Upvote 0
Hello Rasberry,
Here's something to give you some ideas.
Assumes:
1) there will only be one instance of what you're searching for in the worksheet
- or you just want to find the first instance of it.
2) The code will be run with the sheet to be searched as the active sheet.
3) The string to be searched for will not always be 'ROIC'.
Code:
Sub FindAndCopy()
Dim ValueToFind As String, fCell As Range
ValueToFind = InputBox("Enter the value to search for.", "Search for . . ")
Set fCell = Cells.Find(ValueToFind, lookat:=xlWhole)

If fCell Is Nothing Then
  MsgBox "'" & ValueToFind & "' can not be found in this sheet.", vbInformation, "Search Results"
Else
  fCell(2).Copy 'Your paste destination goes here.
End If

Application.CutCopyMode = False
End Sub


[EDIT:]
I see that Joe has already got something similar but there are enough differences
between these two suggestions to warrent posting this anyway.
 
Upvote 0
Wonderful--that works mostly. I changed it a little bit. Perhaps I made it goof up. It works on the first 3 files, then on the next file, it doesn't go to the err_chk when it can't find ROIC. I'm wondering if the Find 'after activecell' is the problem. Here is the code:

Columns("A:A").Select
Selection.Find(What:="end", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
NumberOfFiles = ActiveCell.Row
Range("A4").Select
For I = 1 To NumberOfFiles - 1
DataGathering:
ActiveCell.Offset(1, 0).Select
Application.DisplayAlerts = False
Filename = ActiveCell.Value
Workbooks.Open Filename:="G:\analysis\MASTER\A\" & Filename
' Error handling if it can't find value
On Error GoTo err_chk

' Find value and copy cell under it
Cells.Find(What:="ROIC", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Offset(1, 0).Copy

' Go to new location and paste
Windows("Credit_Statistics_Report.xls").Activate
Sheets("StatSheet").Activate
ActiveCell.Select
ActiveCell.Offset(0, 8).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.Offset(0, -8).Select
Workbooks(Filename).Activate
ActiveWorkbook.Close
Next I
Application.DisplayAlerts = True
' Error handling
err_chk:
If Err.Number = 91 Then
MsgBox "Cannot find the value ROIC on active sheet"
Else
MsgBox Err.Number & ": " & Err.Description
End If
ActiveWorkbook.Close
Windows("Credit_Statistics_Report.xls").Activate
GoTo DataGathering
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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