?
Solved

Enter a formula in worksheet using VBA

Posted on 2013-12-27
7
Medium Priority
?
384 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 200 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 52

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 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 48

Accepted Solution

by:
Dale Fye earned 1600 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
Independent Software Vendors: 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 48

Expert Comment

by:Dale Fye
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

777 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