Excel VBA, How to select rows based on data in a column?

前端 未结 2 1718
盖世英雄少女心
盖世英雄少女心 2020-12-21 01:50
Sub SelectAllReleventText()
Do While Range(“A1”).Offset(1, 6) <> Empty
Rows(ActiveCell.Row).Select
ActiveCell.Offset(1, 0).Select
Loop
End Sub
<
2条回答
  •  难免孤独
    2020-12-21 02:45

    The easiest way to do it is to use the End method, which is gives you the cell that you reach by pressing the end key and then a direction when you're on a cell (in this case B6). This won't give you what you expect if B6 or B7 is empty, though.

    Dim start_cell As Range
    Set start_cell = Range("[Workbook1.xlsx]Sheet1!B6")
    Range(start_cell, start_cell.End(xlDown)).Copy Range("[Workbook2.xlsx]Sheet1!A2")
    

    If you can't use End, then you would have to use a loop.

    Dim start_cell As Range, end_cell As Range
    
    Set start_cell = Range("[Workbook1.xlsx]Sheet1!B6")
    Set end_cell = start_cell
    
    Do Until IsEmpty(end_cell.Offset(1, 0))
        Set end_cell = end_cell.Offset(1, 0)
    Loop
    
    Range(start_cell, end_cell).Copy Range("[Workbook2.xlsx]Sheet1!A2")
    

提交回复
热议问题