Macro for copying contents in active cell and searching another sheet for that copied value

MNik

New Member
Joined
Aug 30, 2022
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to put together a macro that will copy the value from column H in same row of my active cell, then search another sheet for that value. If the value is found, I would like it to highlight the entire row, if not, then I would like a message box to return "Not Found"

Here is what I have so far.

VBA Code:
Sub SearchTest()


Rng = ActiveCell.Value

Application.ScreenUpdating = False


    Sheets("Open but Delinquent").Select
    Columns("H:H").Select
        Cells.Find(What:=Rng, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
   
      
     ActiveCell.Select
        Selection.EntireRow.Select
           
   


Application.ScreenUpdating = True


End Sub

Currently, it will successfully run if it finds a match. If it does not find a match it provides a "Runtime error '91: Object variable or With block variable not set" if the value is not found on the second sheet. Is there a way for it to search using the value in column H based on the row I am in without needing to be in that cell? How can I re-write to stop providing an error when a matching value is not found?

Thanks in advance!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
try this:
VBA Code:
Sub SearchTest()

rowno = ActiveCell.Row

Rng = Range("H" & rowno)


Application.ScreenUpdating = False


    Sheets("Open but Delinquent").Select
    Columns("H:H").Select
On Error GoTo errorhandle:
        Cells.Find(What:=Rng, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
   
      
     ActiveCell.Select
        Selection.EntireRow.Select
           
   


Application.ScreenUpdating = True

Exit Sub
errorhandle:
MsgBox (" Not Found")
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you for you help! These additions helped it worked nearly flawlessly. I did make a few tweaks and I'm 99% of the way there.

One quirk that I noticed is that when a matching value is found, it does highlight the row but it doesn't bring me to the highlighted row, meaning you may have to scroll to find it. Any tips on how I could tweak it to take the me to the highlighted row?

Here is my updated version:

VBA Code:
Sub SearchTest()

'
'Copy cell "E" in current row and search for value in OBD
'

rowno = ActiveCell.Row

rng = Range("E" & rowno)


Application.ScreenUpdating = False


    Sheets("Open but Delinquent").Select
    Columns("H:H").Select
On Error GoTo errorhandle:
        Cells.Find(What:=rng, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

      
    ActiveCell.Select
    Selection.EntireRow.Select
   
   
Application.ScreenUpdating = True

'
'If not found
'

Exit Sub
errorhandle:
MsgBox ("Not Found")
Sheets("Completed Shipments").Select


Application.ScreenUpdating = True


End Sub
 
Upvote 0
try deleting the:
VBA Code:
Application.ScreenUpdating = False
and the
VBA Code:
Application.ScreenUpdating = TRUE
I never use these, I find if you write good fast macros it is really uncessary and can cause problems, specially when the macro crashes out. ( you are left with screen NOT updating!!)
 
Upvote 0
Solution
Yep, that did the trick. I'm new to working with macros, so I appreciate the help and tip immensely. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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