Hi, I'm using this formula to find the range of used cells and then offset to a specific columna and insert a formula:
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(, 26) .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)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])"
Last edited by ker9; 11-17-2011 at 11:28 AM.
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.
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.
Hi, thank you - still having a problem:
As before, this works:
(This correctly puts it in Column AA for the correct range of data)With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(, 26) .FormulaR1C1 = "Test"
Trying to make this work:
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).With Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Offset(Cells(1, Columns.Count).End(xlToRight).Column + 1) .FormulaR1C1 = "TEST"
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
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.
Is this what you are trying to do?
an alternative might beSub 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
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
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.
Thanks to both of you. GeneralDisarray - it worked! Thank you.
Your question was
As far as I can see GeneralDisarrays' solution uses offset ........ but how can I determine programmatically where the last column is instead of using offset?
Also
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.Offset(, Cells(1, Columns.Count).End(xlToLeft).Column + 1)
Last edited by Marcol; 11-18-2011 at 04:39 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks