Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
AG AG is offline
external usenet poster
 
Posts: 54
Default Reference/define last cell

I need to define a formula in cell M1 similar to:
If the Last Cell in column M = 0, use the value from the row above in column
M, otherwise use the value of the Last Cell in column M. For example if the
last cell in Column M was in Row 100 the code would read

Range("M1").Select
ActiveCell.FormulaR1C1 = IF(M100 = 0, M99, M100)

My problem is that the last cell in Column M changes between sheets that
would use the macro.
Is there a way to reference or define these in a formula?
Please note columns N and O also contain data so a reference to the current
region would not work.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 192
Default Reference/define last cell

this sets "lastvalue" = the last value in M if it isn't 0 or the second to
last value in M if the last value is 0.

Sub selectvalue()

Dim lastvalue As Range

Range("M1").Select
With Range("M1")
If ActiveCell.End(xlDown).Value < 0 Then
Set lastvalue = ActiveCell.End(xlDown)
Else: Set lastvalue = ActiveCell.End(xlDown).Offset(-1, 0)
End If
End With

End Sub

--
-SA


"AG" wrote:

I need to define a formula in cell M1 similar to:
If the Last Cell in column M = 0, use the value from the row above in column
M, otherwise use the value of the Last Cell in column M. For example if the
last cell in Column M was in Row 100 the code would read

Range("M1").Select
ActiveCell.FormulaR1C1 = IF(M100 = 0, M99, M100)

My problem is that the last cell in Column M changes between sheets that
would use the macro.
Is there a way to reference or define these in a formula?
Please note columns N and O also contain data so a reference to the current
region would not work.

  #3   Report Post  
Posted to microsoft.public.excel.programming
AG AG is offline
external usenet poster
 
Posts: 54
Default Reference/define last cell

Thanks for the info.
BTW, I like the username!


"StumpedAgain" wrote:

this sets "lastvalue" = the last value in M if it isn't 0 or the second to
last value in M if the last value is 0.

Sub selectvalue()

Dim lastvalue As Range

Range("M1").Select
With Range("M1")
If ActiveCell.End(xlDown).Value < 0 Then
Set lastvalue = ActiveCell.End(xlDown)
Else: Set lastvalue = ActiveCell.End(xlDown).Offset(-1, 0)
End If
End With

End Sub

--
-SA


"AG" wrote:

I need to define a formula in cell M1 similar to:
If the Last Cell in column M = 0, use the value from the row above in column
M, otherwise use the value of the Last Cell in column M. For example if the
last cell in Column M was in Row 100 the code would read

Range("M1").Select
ActiveCell.FormulaR1C1 = IF(M100 = 0, M99, M100)

My problem is that the last cell in Column M changes between sheets that
would use the macro.
Is there a way to reference or define these in a formula?
Please note columns N and O also contain data so a reference to the current
region would not work.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Reference/define last cell

You do not need to select M1 in order to change make changes to it. When
ever you see code constructed like this...

Range("A1").Select
ActiveCell.<whatever

you can almost always do this instead...

Range("A1").<whatever

As to your question, these two lines can be used to replace your two line
attempt...

LastCell = Cells(Rows.Count, "M").End(xlUp).Row
Range("M1").Formula = "=IF(M" & LastCell & "=0,M" & (LastCell - 1) & _
", M" & LastCell & ")"

Rick


"AG" wrote in message
...
I need to define a formula in cell M1 similar to:
If the Last Cell in column M = 0, use the value from the row above in
column
M, otherwise use the value of the Last Cell in column M. For example if
the
last cell in Column M was in Row 100 the code would read

Range("M1").Select
ActiveCell.FormulaR1C1 = IF(M100 = 0, M99, M100)

My problem is that the last cell in Column M changes between sheets that
would use the macro.
Is there a way to reference or define these in a formula?
Please note columns N and O also contain data so a reference to the
current
region would not work.


  #5   Report Post  
Posted to microsoft.public.excel.programming
AG AG is offline
external usenet poster
 
Posts: 54
Default Reference/define last cell

Thanks for the solution.
I appreciate the tip. I am learning VBA via the recorder and sometimes I
just dont go back and edit to a higher degrees of elegance.


"Rick Rothstein (MVP - VB)" wrote:

You do not need to select M1 in order to change make changes to it. When
ever you see code constructed like this...

Range("A1").Select
ActiveCell.<whatever

you can almost always do this instead...

Range("A1").<whatever

As to your question, these two lines can be used to replace your two line
attempt...

LastCell = Cells(Rows.Count, "M").End(xlUp).Row
Range("M1").Formula = "=IF(M" & LastCell & "=0,M" & (LastCell - 1) & _
", M" & LastCell & ")"

Rick


"AG" wrote in message
...
I need to define a formula in cell M1 similar to:
If the Last Cell in column M = 0, use the value from the row above in
column
M, otherwise use the value of the Last Cell in column M. For example if
the
last cell in Column M was in Row 100 the code would read

Range("M1").Select
ActiveCell.FormulaR1C1 = IF(M100 = 0, M99, M100)

My problem is that the last cell in Column M changes between sheets that
would use the macro.
Is there a way to reference or define these in a formula?
Please note columns N and O also contain data so a reference to the
current
region would not work.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to use a cell reference to define the database? PerplexedinKY Excel Worksheet Functions 3 November 14th 06 09:00 PM
how do I define a chart series with an indirect reference tvanellen Charts and Charting in Excel 4 May 31st 06 10:52 AM
Define chart range using indirect reference Thomas Charts and Charting in Excel 3 May 10th 06 09:44 PM
Define Name reference Lp12 Excel Worksheet Functions 0 February 19th 06 08:08 AM
relative reference in a macro to define a name Hal Forman Excel Programming 2 May 28th 04 07:13 PM


All times are GMT +1. The time now is 12:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"