Excel VBA code Private Sub Worksheet Change Power function not working

2

1

I'm making a worksheet where the wind speed defines the potential energy of the location. This value is the basis of a few sheets in the same Excel file. I'm trying to write a code that where I10 is the wind speed and I12 is the potential kWh on that location. I want both I10 as I12 to be input cells where you can either say: I want to know what wind speed I need to generate 4000 kwh i.e. or my wind speed is 4.2 m/s what will my potential energy be?

This is what I made so far, but I don't get the power function to work. At the moment I defined the cell I12 (The kWh's) without VBA as =(2208,5/(54,872))*I10^3 where I10 is the windspeed.

VBA code behind sheet which doesn't work:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Address = "$I$10" Or Target.Address = "$I$12" Then

        WIND = Range("I10")
        KWH = Range("I12")

        Application.EnableEvents = False

        If Target.Address = "$I$10" Then
            Range("I12").Value = (2208.5 / (54.872)) POWER(WIND,3)
        Else
            Range("I10").Value = POWER( KWH (2208.5 / (54.872)),(1/3)

        End If
    End If
    Application.EnableEvents = True
End Sub

Potential solution, but not working at the moment.

Function MyPower(Number As Double, Exponent As Double) As Double
    MyPower = Number ^ Exponent
End Function

YouKnowWho

Posted 2014-01-13T09:41:42.790

Reputation: 23

POWER isn't being called correctly. – Raystafarian – 2014-01-13T13:48:06.353

Answers

0

Try this (but double check the math, I'm not sure if it's calculating correctly because I'm not sure what the equation should look like)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Address = "$I$10" Or Target.Address = "$I$12" Then

        wind = Range("I10")
        KWH = Range("I12")

        Application.EnableEvents = False

        If Target.Address = "$I$10" Then
            Range("I12").Value = (2208.5 / 54.872) * wind ^ 3
        Else
            Range("I10").Value = KWH * (2208.5 / 54.872) ^ (1/3)

        End If
    End If
    Application.EnableEvents = True
End Sub

Raystafarian

Posted 2014-01-13T09:41:42.790

Reputation: 20 384

The code you edited worked! Thanks a lot! The formula turned out to be: Range("I10").Value = ((KWH * 54.872) / 2208.5) ^ (1 / 3) I worked with an ugly solution to define some cells behind an graph with the correct data, but this is much more elegant. – YouKnowWho – 2014-01-13T21:07:17.197

@YouKnowWho I updated the code to reflect this change. If your problem is solved, you can go ahead and mark the question as answered so that it is removed from the "unanswered" question list. – Raystafarian – 2014-01-14T13:52:36.323