vba select cell with same value as another cell *

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
325
Office Version
  1. 365
Platform
  1. Windows
i HAVE A NUMBER in cell AA10. I want to find the same number in range AI12:AO57 then offset 1 cell right. if that cell is "" then 1 else "". I want to do this without selecting cells if possible.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe...

VBA Code:
Sub bluefeather()
    Dim FndRng As Range
    
    With Range("AI12:AO57")
        Set FndRng = .Find(Range("AA10").Value, .Cells(.Cells.Count), xlValues, , xlByRows, xlNext)
    End With

    If Not FndRng Is Nothing Then
        If FndRng.Offset(, 1) = "" Then
            FndRng.Offset(, 1).Value = 1
        Else
            FndRng.Offset(, 1).Value = ""
        End If
    End If

End Sub
 
Upvote 0
Without sample data it is a bit hard to know just what might be in the range, whether there could be duplicates etc, but you could also test this.

VBA Code:
Sub bluefeather_v2()
  Dim FndRng As Range
  
  Set FndRng = Range("AI12:AO57").Find(What:=Range("AA10").Value, LookAt:=xlWhole)
  If Not FndRng Is Nothing Then FndRng.Offset(, 1).Value = Mid(1, 1, -(FndRng.Offset(, 1).Value = ""))
End Sub
 
Upvote 0
Solution
If there were duplicates and you wanted the offset to apply to all the matching entries then my code adjusted would be as per the below (if you just wanted the 1st matching entry then it would still be my 1st code, Peter's would need adjusting as it would pick the 2nd matching entry if the 1st matching entry was in AI12)

VBA Code:
Sub bluefeather2()
    Dim FndRng As Range, firstaddress As String
  
    Application.ScreenUpdating = False
  
    With Range("AI12:AO57")
        Set FndRng = .Find(Range("AA10").Value, .Cells(.Cells.Count), xlValues, , xlByRows, xlNext)
      
        If Not FndRng Is Nothing Then
            firstaddress = FndRng.Address
          
            Do
              
                If FndRng.Offset(, 1) = "" Then
                    FndRng.Offset(, 1).Value = 1
                Else
                    FndRng.Offset(, 1).Value = ""
                End If
                Set FndRng = .FindNext(FndRng)
             
            Loop While Not FndRng.Address = firstaddress
      
        End If
  
    End With

End Sub
 
Last edited:
Upvote 0
If there were duplicates and you wanted ...
I will wait to see if such a circumstance can happen and, if so, what action should be taken before possibly modifying my code.


VBA Code:
If FndRng Is Nothing Then Exit Do
Mark, that line is superfluous. Inside the Do Loop, FndRng will never be Nothing
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,086
Members
449,206
Latest member
ralemanygarcia

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