Hi,
I have a code that brings in specified inputs from a worksheet incrementally (Min, max, increment values specified). I.e min = 1, max = 5 & increm = 2 would result in 3 unique solutions as the output.
I also have a code that brings in a list of values that have been specified in a column on the same worksheet to an equation. I.e A list of 5 unique inputs would result in 5 unique solutions at the output.
My aim is to now incorporate both codes into one function so that I can bring in a range of inputs for one part of an equation, whilst also bringing in an incrementally rising input for another part of the same equation. For example, if i had a list of inputs in a column ranging from 1 to 5 (ie 1, 2, 3, 4, 5) and another input with a min value of 1, a max of 5 and increments of 2 (ie values of 1, 3, 5) I should get 15 individual solutions.
Any advise, tips, pointers or help with combining these codes will be much appreciated!
Here is the code that brings in inputs incrementally between a specific min & max.
Function valzArrayExp(min As Double, max As Double, inc As Double)
Dim i As Double, num As Double
Dim TempArr As Variant
num = (max - min) / inc
ReDim TempArr(num)
i = 0
Do Until i = num + 1
TempArr(i) = min + inc * i
i = i + 1
Loop
valzArrayExp = TempArr
End Function
Sub array_elements_input_equation_test()
Dim vValz, answers
Dim vElements As Double
Dim i As Double, k As Double
Dim A As Double, B As Double, C As Double
With Worksheets(1)
A = .Range("C13")
B = .Range("C6")
C = .Range("C12")
vValz = valzArrayExp(.Range("F5"), .Range("F4"), .Range("F6")) ' (min, max, inc)
End With
vElements = UBound(vValz) + 1
ReDim answers(vElements - 1)
For i = 0 To vElements - 1
answers(k) = (vValz(i) * A * B) / C
If Err Then
Err.Clear
answers(k) = 0
End If
k = k + 1
Next i
Range("Z4").Resize(UBound(answers) + 1) = Application.Transpose(answers)
End Sub
Here is the code that inputs a listed set of values from the worksheet column.
Sub test()
Dim D As Double
lastrow = Cells(Rows.Count, "L").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 12))
For i = 4 To lastrow
D = inarr(i, 12)
Cells(i, 26) = 1 + (20 * D) / 5 'example equation
Next i
End Sub
Individually they both work as intended.
I have combined them as follows.
Sub array_elements_and_Changing_input_equation()
Dim vValz, answers
Dim vElements As Double
Dim i As Double, k As Double
Dim A As Double, D As Double
With Worksheets(1)
A = .Range("C6")
vValz = valzArrayExp(.Range("F5"), .Range("F4"), .Range("F6"))
End With
lastrow = Cells(Rows.Count, "L").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 12))
For i2 = 4 To lastrow
D = inarr(i2, 12)
Next i2
vElements = UBound(vValz) + 1
ReDim answers(vElements - 1)
For i = 0 To vElements - 1
answers(k) = (vValz(i) * A) / D
If Err Then
Err.Clear
answers(k) = 0
End If
k = k + 1
Next i
Range("M4").Resize(UBound(answers) + 1) = Application.Transpose(answers)
End Sub
This script does not produce any error messages, but it does not work as intended.
Any help will be very much appreciated.
Bookmarks