Current location: Hot Scripts Forums » Programming Languages » Visual Basic » Summing cells from different worksheets within a formula in vba


Summing cells from different worksheets within a formula in vba

Reply
  #1 (permalink)  
Old 10-17-06, 10:57 AM
sintegro65 sintegro65 is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Summing cells from different worksheets within a formula in vba

Hi I have been trying to write a code that sets a function equal to 0 based on a variable input. However the function is a summation of terms that each involve the variable input, so solver does not work. I have started to write some of the code yet I have to sum up cells within the function code, and it keeps producing an error. I have very limited coding knowledge and I have no idea what is wrong with it. Here is the code any help would be greatly appreciated.

Code:
Do Until Benefit <= 0.00000001 And Benefit > -0.00000001
AVPYr1 = CDbl(Worksheets("Investment Income").Range("$C$27").Value)
 
AVPYr2 = CDbl(Worksheets("Investment Income").Range("C19").Value) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C37").Value) + (AVPYr1 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("C39").Value)) + CDbl(Range("D13").Value) + CDbl(Worksheets("Investment Income").Range("D15").Value) + (CDbl(Worksheets("Investment Income").Range("D17").Value) * 0.5)
 
AVPYr3 = AVPYr2 + (CDbl(Worksheets("Investment Income").Range("D17").Value) * 0.5) + (CaptiveIR * AVPYr2) + CDbl(Worksheets("Assumpions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C38").Value) + (AVPYr2 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("D39").Value)) + CDbl(Worksheets("Investment Income").Range("E13").Value) + CDbl(Worksheets("Investment Income").Range("e15").Value) + (CDbl(Range("e17").Value) * 0.5)
 
AVPYr4 = AVPYr3 + (CDbl(Worksheets("Investment Income").Range("E17").Value) * 0.5) + (CaptiveIR * AVPYr3) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C39").Value) + (AVPYr3 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("E39").Value)) + CDbl(Worksheets("Investment Income").Range("F13").Value) + CDbl(Worksheets("Investment Income").Range("F15").Value) + (CDbl(Worksheets("Investment Income").Range("F17").Value) * 0.5)
 
AVPYr5 = AVPYr4 + (CDbl(Worksheets("Investment Income").Range("F17").Value) * 0.5) + (CaptiveIR * AVPYr4) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C40").Value) + (AVPYr4 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("F39").Value)) + CDbl(Worksheets("Investment Income").Range("G13").Value) + CDbl(Worksheets("Investment Income").Range("G15").Value) + (CDbl(Worksheets("Investment Income").Range("G17").Value) * 0.5)
 
InvestOnshore = CDbl(Worksheets("Investment Income").Range("Q173").Value) * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5)
 
InvestCap = CaptiveIR * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5)
 
IncTax = CDbl(Worksheets("Investment Income").Range("H259").Value)
 
Premium = CDbl(Worksheets("Investment Income").Range("F164").Value)
 
NetCostCap = CDbl(Worksheets("Investment Income").Range("F165").Value)
 
ImpactTax = CDbl(Worksheets("Assumptions").Range("B8")) * ((Sheets("Premium Gross Up") + WorksheetFunction.Sum(Range("H37"), Range("H41")) + (Sheets("High Deductible Detail") + WorksheetFunction.Sum(.Range(.Range("C10"), .Range("G16"))) + CDbl(Worksheets("Investment Income").Range("Q173").Value) * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5))))
 
Benefit = ImpactTax + Premium + NetCostCap + InvestCap + InvestOnshore + IncTax
 
If Benefit < 0.00000001 Then
CaptiveIR = CaptiveIR + 0.0001
Else: CaptiveIR = CaptiveIR - 0.0001
End If
 
Loop
 
Range("AL173").Value = CaptiveIR0

Last edited by digioz; 10-18-06 at 09:10 AM. Reason: Please use CODE Tags
Reply With Quote
  #2 (permalink)  
Old 10-18-06, 09:21 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
What is the exact error it gives you? Did you try stepping through the code (F8) and/or putting break points?
__________________
Reply With Quote
  #3 (permalink)  
Old 10-18-06, 09:34 AM
sintegro65 sintegro65 is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I saw what i did on that, but I want it to calculate the break even interest rate and put that value in cell al173, but when the function runs it only spits out the original value for captiveIR here is the code, any help again would be greatly appreciated:

Code:
Private Sub CommandButton1_Click()
Run Breakeven()
 
End Sub
 
Function Breakeven() As Variant
 
