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

前端 未结 2 1698
盖世英雄少女心
盖世英雄少女心 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")
    
    0 讨论(0)
  • 2020-12-21 02:50

    Yes using Option Explicit is a good habit. Using .Select however is not :) it reduces the speed of the code. Also fully justify sheet names else the code will always run for the Activesheet which might not be what you actually wanted.

    Is this what you are trying?

    Option Explicit
    
    Sub Sample()
        Dim lastRow As Long, i As Long
        Dim CopyRange As Range
    
        '~~> Change Sheet1 to relevant sheet name
        With Sheets("Sheet1")
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            For i = 2 To lastRow
                If Len(Trim(.Range("A" & i).Value)) <> 0 Then
                    If CopyRange Is Nothing Then
                        Set CopyRange = .Rows(i)
                    Else
                        Set CopyRange = Union(CopyRange, .Rows(i))
                    End If
                Else
                    Exit For
                End If
            Next
    
            If Not CopyRange Is Nothing Then
                '~~> Change Sheet2 to relevant sheet name
                CopyRange.Copy Sheets("Sheet2").Rows(1)
            End If
        End With
    End Sub
    

    NOTE

    If if you have data from Row 2 till Row 10 and row 11 is blank and then you have data again from Row 12 then the above code will only copy data from Row 2 till Row 10

    If you want to copy all rows which have data then use this code.

    Option Explicit
    
    Sub Sample()
        Dim lastRow As Long, i As Long
        Dim CopyRange As Range
    
        '~~> Change Sheet1 to relevant sheet name
        With Sheets("Sheet1")
            lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            For i = 2 To lastRow
                If Len(Trim(.Range("A" & i).Value)) <> 0 Then
                    If CopyRange Is Nothing Then
                        Set CopyRange = .Rows(i)
                    Else
                        Set CopyRange = Union(CopyRange, .Rows(i))
                    End If
                End If
            Next
    
            If Not CopyRange Is Nothing Then
                '~~> Change Sheet2 to relevant sheet name
                CopyRange.Copy Sheets("Sheet2").Rows(1)
            End If
        End With
    End Sub
    

    Hope this is what you wanted?

    Sid

    0 讨论(0)
提交回复
热议问题