简体   繁体   中英

Select range until cell with certain text in Excel with VBA

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

`

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM