Solved

Enter a formula in worksheet using VBA

Posted on 2013-12-27
7
347 Views
Last Modified: 2013-12-28
Dear Experts:

My current workbook has several worksheets.

On the first worksheet named 'MainSheet' (current worksheet) ...
... I need to enter the following formula into 'B20' using a VBA macro.

=SUM('MyCustomSheetName'!$G$5:$G$259)

The requirements:
1) The Name of 'MyCustomSheetName' worksheet is variable, i.e. this name is to be taken from the cell value of 'A17' of the 'MainSheet' worksheet. If the cell value of 'A17' is blank, the macro has to say so and will exit.

2) The range of the sum always starts at G5 but the End Range Value varies, i.e. it could be $G$5:$G$3574 or some other End Range value.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
7 Comments
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 50 total points
ID: 39741658
If you want to add formulas to your sheets using VBA, "just" put them into the "Formula" property of the corresponding Cell/Range object(s), as shown here:
http://msdn.microsoft.com/en-us/library/office/aa221602%28v=office.11%29.aspx
0
 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 50 total points
ID: 39741667
Hi,

pls try

Sub Macro2()
srcSht = Sheets("MainSheet").Range("A17").Value
If srcSht = "" Then
    MsgBox "No Sheet choosed:  Exiting..."
    Exit Sub
End If
Set myRange = Sheets(srcSht).Range(Range("G5"), Range("G" & Rows.Count).End(xlUp))
Sheets("MainSheet").Range("B20").Formula = "=SUM('" & srcSht & "'!" & myRange.Address & ")"
End Sub

Open in new window

Regards
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 400 total points
ID: 39741696
You will need to build the string to assign to the formula programmatically.

You would first need to check to see whether the value of 'MainSheet' cell A17 is blank

strSheet = ActiveWorkbook.sheets("MainSheet").Range("A17").Value
if strSheet = "" Then Exit Sub

To get the last row used in a column, you can use code similar to:

LastRow = Activesheet.Range("G65000").end(xlup).row

Then you would build the formula string

strFormula = "=SUM('" & strSheet & "'!$G$5:$G$" & LastRow & ")"
ActiveWorkbook.sheets("MainSheet").Range("B20").Formula = strFormula

String all of that together in a macro, subroutine, or function to get what you need.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:andrew_man
ID: 39741712
Just can use the formula indirect is okay

=SUM(indirect(somewhere))

Put    'MyCustomSheetName'!$G$5:$G$259  on somewhere ....
0
 

Author Comment

by:AndreasHermle
ID: 39741804
Hi Rgonzo: Thank you very much for your swift and professional support. I am afraid to tell you that your code throws an 1004 error message and the error trapping says that the range is not created on the 'srcSht' Worksheet but on the main Sheet. Any idea why?

Hi Dale: your code works great. Thank you very much for it.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39741819
glad to help.
0
 

Author Closing Comment

by:AndreasHermle
ID: 39744381
Dear All,

thank you very much for your great support. I can use parts of all the answers and integrate it into my final code. Thank you very much for your professional help.

I really appreciate it.
Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
copy the content of word to outlook 3 48
ActiveX Listbox Multi Select in Excel 2010 8 16
Excel VBA When using VLookup 6 26
Sum iF  based on a null cell 11 29
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now