VBA Code to Execute When Cell Value is Changed

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance. I would like to run the Macro "Find_Vals" when I change an entry in Cell B7, but nothing happens when I do make a change. Please note that it has a pick list (data validation) in B7. The macro "Find_Vals" works when I hit run, but as indicated, I would like it to be automatic when I change cell B7.
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$7" Then
        Call Find_Vals
    End If
End Sub


'***************************************************************************************************************
Private Sub Find_Vals()

    '__________________________________________________________________________________________________________
    'Dimensioning
  
        Dim LastRow As Long
  
        Dim WS As Worksheet
  
        Dim RngB As Range
        Dim FndRng As Range
  
        Dim FndTckVal As String
        Dim FndCNVal As String


    '__________________________________________________________________________________________________________
    'Code
          
        Set WS = Sheets("Our.Summary")
      
        With Sheets("Our.Summary")
            LastRow = .Cells.Find(What:="*", After:=Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            Set RngB = .Range("B9:B" & LastRow)
        End With
      
      
        'Move the cursor to the cell
            With WS
              
                FndTckVal = .Range("B7")
                  
                Set FndRng = RngB.Find(FndTckVal, LookIn:=xlValues)
          
                If Not FndRng Is Nothing Then
                    FndRng.Select
                Else
                    MsgBox FndTckVal & " is not found!"
                End If
      
            End With



End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
For more help I would need to know in words what your attempting to do.
I do not like to read scripts to see what user is trying to do when the script does not do what you want.
 
Upvote 0
For more help I would need to know in words what your attempting to do.
I do not like to read scripts to see what user is trying to do when the script does not do what you want.
I have a list of values in a sheet within Range("B9"B" & LastRow). When I change the value in cell B7, I would like it to go to the cell within Range("B9"B" & LastRow) that contains the value I entered into B7. When I run the Macro manually it works, but when I make the change to cell B7, the macro "Worksheet_Change(ByVal Target As Range)" does not execute the Macro "Find_Vals()".
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/30/2021  7:43:39 PM  EDT
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$B$7" Then
Dim ans As String
ans = Target.Value
Dim SearchRange As Range
SearchString = ans
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B9:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
SearchRange.Select

End If
End Sub
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/30/2021  7:43:39 PM  EDT
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$B$7" Then
Dim ans As String
ans = Target.Value
Dim SearchRange As Range
SearchString = ans
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B9:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
SearchRange.Select

End If
End Sub
Thanks My Aswer Is This for your assistance. It still does not work. Not sure what the issue is.
 
Upvote 0
So when you ran my script what did it do?
You said search for value in Range("B9") to last row with data in Column B and then select that cell.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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