VBA for checking if value exists on another sheet

kottn

New Member
Joined
Mar 31, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm new to VBA and want to do a macro that checks if value in C2 on sheet2 exists in column B at sheet1
If it exists i want a text added to that row on sheet1 at column L

If it does not exist i want an error message on sheet2 E3

Any advice?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this
VBA Code:
Sub CheckValues()

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim valueToFind As Variant
    Dim searchRange As Range
    Dim foundCell As Range
    
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")
    
    ' Get the value to find from Sheet2, Cell C2
    valueToFind = ws2.Range("C2").Value
    
    ' Set the search range to Sheet1, Column B
    Set searchRange = ws1.Range("B:B")
    
    ' Search for the value in the search range
    Set foundCell = searchRange.Find(what:=valueToFind, LookIn:=xlValues, lookat:=xlWhole)
    
    If foundCell Is Nothing Then
        ' If the value is not found, display an error message on Sheet2, Cell E3
        ws2.Range("E3").Value = "Value not found in Sheet1"
    Else
        ' If the value is found, add text to the corresponding row in Sheet1, Column L
        ws1.Range("L" & foundCell.Row).Value = "Text added"
    End If
    
End Sub
 
Upvote 0
Solution
Try this
VBA Code:
Sub CheckValues()

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim valueToFind As Variant
    Dim searchRange As Range
    Dim foundCell As Range
   
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")
   
    ' Get the value to find from Sheet2, Cell C2
    valueToFind = ws2.Range("C2").Value
   
    ' Set the search range to Sheet1, Column B
    Set searchRange = ws1.Range("B:B")
   
    ' Search for the value in the search range
    Set foundCell = searchRange.Find(what:=valueToFind, LookIn:=xlValues, lookat:=xlWhole)
   
    If foundCell Is Nothing Then
        ' If the value is not found, display an error message on Sheet2, Cell E3
        ws2.Range("E3").Value = "Value not found in Sheet1"
    Else
        ' If the value is found, add text to the corresponding row in Sheet1, Column L
        ws1.Range("L" & foundCell.Row).Value = "Text added"
    End If
   
End Sub
Works brilliantly, thank you so much ! :)
 
Upvote 0
If you received at least one reply answering your question and solving your problem,
then please remember to click the "Mark as solution" button - - right next to the best post which is answering your question.
 
Upvote 0

Forum statistics

Threads
1,223,727
Messages
6,174,139
Members
452,546
Latest member
Rafafa

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