Dim Benefit As Double
Dim CaptiveIR As Double
Dim AVPYr1 As Double
Dim AVPYr2 As Double
Dim AVPYr3 As Double
Dim AVPYr4 As Double
Dim AVPYr5 As Double
Dim ImpactTax As Double
Dim Premium As Double
Dim NetCostCap As Double
Dim InvestCap As Double
Dim InvestOnshore As Double
Dim IncTax As Double
Dim PGTotal As Double
Dim HDDDTotal As Double
CaptiveIR = 0.05
 
 
 
Do Until Benefit <= 0.00000001 And Benefit > -0.00000001
AVPYr1 = CDbl(Worksheets("Investment Income").Range("$C$27").Value)
 
AVPYr2 = CDbl(Worksheets("Investment Income").Range("C19").Value) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C37").Value) + (AVPYr1 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("C39").Value)) + CDbl(Range("D13").Value) + CDbl(Worksheets("Investment Income").Range("D15").Value) + (CDbl(Worksheets("Investment Income").Range("D17").Value) * 0.5)
 
AVPYr3 = AVPYr2 + (CDbl(Worksheets("Investment Income").Range("D17").Value) * 0.5) + (CaptiveIR * AVPYr2) + CDbl(Worksheets("Assumpions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C38").Value) + (AVPYr2 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("D39").Value)) + CDbl(Worksheets("Investment Income").Range("E13").Value) + CDbl(Worksheets("Investment Income").Range("e15").Value) + (CDbl(Range("e17").Value) * 0.5)
 
AVPYr4 = AVPYr3 + (CDbl(Worksheets("Investment Income").Range("E17").Value) * 0.5) + (CaptiveIR * AVPYr3) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C39").Value) + (AVPYr3 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("E39").Value)) + CDbl(Worksheets("Investment Income").Range("F13").Value) + CDbl(Worksheets("Investment Income").Range("F15").Value) + (CDbl(Worksheets("Investment Income").Range("F17").Value) * 0.5)
 
AVPYr5 = AVPYr4 + (CDbl(Worksheets("Investment Income").Range("F17").Value) * 0.5) + (CaptiveIR * AVPYr4) + CDbl(Worksheets("Assumptions").Range("B8").Value) * (CDbl(Worksheets("Premium Gross Up").Range("C40").Value) + (AVPYr4 * CaptiveIR) + CDbl(Worksheets("Financial Statements").Range("F39").Value)) + CDbl(Worksheets("Investment Income").Range("G13").Value) + CDbl(Worksheets("Investment Income").Range("G15").Value) + (CDbl(Worksheets("Investment Income").Range("G17").Value) * 0.5)
 
InvestOnshore = CDbl(Worksheets("Investment Income").Range("Q173").Value) * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5)
 
InvestCap = CaptiveIR * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5)
 
IncTax = CDbl(Worksheets("Investment Income").Range("H259").Value)
 
Premium = CDbl(Worksheets("Investment Income").Range("F164").Value)
 
NetCostCap = CDbl(Worksheets("Investment Income").Range("F165").Value)
 
ImpactTax = CDbl(Worksheets("Assumptions").Range("B8")) * ((Sheets("Premium Gross Up")) + WorksheetFunction.Sum(Range("H37"), Range("H41")) + (Sheets("High Deductible Detail")) + WorksheetFunction.Sum(Range("C10"), Range("G16")) + (CDbl(Worksheets("Investment Income").Range("Q173").Value) * (AVPYr1 + AVPYr2 + AVPYr3 + AVPYr4 + AVPYr5)))
Benefit = ImpactTax + Premium + NetCostCap + InvestCap + InvestOnshore + IncTax
 
If Benefit < 0.00000001 Then
CaptiveIR = CaptiveIR + 0.0001
Else: CaptiveIR = CaptiveIR - 0.0001
End If
 
Loop
 
Range("AL173").Value = CaptiveIR
 
 
End Function

Last edited by digioz; 10-18-06 at 10:04 AM.
Reply With Quote
  #4 (permalink)  
Old 10-18-06, 10:07 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Can you put the actual excel spreadsheet into a zip file and post it here? Makes it easier to troubleshoot.
__________________
Reply With Quote
  #5 (permalink)  
Old 10-18-06, 10:14 AM
sintegro65 sintegro65 is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
its a bit disorganized if I was to create a simplified file that runs through the same process would that help? thanks for all your help
Reply With Quote
  #6 (permalink)  
Old 10-18-06, 11:40 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Sure. That would do the trick.
__________________
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Excel Formula using VBA rich8008 Visual Basic 1 09-26-06 08:22 PM
EXCEL VBA: Delete all empty worksheets crmpicco Visual Basic 1 05-16-05 07:53 PM


All times are GMT -5. The time now is 01:17 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.