+ Reply to Thread
Results 1 to 8 of 8

Thread: Modify Formula to Find Last Column

  1. #1
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    184

    Modify Formula to Find Last Column

    Hi, I'm using this formula to find the range of used cells and then offset to a specific columna and insert a formula:

    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(, 26)
        .FormulaR1C1 = "=IF(RC[-1] = """", """", TODAY()-RC[-1])"
    Works great - but how can I determine programmatically where the last column is instead of using offset? I've been messing around (see below) but while this doesn't throw an error it also doesn't do anything.

    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset.Cells(Columns.Count, "A").End(xlToRight)
        .FormulaR1C1 = "=IF(RC[-1] = """", """", TODAY()-RC[-1])"
    Need to find the last column in row 1 and then offset 1 (to a blank column) (or find first blank column)
    Last edited by ker9; 11-17-2011 at 11:28 AM.

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Modify Formula to Find Last Column

    Try this
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        'or
        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        NextCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  3. #3
    Valued Forum Contributor GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2007/2010/2011
    Posts
    310

    Re: Modify Formula to Find Last Column

    Ok, you can use the following to find the last column in general, i'm using a slightly different statement.

    The key is, you can use "columns.count" in the column entry just like you used rows.count. Also, you can use the End mode to travel left from the furthest column but stating .end(xltoleft) -- just like you used .end(xlup) to find the last row.

    Range("A2" & ":" & Cells(your_row_number, Cells(2,columns.count).end(xlToLeft).address))
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  4. #4
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    184

    Re: Modify Formula to Find Last Column

    Hi, thank you - still having a problem:

    As before, this works:
    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(, 26)
        .FormulaR1C1 = "Test"
    (This correctly puts it in Column AA for the correct range of data)

    Trying to make this work:
    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(Cells(1, Columns.Count).End(xlToRight).Column + 1)
        .FormulaR1C1 = "TEST"
    Doesn't seem to matter if I use (xlToRight) or (xlToLeft), it hits column A starting at row 16387. The original data range ends at Row 235 (and I've made sure last cell is in this row).

  5. #5
    Valued Forum Contributor GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2007/2010/2011
    Posts
    310

    Re: Modify Formula to Find Last Column

    Hmm... using xltoright from the last column with an offset statement is definitely going to give problems - if it worked it would be offsetting your range right off the worksheet!

    I tried this (put in a comma you were missing and used xltoleft). seems to work (not exactly sure what you are doing though).

    check the attachment too.

    With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(, Cells(1, Columns.Count).End(xlToLeft).Column + 1)
        .FormulaR1C1 = "TEST"
    End With
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  6. #6
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Modify Formula to Find Last Column

    Is this what you are trying to do?
    Sub DaysElapsed()
        Dim LastRow As Long, NextCol As Long
    
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        NextCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
    
        With Range(Cells(2, NextCol), Cells(LastRow, NextCol))
            .FormulaR1C1 = "=IF(RC[-1] = """", """", TODAY()-RC[-1])"
        End With
     
    End Sub
    an alternative might be
    Sub DaysElapsed()
        Dim LastRow As Long, NextCol As Long
    
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        NextCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
    
        Cells(2, NextCol).Resize(LastRow - 1, 1).Formula = "=IF(RC[-1] = """", """", TODAY()-RC[-1])"
    
    End Sub
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  7. #7
    Forum Contributor
    Join Date
    06-16-2010
    Location
    Tampa, FL
    MS-Off Ver
    Excel 2007
    Posts
    184

    Re: Modify Formula to Find Last Column

    Thanks to both of you. GeneralDisarray - it worked! Thank you.

  8. #8
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Modify Formula to Find Last Column

    Your question was
    ..... but how can I determine programmatically where the last column is instead of using offset?
    As far as I can see GeneralDisarrays' solution uses offset ...
    Also
    Offset(, Cells(1, Columns.Count).End(xlToLeft).Column + 1)
    This skips a column and returns the formula in the second available blank column so your formula will always return "", remove the +1 and all will be okay.
    Last edited by Marcol; 11-18-2011 at 04:39 AM.

+ 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.2.0