1

Trying to select header / first row and data under it from columns C to O Until the first cell in column C has the word "Estimated" I came up with this VBA code based on another answer on stackoverflow, but it doesn't seem to be working

All help is deeply appreciated!!

Range("C1").Select
For i = 1 To 9999
If ISNUMBER(SEARCH(ActiveCell.Offset(0, i), "Estimated")) = TRUE Then Exit For
If ISNUMBER(SEARCH(ActiveCell.Offset(0, i), "Estimated")) = FALSE Then Exit For
Next
If ISNUMBER(SEARCH(ActiveCell.Offset(0, i), "Estimated")) = TRUE Then             
    Range(Cells(ActiveCell.Row, 15), Cells(ActiveCell.Row, ActiveCell.Column + i - 1)).Select

`

David Zemens
  • 53,033
  • 11
  • 81
  • 130
Daren S
  • 11
  • 1
  • 2
  • 1
    why not use the `Range.Find` method? Also, you [probably don't need to `Select` anything or use the `Activate/ActiveCell`](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) (and it should generally be avoided). – David Zemens Jul 12 '18 at 20:58

2 Answers2

1

Try,

range(cells(1, "C"), cells(application.match("Estimated", columns(3), 0)-1, "O")).select

Not sure from your narrative whether the row containing "Estimated" was to be included or not. The -1 tells it not to include the "Estimated" row.

  • Thanks for the response. it is supposed to exclude it. When I ran it, I got "type mismatch" as an error. – Daren S Jul 18 '18 at 14:06
0

you could use

Range("O1", Columns(3).Find("Estimated", , xlValues, xlPart)).Select ' to include row with "Estimated" in column C

or

Range("O1", Columns(3).Find("Estimated", , xlValues, xlPart).offset(-1)).Select ' to exclude row with "Estimated" in column C
DisplayName
  • 13,283
  • 2
  • 11
  • 19