Pregunta

So i have this number of different two dimensional arrays that contain different physical attributes of a flow (mach number, temperature, etc). i need to plot these values in excel and calculate different averages. these arrays are declared globally. To do this i used to write one subroutine for each array but its not a good way, because you should keep it as general as possible right?

so what i end up with is: the subroutine is passed the arrays and the corresponding worksheet...

or use pointers? would also be interesting to know how you would solve this in other more advanced programming languages (C++,..) since vba is not quite my weapon of choice but in this case its necessary.

It errors with a "Type mismatch: Array or user-defined type expected!"

When i call i use:

Call WriteFlowVariable(ws_meridian_velocity, MeridSpeed(), average_MeridSpeed(), area_average_MeridSpeed(), mass_average_MeridSpeed())

This is the Subroutine:

Sub WriteFlowVariable(ws As Worksheet, FlowVariable() As Double, average_FlowVariable() As Double, area_average_FlowVariable(), mass_average_FlowVariable())

Dim i As Integer
Dim j As Integer

Dim sum_v As Double
Dim FlowVariabledeltaA(1000, 300) As Double
Dim FlowVariabledeltaA_added(1000) As Double

Dim FlowVariabledeltaM(1000, 300) As Double
Dim FlowVariabledeltaM_added(1000) As Double

'write titles of the charts
ws.Cells.Clear

For j = 0 To SecNumber - 1
    ws.Cells(1, j + 2).value = (j + 1)
Next j
ws.Cells(1, SecNumber + 2) = "Arithmetic Average"
ws.Cells(1, SecNumber + 3) = "Area Average"

For i = 0 To number_of_axial_positions - 1

    sum_v = 0

    'Write section number
    ws.Cells(i + 2, 1).value = i + 1

    'Write value and calculate arithmetic, mass and area average
    For j = 0 To SecNumber - 1
        ws.Cells(i + 2, j + 2).value = FlowVariable(i, j)
        sum_v = sum_v + FlowVariable(i, j)

        If j < SecNumber - 1 Then

            FlowVariable(i, j) = FlowVariable(i, j) * deltaA(i, j)

            FlowVariabledeltaA_added(i) = FlowVariabledeltaA_added(i) + FlowVariabledeltaA(i, j)

            FlowVariabledeltaM(i, j) = FlowVariable(i, j) * deltaM(i, j)

            FlowVariabledeltaM_added(i) = FlowVariabledeltaM_added(i) + FlowVariabledeltaM(i, j)

        End If

    Next j

    average_FlowVariable(i) = sum_v / SecNumber
    'Write arithmetic average to the third last column
    ws.Cells(i + 2, SecNumber + 2) = average_FlowVariable(i)

    area_average_FlowVariable(i) = FlowVariabledeltaA_added(i) / crosssectionareaInput(i)
    'Write area average to the second last column
    ws.Cells(i + 2, SecNumber + 3) = area_average_FlowVariable(i)

    mass_average_FlowVariable(i) = FlowVariabledeltaM_added(i) / crosssectionareaInput(i)
    'Write mass average to the last column
    ws.Cells(i + 2, SecNumber + 4) = mass_average_FlowVariable(i)
Next i

End Sub
¿Fue útil?

Solución

I didn't examine your Sub too closely at all, but assuming

FlowVariable(,) As Double
average_FlowVariable(,) As Double
area_average_FlowVariable(,)
mass_average_FlowVariable(,)

are all declared correctly to be considered global variables, your call should look as follows:

Call WriteFlowVariable(ws_meridian_velocity, MeridSpeed, average_MeridSpeed, area_average_MeridSpeed, mass_average_MeridSpeed)

... In other words, remove the ().

Also, since these are 2-D arrays, your Sub should look as follows:

Sub WriteFlowVariable(ws As Worksheet, FlowVariable(,) As Double, average_FlowVariable(,) As Double, area_average_FlowVariable(,), mass_average_FlowVariable(,))Dim i As Integer

Hope this does the trick, otherwise, lemme know and I'll look closer.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top