VBA - select the row if it contains certain words

macroos

New Member
Joined
May 30, 2018
Messages
45
Hi All.

I am trying to select the rows that contains the text "#N/A" on column N.
How do I do that?
My data are on columns B through columns N.

Code:
Sub CutNA()

    Dim FirstNA As Long, LastNA As Long
    FirstNA = Columns("N").Find("#N/A", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
    LastNA = Cells(Rows.Count, "B").End(xlUp).Row
    
    Rows("B"&FirstNA&":B"&LastNA).Select
    Selection.Cut
    ...

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Your logic looks fine, kinda, not sure why you add 1 to the row that the NA is found, I guess you want to start cutting after the NA.

Also you want to use Range function, not Rows...

Code:
Range("B" & FirstNA & ":B" & LastNA).Select

Also LastNA is actually the row number of the last cell in the column that is not empty... just FYI

FirstNA is the row number after the row where the NA is found. If that is your intention then ok but the way you name your variables doesnt accurately describe to a reader what your code is doing.
 
Upvote 0
Hi All.

I am trying to select the rows that contains the text "#N/A" on column N.
How do I do that?
My data are on columns B through columns N.

Code:
Sub CutNA()

    Dim FirstNA As Long, LastNA As Long
    FirstNA = Columns("N").Find("#N/A", , xlValues, , xlRows, xlPrevious, , , False).Row + 1
    LastNA = Cells(Rows.Count, "B").End(xlUp).Row
    
    Rows("B"&FirstNA&":B"&LastNA).Select
    Selection.Cut
    ...

End Sub
First, I would note that you do not have to select the rows in order to cut them... you can cut them directly.

If those #N/A errors were generated by formulas in the cells, then use this macro...
Code:
Sub CutNA()
  Columns("E").SpecialCells([B][COLOR="#0000FF"]xlFormulas[/COLOR][/B], xlErrors).EntireRow.Cut
  ....
End Sub
But if those #N/A values are constants (either the #N/A were typed into the cells or the formulas were replaced by their values), then use this macro instead (I highlighted the difference between the macros in blue)...
Code:
Sub CutNA()
  Columns("E").SpecialCells([B][COLOR="#0000FF"]xlConstants[/COLOR][/B], xlErrors).EntireRow.Cut
  ....
End Sub
 
Last edited:
Upvote 0
Thank you both!
I am not sure my macro will do what you want... I answered what you appeared to ask in the text description, but I now note your code is doing something completely different from what you described in the text, so I am not sure what your actual needs are.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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