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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Nimrod

MrExcel MVP
Joined
Apr 29, 2002
Messages
6,259
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,504
Messages
5,832,111
Members
430,111
Latest member
Francis Xavier

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
Top