PC Review


Reply
Thread Tools Rate Thread

Adding a formula to a macro

 
 
marcia2026
Guest
Posts: n/a
 
      26th Jul 2008
I have been building upon Ron's macro that merges worksheets together into a
master worksheet. But I need to add 2 more steps to it. I need for it to
sum column E. The problem is that the range will vary each time the macro is
run. I still do not understand how to do the code for finding the last row.
I also need for it to add an if statement that puts a value in one of two
columns based on a value in a third column. My formula that I have been
entering manually is:
=If F1 = "R" then G1=E1 else H1=E1. I need this formula to loop through
each row in the table, but the end of the table varies. I really appreciate
all the help I have received from you guys. This is my very first attempt to
do any programming. THANKS BUNCHES
 
Reply With Quote
 
 
 
 
NoodNutt
Guest
Posts: n/a
 
      26th Jul 2008
G'day Marcia

Rather than specifying a range, select the whole column.

=Sum(E:E)

HTH
Mark.


 
Reply With Quote
 
Wigi
Guest
Posts: n/a
 
      26th Jul 2008
Hi Marcia

Have a look here on last cells: http://www.ozgrid.com/VBA/ExcelRanges.htm

--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"NoodNutt" wrote:

> G'day Marcia
>
> Rather than specifying a range, select the whole column.
>
> =Sum(E:E)
>
> HTH
> Mark.
>
>
>

 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      26th Jul 2008
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro adding formula to new sheet toddsavage100 Microsoft Excel Misc 2 14th Mar 2006 03:03 AM
adding a formula to a macro Hemming Microsoft Excel Misc 1 9th Mar 2006 03:15 PM
adding a formula in macro =?Utf-8?B?Y2luZHk=?= Microsoft Excel Programming 8 10th Aug 2005 09:43 PM
Adding a character to a defined name with a formula or macro =?Utf-8?B?SW5maW5pdHlEZXNpZ25z?= Microsoft Excel Misc 20 24th Jun 2005 06:36 AM
Adding macro code to Personal Macro Workbook mika Microsoft Excel Misc 2 16th Oct 2003 09:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:24 AM.