Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Programmatically add worksheet function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Office_Novice - 21 May 2008 13:58 GMT
I haven't tried this before now and i am having a little trouble.

This is what i have:
Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C5000)"

And it works ok, But i wont always know the range. I need something like:

Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C" & LastRow)"

But i tried that and the function didn’t recognize my variable. Any help
would be great.
Chip Pearson - 21 May 2008 14:11 GMT
You can't use a variable directly within the formula string because that is
passed to Excel and Excel has no idea about VBA variable. Your code is very
close to what you want. Try

Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C" & LastRow & ")"

Also, as a matter of style, I would change .Value to .Formula. Value will
work, but Formula is more accurate and self-explanatory.

Signature

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
   Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

>I haven't tried this before now and i am having a little trouble.
>
[quoted text clipped - 7 lines]
> But i tried that and the function didn’t recognize my variable. Any help
> would be great.
Office_Novice - 21 May 2008 17:01 GMT
Thanks for the help. The .Formula tip will be most useful.

> You can't use a variable directly within the formula string because that is
> passed to Excel and Excel has no idea about VBA variable. Your code is very
[quoted text clipped - 16 lines]
> > But i tried that and the function didn’t recognize my variable. Any help
> > would be great.
JW - 21 May 2008 14:13 GMT
On May 21, 8:58 am, Office_Novice
<OfficeNov...@discussions.microsoft.com> wrote:
> I haven't tried this before now and i am having a little trouble.
>
[quoted text clipped - 7 lines]
> But i tried that and the function didn’t recognize my variable. Any help
> would be great.

Your inserting a formula.  Why are you using .Value?  You should use
something like this:
Dim LastRow As Long
   LastRow = Cells(Rows.Count, "C").End(xlUp).Row
   Worksheets("Sheet2").Range("C1").Formula = _
       "=SUM(C2:C" & LastRow & ")"
Sam Wilson - 21 May 2008 14:18 GMT
Worksheets("Sheet2").Range("C1").formula = "=SUM(C2:" &
range("c2").end(xldown).address(false,false) & ")"

> I haven't tried this before now and i am having a little trouble.
>
[quoted text clipped - 7 lines]
> But i tried that and the function didn’t recognize my variable. Any help
> would be great.
Bob Phillips - 21 May 2008 14:23 GMT
Worksheets("Sheet2").Range("C1").Value = "=SUM(C2:C" & LastRow & ")"

Signature

---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

>I haven't tried this before now and i am having a little trouble.
>
[quoted text clipped - 7 lines]
> But i tried that and the function didn't recognize my variable. Any help
> would be great.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.