+ Reply to Thread
Results 1 to 5 of 5

Finging first row of a given column that has data in it.......exclude header

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Finging first row of a given column that has data in it.......exclude header

    I have this code and it works to find the first row of data in colB where it excludes the header row.


    first_data_row = ws.Cells.Find(What:="*", After:=[B1], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row

    However, when i try to make the code more flexible by makeing B1 a variable .....i get a type mismatch........see below

    Dim study_col As Integer
    
    fl_name = ThisWorkbook.Name
    Set ws = Workbooks(fl_name).Worksheets("Sheet1")
    Set ws3 = Workbooks(fl_name).Worksheets("Sheet3")
    study_col = 2
    'first_data_row = ws.Cells.Find(What:="*", After:=[B1], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
    first_data_row = ws.Cells.Find(What:="*", After:=[alphacol(study_col) & "1"], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
    MsgBox (first_data_row)



    I am enclosing an excel file as this usually makes things easier.
    Attached Files Attached Files
    Last edited by welchs101; 07-14-2011 at 02:23 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Finging first row of a given column that has data in it.......exclude header

    Hi welchs101, I had troubles with this line
    first_data_row = ws.Cells.Find(What:="*", After:=[alphacol(study_col) & "1"], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
    and I think it is because of alphacol. You have to declare alphacol as a range and then set it to B1 first. Let me know if the following does what you want
    Sub Macro3()
        Dim study_col As Integer
        Dim first_data_row As Long, alphacol As Range
        fl_name = ThisWorkbook.Name
        Set ws = Workbooks(fl_name).Worksheets("Sheet1")
        Set ws3 = Workbooks(fl_name).Worksheets("Sheet3")
        study_col = 2
        Set alphacol = ws.Cells(1, 2)
        first_data_row = ws.Cells.Find(What:="*", After:=[alphacol], SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
        MsgBox (first_data_row)
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Finging first row of a given column that has data in it.......exclude header

    Yes, that worked perfectly.

    But how did you know to convert it to a range.

    How did you know what the cells.find format needed?

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Finging first row of a given column that has data in it.......exclude header

    I knew that because After:= means after a range/cell (at least that's what I thought it meant).

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Finging first row of a given column that has data in it.......exclude header

    good enough for me.....thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1