Solved

Enter a formula in worksheet using VBA

Posted on 2013-12-27
7
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

735 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