PostScriptStudent
New Member
- Joined
- Nov 22, 2020
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Procedures to perform are:
* Populate a 1D range of general size n - done
* Add two 1D ranges - I have the ranges but I can't get the sum of them (code below)
Private Sub CommandButton1_Click()
'Clearing worksheet
Range("A:Z").Clear
'Declarations
Dim range1, range2 As Range, array1() As Double, SumRange As Double
SumRange = 0
'Initialization
Set range1 = populateRange(InputBox("enter # of rows of range 1"), 1, "A1")
Set range2 = populateRange(InputBox("enter # of rows of range 2"), 1, "C1")
Range("E1").Value = SumRange
End Sub
Here are my Functions:
'Function populates a 2D range starting at any cell in the worksheet using InputBox
Function populateRange(m As Integer, n As Integer, RangeDef As String)
'Function Documentation
'm is the number of rows of the range
'n is the number of colums of the range
'RangeDef is where the range will start being stored on the worksheet
'Declarations
Dim i, j As Integer
Dim range1 As Range
'Initialization
Set range1 = Range(RangeDef)
For i = 1 To m
For j = 1 To n
range1.Offset(i - 1, j - 1).Value = InputBox("Enter " & i & ", " & j & " element of array ")
Next j
Next i
Set range1 = Range(Range(RangeDef), Range(RangeDef).End(xlDown))
Set populateRange = range1
End Function
'Function converting a given 1D range to an array
Function Range2Array(range1 As Range) As Double()
'Declarations
Dim length, i As Integer, array1() As Double
length = range1.Rows.Count
ReDim array1(length)
For i = 1 To length
array1(i) = range1(i, 1).Value
Next i
Range2Array = array1
End Function
I would love some help with this
Thank you
PSStudent
* Populate a 1D range of general size n - done
* Add two 1D ranges - I have the ranges but I can't get the sum of them (code below)
Private Sub CommandButton1_Click()
'Clearing worksheet
Range("A:Z").Clear
'Declarations
Dim range1, range2 As Range, array1() As Double, SumRange As Double
SumRange = 0
'Initialization
Set range1 = populateRange(InputBox("enter # of rows of range 1"), 1, "A1")
Set range2 = populateRange(InputBox("enter # of rows of range 2"), 1, "C1")
Range("E1").Value = SumRange
End Sub
Here are my Functions:
'Function populates a 2D range starting at any cell in the worksheet using InputBox
Function populateRange(m As Integer, n As Integer, RangeDef As String)
'Function Documentation
'm is the number of rows of the range
'n is the number of colums of the range
'RangeDef is where the range will start being stored on the worksheet
'Declarations
Dim i, j As Integer
Dim range1 As Range
'Initialization
Set range1 = Range(RangeDef)
For i = 1 To m
For j = 1 To n
range1.Offset(i - 1, j - 1).Value = InputBox("Enter " & i & ", " & j & " element of array ")
Next j
Next i
Set range1 = Range(Range(RangeDef), Range(RangeDef).End(xlDown))
Set populateRange = range1
End Function
'Function converting a given 1D range to an array
Function Range2Array(range1 As Range) As Double()
'Declarations
Dim length, i As Integer, array1() As Double
length = range1.Rows.Count
ReDim array1(length)
For i = 1 To length
array1(i) = range1(i, 1).Value
Next i
Range2Array = array1
End Function
I would love some help with this
Thank you
PSStudent