I am trying to create some VB code to create a macro in Excel. However I have only used Python and Java before so brand new to this, The problem is I have a cell which contains a list of numbers all seperated by commas and stored as a string e.g. 12,5,7,9. This has been caluclated from elsewhere. I want to be able to iterate through the list of numbers, split them up, convert them to an integer and then calculate the average of them depending on how many there are. I would like this to apply to the currently selected cell and display the result in the next cell along.
Here is the code I have done so far. It does not work and I cant test it as get the error Object required when I run it. Any suggestions would be great. Thanks.
Sub CalculateAverage()
Dim contents As String
Dim cell As Range
Dim NumbersArray() As String
Set cell = ActiveCell.Select()
contents = Range(cell).value
NumbersArray = Split(contents, ",")
Dim count As Integer
Dim lengthOfArray As Integer
Dim first As Integer
Dim last As Integer
first = LBound(NumbersArray)
last = UBound(NumbersArray)
lengthOfArray = last - first
Dim total As Integer
Dim value As Integer
count = 0
While count <= lengthOfArray
total = total + CInt(NumbersArray(count))
count = count + 1
Wend
Dim average As Double
average = total / count
Range("A2").value = average
End Sub