First cell with Data in a Variable

HGDantes

New Member
Joined
Feb 10, 2023
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
Hello

In the following code, I would like to put the Row number of the first line with Data in a variable. (or be able to start my selection from the first line with Data in column A) Right now, my script works well, only when the first line with Data, is Line 2.

VBA Code:
ActiveSheet.Range("A:A").AutoFilter Field:=5, Criteria1:="JD"
If IsEmpty(Range("A2").Value) = False Then
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
End If

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello

In the following code, I would like to put the Row number of the first line with Data in a variable. (or be able to start my selection from the first line with Data in column A) Right now, my script works well, only when the first line with Data, is Line 2.

VBA Code:
ActiveSheet.Range("A:A").AutoFilter Field:=5, Criteria1:="JD"
If IsEmpty(Range("A2").Value) = False Then
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
End If

Thanks
Does your data in col A have a header? If yes, what is the header?
 
Upvote 0
This will return the row number of the first cell below the header cell in column A, provided the header cell is visible.
VBA Code:
Sub FirstDataCell()
'returns the row number of the cell after a header cell in column A, provided the header cell is visible.
Dim FirstDataCell As Range
Const Hdr As String = "Date"   'change to suit
On Error Resume Next
Set FirstDataCell = Range("A:A").Find(what:=Hdr, after:=Cells(Rows.Count, "A").End(xlUp), searchdirection:=xlNext, LookIn:=xlValues, lookat:=xlWhole).Offset(1, 0)
On Error GoTo 0
If Not FirstDataCell Is Nothing Then
    MsgBox "Row " & FirstDataCell.Row
Else
    MsgBox "A cell with the header " & Hdr & " in it is not visible in column A"
End If
End Sub
 
Upvote 0
Thanks, but it still not working properly. Here it's returning Row 2. In this example, I would like it to return Row 8.

Thanks

Regards



1676385386048.png
 
Upvote 0
OK, try this.
This will return the row number of the first visible cell below the header cell in column A, provided the header cell is visible.
VBA Code:
Sub FirstDataCell()
'returns the row number of the first visible cell after a header cell in column A
Dim HdrCell As Range, FirstDataCell As Range
Const Hdr As String = "Date"   'change to suit
On Error Resume Next
Set HdrCell = Range("A:A").Find(what:=Hdr, after:=Cells(Rows.Count, "A").End(xlUp), searchdirection:=xlNext, LookIn:=xlValues, lookat:=xlWhole)
On Error GoTo 0
If Not HdrCell Is Nothing Then
    On Error Resume Next
    Set FirstDataCell = Range(HdrCell.Offset(1, 0), Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible)(1)
    On Error GoTo 0
    If Not FirstDataCell Is Nothing Then
        If FirstDataCell.Address = HdrCell.Address Then
            MsgBox "There are no visible cells in the data range below the header cell."
        Else
            MsgBox "First visible data cell is in Row " & FirstDataCell.Row
        End If
    End If
Else
    MsgBox "A cell with the header " & Hdr & " in it is not visible in column A"
End If
End Sub
 
Upvote 1
Solution
OK, try this.
This will return the row number of the first visible cell below the header cell in column A, provided the header cell is visible.
VBA Code:
Sub FirstDataCell()
'returns the row number of the first visible cell after a header cell in column A
Dim HdrCell As Range, FirstDataCell As Range
Const Hdr As String = "Date"   'change to suit
On Error Resume Next
Set HdrCell = Range("A:A").Find(what:=Hdr, after:=Cells(Rows.Count, "A").End(xlUp), searchdirection:=xlNext, LookIn:=xlValues, lookat:=xlWhole)
On Error GoTo 0
If Not HdrCell Is Nothing Then
    On Error Resume Next
    Set FirstDataCell = Range(HdrCell.Offset(1, 0), Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible)(1)
    On Error GoTo 0
    If Not FirstDataCell Is Nothing Then
        If FirstDataCell.Address = HdrCell.Address Then
            MsgBox "There are no visible cells in the data range below the header cell."
        Else
            MsgBox "First visible data cell is in Row " & FirstDataCell.Row
        End If
    End If
Else
    MsgBox "A cell with the header " & Hdr & " in it is not visible in column A"
End If
End Sub
Super, It's working Perfectly now.

Thanks a lot
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top