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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,211,804
Messages
6,104,076
Members
447,893
Latest member
Hoosier605

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