0
votes

is it possible to select cells in excel that are only above cells with data (the cell below the one that needs to be selected needs to have data)? if every cell in a column has data then only the top cell should be selected. if there is no data cells above or below the cell then it should not be selected. Thank you

I found this macro to select all data cells in range, is it possible to add an IF for the "data below" into this? I tried, Im really green with VBA. Thank you.

Sub SelectNotBlackRange()
'Update20131220
Dim Rng As Range
Dim WorkRng As Range
Dim OutRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    If Rng.Value <> "" Then
        If OutRng Is Nothing Then
            Set OutRng = Rng
        Else
            Set OutRng = Union(OutRng, Rng)
        End If
    End If
Next
If Not OutRng Is Nothing Then
    OutRng.Select
End If
End Sub
1
Thank you, I updated my postalex m

1 Answers

0
votes

Here is an example for a typical column, column C:

Sub CSelect()
    Dim kolumn As String, N As Long
    Dim i As Long
    kolumn = "C"
    N = Cells(Rows.Count, kolumn).End(xlUp).Row
    For i = 1 To N
        If Cells(i, kolumn).Value <> "" And Cells(i + 1, kolumn).Value <> "" Then
            Cells(i, kolumn).Select
            Exit Sub
        End If
    Next i
End Sub

enter image description here

NOTE:

This will only select the first cell meeting the selection criteria.

EDIT#1:

The sub will Select all the cells in a column meeting the criteria:

Sub CSelectALL()
    Dim kolumn As String, N As Long
    Dim i As Long, rng As Range
    kolumn = "C"
    N = Cells(Rows.Count, kolumn).End(xlUp).Row
    Set rng = Nothing

    For i = 1 To N
        If Cells(i, kolumn).Value <> "" And Cells(i + 1, kolumn).Value <> "" Then
            If rng Is Nothing Then
                Set rng = Cells(i, kolumn)
            Else
                Set rng = Union(rng, Cells(i, kolumn))
            End If
        End If
    Next i

    If rng Is Nothing Then
    Else
        rng.Select
    End If
End Sub

enter image description here