Set variable for loop in VBA based on # of rows with data

J88L

New Member
Joined
Nov 7, 2005
Messages
16
I'm creating a macro to add data to the end of a list. When imported, the data has three header rows. The first row of headers is found using:
Code:
Cells.Find(What:="Date:", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
From there,
Code:
Activecell.Offset(4, 0).Select
...takes you to the first row of actual data. I now want the macro to capture how many rows of data (including that row) exist. Call it a pre-created variable named RowsWithData

How do I do that? :unsure:
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Public Sub Demo()


    Set FirstHdr = Cells.Find(What:="Date:", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False)
    
    
    If FirstHdr Is Nothing Then
        MsgBox ("no Date header found ")
    Else
        FirstDataRow = FirstHdr.Row + 4
        DateColumn = FirstHdr.Column
        
        LastDataRow = Cells(65536, DateColumn).End(xlUp).Row
        
        ln1 = "Data Row " & FirstDataRow & vbNewLine
        ln2 = "Date Column " & DateColumn & vbNewLine
        ln3 = "Last Data Row " & LastDataRow & vbNewLine
        
        msg = ln1 & ln2 & ln3
        MsgBox (msg)
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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