Discussion Locked

Need help finding an Excel Formula

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a spreadsheet that has a list of products and prices. The products are in column A and prices from C to whatever. The columns C-?? have prices each column a new date respectively. I need a formula that will report the furthest # to the right and show it in Column B. I also need the expandability to add columns for the latest pricing. I'm stuck on this one!!!!

Thanks in advanced

Matt

12 replies

by peyison

Try this formula (in cell B1):

=INDIRECT("R1" & "C" & 2+COUNTA(C1:AZ1),FALSE)

This will count the number of non-empty cells in the range C1 to AZ1 (make this larger if needed), add 2 (since it starts at column C) and create an R1C1 reference to the last non-empty cell. The reference is then resolved using Indirect().

If you want to be able to copy this formula down the column, use this version:

=INDIRECT("R" & ROW(B1) & "C" & 2+COUNTA(C1:AZ1),FALSE)

This has Row(B1) instead of a hardcoded 1. When you copy this down, the cell in Row() will change, so it will always refer to the current row.

One thing - this will only work if you enter prices in continuous columns. If you skip a column, counta+2 won't be the same as the last column in the range.

Hope this helps.

by mkramer

Yes that was the problem I was coming across. I dont always buy every product every week and i dont want to spend time putting in 0's in all the products. Do you know of any way around it?

Thanks

Matt

by peyison

The only other way I know is to use a custom function.

From Excel, Select Tools -> Macro -> Visual Basic Editor

Press Ctrl+R to open the Projects Window

Select your workbook in the Projects Window and from the main menu select Insert -> Module

Double Click on the new module to open it

In the code window, paste this code:

Public Function GetLastValue(arLastCell As Range)
Dim lOffset As Long

'--make sure this function gets recalced when the workbook recalcs
Application.Volatile

lOffset = 0
Do Until arLastCell.Offset(0, lOffset).Value > 0
lOffset = lOffset - 1
Loop
GetLastValue = arLastCell.Offset(0, lOffset).Value

End Function

To use the function, enter (in cell B1):

=GetLastValue(AZ1)

Pass it the last cell you want to check for values.

You should be able to copy this down the column, but if there is not any data to the right of the cell, you might get a circular reference error since the last cell with a value will be the cell the formula is in.

If you have a large workbook, you can remove the line Application.Volatile from the function and recalc manually (F9).

by sankulpune

Hello Matt,

1) Formula or (user defined) function would be a little difficult because the calculation requires cell navigation which formula or function can't do.
2) You can however have a procedure and assign a keyboard shortcut to it. You haveto run this procedure for every calculation you do. You also need some rules to follow:
a) The top row should have labels and every column must have a labels. These labels should be contiguous with no intermittent blanks.
b) The cells that containno data must be 'null', not even spaces (which may be typed un-intentionally).

Following procedure will work if the above two conditions are ensured.

Sub FindLatestPrice()
'
' FindLatestPrice Macro
' Macro created on 14/05/2001 by SanganakSakha

Dim intCurRo As Integer
Dim intCurCol As Integer
Dim intTopRo As Integer
intCurRo = ActiveCell.Row
intCurCol = ActiveCell.Column
Selection.End(xlUp).Select
intTopRo = Selection.Row
Selection.End(xlToRight).Select
Selection.Offset(intCurRo - intTopRo, 1).Select
Selection.End(xlToLeft).Select
Cells(intCurRo, intCurCol + 1) = Selection.Value
Cells(intCurRo, intCurCol).Select
End Sub

I have successfully tried this code and would be happy to help you successfully implement this code.

Your feedback would be highly appreciated.

With best regards,

SanganakSakha,
Your Friend In Need.

by sankulpune

Hello,
I just forgot to mention that a cell only in the first column of the data shuld be selected before this sub is executed.

Hello Matt,

1) No formula or (user defined) function is possible because the calculation requires cell navigation which formula or function can't do.
2) You can however have a procedure and assign a keyboard shortcut to it. You have to run this procedure for every calculation you do. You also need some rules to follow:
a) The top row should have labels and every column must have a labels. These labels should be contiguous with no intermittent blanks.
b) The cells that contain no data must be 'null', not even spaces (which may be typed un-intentionally).

Following procedure will work if the above two conditions are ensured.

Sub FindLatestPrice()
'
' FindLatestPrice Macro
' Macro created on 14/05/2001 by SanganakSakha

Dim intCurRo As Integer
Dim intCurCol As Integer
Dim intTopRo As Integer
intCurRo = ActiveCell.Row
intCurCol = ActiveCell.Column
Selection.End(xlUp).Select
intTopRo = Selection.Row
Selection.End(xlToRight).Select
Selection.Offset(intCurRo - intTopRo, 1).Select
Selection.End(xlToLeft).Select
Cells(intCurRo, intCurCol + 1) = Selection.Value
Cells(intCurRo, intCurCol).Select
End Sub

I have successfully tried this code and would be happy to help you successfully implement this code.

Your feedback would be highly appreciated.

With best regards,

SanganakSakha,
Your Friend In Need.

by mkramer

Ok give me some time to check it out to see if it works I should be able to get back to u all tomorrow night.

by dklippert

Oops, I see the problem your working with.

Paste this function into a VBA module.
In cell B1 enter =LASTINROW(1:1) and then copy it down the column.
It won?t matter if there are empty cells.
This came from
http://j-walk.com/ss/excel/tips/tip30.htm

(No spaces in the URL)

Function LASTINROW(rngInput As Range) As Variant
Dim WorkRange As Range
Dim i As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = rngInput.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For i = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(i)) Then
LASTINROW = WorkRange(i).Value
Exit Function
End If Next i
End Function

This discussion is closed to new replies.