LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Adding a formula to a macro

Are you talking about Ron DeBruin? His site includes in it's eaxamples
a function that returns the number of the last row:

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

It's very easy to use and highly effective. The only situation I've
found where it didn't work perfectly is on a filtered dataset.

Here's some code that does what you mentioned in your post:

Sub way()

Dim lastR As Long
Dim ws As Worksheet
Dim sumE As Double

Set ws = Sheet1 'put your sheet reference here
lastR = LastRow(ws) 'get the number of the last row

'use the number of the last row to define the range
sumE = Application.Sum(ws.Range("e2:e" & lastR))
'you didn't mention what you were doing with the sum _
of column e, so I just stored it in variable sumE

Dim c As Range
'again use the number of the last row to define the range
For Each c In ws.Range("f2:f" & lastR)
If c.Value = "R" Then
c.Offset(0, 1).Value = c.Offset(0, -1).Value
Else
c.Offset(0, 2).Value = c.Offset(0, -1).Value
End If
Next c

End Sub

Function LastRow(sh As Worksheet)
'copied from _
http://www.rondebruin.nl/copy1.htm _
near the bottom of the page
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function


Cliff Edwards


 
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
Adding to a Macro Steven Excel Discussion (Misc queries) 2 December 15th 08 06:21 PM
macro adding formula to new sheet toddsavage100 Excel Discussion (Misc queries) 2 March 14th 06 03:03 AM
adding a formula to a macro Hemming Excel Discussion (Misc queries) 1 March 9th 06 03:15 PM
adding a formula in macro cindy Excel Programming 8 August 10th 05 09:43 PM
Adding a character to a defined name with a formula or macro InfinityDesigns Excel Discussion (Misc queries) 20 June 24th 05 06:36 AM


All times are GMT +1. The time now is 02:03 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"