find selects almost entire worksheet instead of just column D

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
as per title.
I have similar code in different part of my workbook and it works fine, but as soon as i started playign with offsets I have this problem.
VBA Code:
    Sub TEST()
    ActiveSheet.Columns("D:D").Select
Set cell = Selection.Find(what:="legs", After:=ActiveCell, LookIn:=xlFormulas, _
        lookat:=xlPart, SearchOrder:=xlByRows, searchdirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If Not IsEmpty(cell.Offset(, 2)) And cell.Offset(, 2) <> 0 Then
    MsgBox "no legs"
Else
    MsgBox "yes legs"
End If

    End Sub
I assume i need some more stuff to make it work.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
just after i try to execute this:
1667389262449.png
If cell.Offset(, 2) <> "" And cell.Offset(, 2) <> 0 Then
 
Upvote 0
That surprises me, as judging by your image that will be looking at col F which will be empty as it's merged with col E.
Best thing you can do is get rid off all the merged cells, they are an abomination & will give you nothing but trouble.
 
Upvote 0
I got it.
It was all about the text it was trying to find it didnt match to anything. However if that was thew case I assume it shodyul still run the chekc and just throw msgbox no legs.
 
Upvote 0
This If cell.Offset(, 2) <> "" And cell.Offset(, 2) <> 0 Then will never be true & so should just take you to the Else.
If you are getting an error on that line, you will have to un-merge the cells to find out why.
 
Upvote 0
changed If cell.Offset(, 2) <> "" And cell.Offset(, 2) <> 0 Then to this: If cell.Offset(, 4) <> "" And cell.Offset(, 4) <> 0 Then
so it now actually goes for collumn H
 
Upvote 0
It was all about the text it was trying to find it didnt match to anything. However if that was thew case I assume it shodyul still run the chekc and just throw msgbox no legs.
Try adding the cell is nothing test:

Rich (BB code):
   Sub TEST()
    Dim cell As Range
    ActiveSheet.Columns("D:D").Select
    Set cell = Selection.Find(what:="legs", After:=ActiveCell, LookIn:=xlFormulas, _
            lookat:=xlPart, SearchOrder:=xlByRows, searchdirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
            
    If cell Is Nothing Then
        MsgBox "no legs"
        Exit Sub
    End If

    If Not IsEmpty(cell.Offset(, 2)) And cell.Offset(, 2) <> 0 Then
        MsgBox "no legs"
    Else
        MsgBox "yes legs"
    End If

    End Sub
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,320
Members
449,218
Latest member
Excel Master

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