VBA Code to Apply an AVERAGE of a Range on Cells as New Records are added Via a User Form

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
217
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet in which I require the average of a range of cells to be calcluated and applied to a designated cell as each new record is added to the worksheet. Currently, I have copied a formula to a number of rows, however this means that I need to manually copy this formula as the number of records increases.

Is it possible for the calculated average to be added to the designated cell as each new record is added in the same code that adds the new record via a command button of might a separate macro need to be run?

Additionally, the average record will amended during the day as addition as information becomes available - Blood Sugar Readings Morning (Initial New record) & Midday and Evening (Updated Records).
Morning = Column G
*Midday = Column H
*Evening = Column I
*Average = Column J
* The value of these columns will change during the day so the code must work at input and update.

If this can be achieved through the command button, then I will subsequently need similar code to apply an average to a range of the new / updated record and the 6 previous records. Something for another post maybe

VBA Code:
Dim CurrentRow          As Long
Dim wsHealthStatistics  As Worksheet
Private Sub UserForm_Initialize()

    Set wsHealthStatistics = ThisWorkbook.Worksheets("Daily Records")
    
    Call cmdClearForm_Click
    
End Sub
Private Sub cmdInputRecord_Click()
    Dim answer      As VbMsgBoxResult
    Dim AddRecord   As Boolean
    
    AddRecord = Val(Me.cmdInputRecord.Tag) = xlAdd
    
    answer = MsgBox(IIf(AddRecord, "Add New", "Update Current") & " Record?", 36, "Information")
    If answer = vbYes Then
    
    If AddRecord Then CurrentRow = wsHealthStatistics.Range("A" & wsHealthStatistics.Rows.Count).End(xlUp).Row + 1
    
    On Error GoTo myerror
    
    
    With wsHealthStatistics
        .Cells(CurrentRow, 1).Value = DTPicker1.Value
        .Cells(CurrentRow, 2).Value = txtWeight.Value
        .Cells(CurrentRow, 3).Value = txtBloodOxygen.Value
        .Cells(CurrentRow, 4).Value = txtPulseRate.Value
        .Cells(CurrentRow, 5).Value = txtBPSystolic.Value
        .Cells(CurrentRow, 6).Value = txtBPDiastolic.Value
        .Cells(CurrentRow, 7).Value = txtBGMorning.Value
        .Cells(CurrentRow, 8).Value = txtBGMidday.Value
        .Cells(CurrentRow, 9).Value = txtBGEvening.Value
        .Cells(CurrentRow, 15).Value = txtDailySteps.Value
    End With
            
    
    MsgBox "Record has been " & IIf(AddRecord, "added", "Updated") & " to the database", 64, "Information"
    
    End If
    
    'Scroll the visible worksheet to lst 20 rows. ONLY FUNCTIONS ONCE THERE ARE 20 LINES OF INFORMATION
    'With ActiveSheet
    'Application.GoTo Reference:=.Cells(.Rows.Count, "A").End(xlUp).Offset(-20), Scroll:=True
    'End With
    
    Call cmdClearForm_Click
    DTPicker1.SetFocus
    
myerror:
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
  
End Sub
Private Sub cmdCallRecord_Click()
    'Used to call a record based on a date value
    Dim rng     As Range
    Dim Res     As Variant, myfind As Variant
    
    Set rng = wsHealthStatistics.Range("A:A")
    myfind = Me.DTPicker1.Value
    
    If Not IsDate(myfind) Then Exit Sub
    
    myfind = CDate(myfind)
    
    Res = Application.Match(CLng(myfind), rng, 0)
    
    If Not IsError(Res) Then
    
        CurrentRow = CLng(Res)
        
        With wsHealthStatistics
        
            DTPicker1.Value = .Cells(CurrentRow, 1)
            txtWeight.Value = .Cells(CurrentRow, 2)
            txtBloodOxygen.Value = .Cells(CurrentRow, 3)
            txtPulseRate.Value = .Cells(CurrentRow, 4)
            txtBPSystolic.Value = .Cells(CurrentRow, 5)
            txtBPDiastolic.Value = .Cells(CurrentRow, 6)
            txtBGMorning.Value = .Cells(CurrentRow, 7)
            txtBGMidday.Value = .Cells(CurrentRow, 8)
            txtBGEvening.Value = .Cells(CurrentRow, 9)
            txtDailySteps.Value = .Cells(CurrentRow, 15)
        End With
        
        'Update submit commandbutton ststus
        With Me.cmdInputRecord
            .Tag = xlUpdateState
            .Caption = "Update"
            .BackColor = rgbGreen
        End With
        
    Else
    
        MsgBox "Date Not Found", vbInformation, "Date Not Found"
        
        End If
     
End Sub
'Private Sub txtWeight_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtWeight = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBloodOxygen_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBloodOxygen = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtPulseRate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtPulseRate = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBPSystolic_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBPSystolic = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBPDiastolic_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBPDiastolic = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBGMorning_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBGMorning = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBGMidday_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBGMidday = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtBGEvening_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtBGEvening = Format(TextBox1, "#,##0.0")
'End Sub
'Private Sub txtDailySteps_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'txtDailySteps = Format(TextBox1, "#,##0.0")
'End Sub
Private Sub cmdClearForm_Click()
'Clears the User Form
    DTPicker1.Value = ""
    txtWeight.Value = ""
    txtBloodOxygen.Value = ""
    txtPulseRate.Value = ""
    txtBPSystolic.Value = ""
    txtBPDiastolic.Value = ""
    txtBGMorning.Value = ""
    txtBGMidday.Value = ""
    txtBGEvening.Value = ""
    txtDailySteps.Value = ""
    
    DTPicker1.SetFocus
    
    With Me.cmdInputRecord
        .Tag = xlAdd
        .Caption = "Add Record"
        .BackColor = rgbYellow
    End With
End Sub
Private Sub cmdCloseForm_Click()
    Unload Me
End Sub

Any help will be gratefully received

 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Unfortunately, no possible solutions have been advised, mean while I continue to search for a solution.

I have found a possible solution however the destination cell now shows #NAME? with the following addition to the code.(Highlighted)

VBA Code:
With wsHealthStatistics
        .Cells(CurrentRow, 1).Value = DTPicker1.Value
        .Cells(CurrentRow, 2).Value = txtWeight.Value
        .Cells(CurrentRow, 3).Value = txtBloodOxygen.Value
        .Cells(CurrentRow, 4).Value = txtPulseRate.Value
        .Cells(CurrentRow, 5).Value = txtBPSystolic.Value
        .Cells(CurrentRow, 6).Value = txtBPDiastolic.Value
        .Cells(CurrentRow, 7).Value = txtBGMorning.Value
        .Cells(CurrentRow, 8).Value = txtBGMidday.Value
        .Cells(CurrentRow, 9).Value = txtBGEvening.Value
       [B] .Cells(CurrentRow, 10).Formula = "=Average(G, H, I)"[/B]
        .Cells(CurrentRow, 15).Value = txtDailySteps.Value
        .Cells(CurrentRow, 17).Value = Cells(CurrentRow - 1, 17).Value + 1
           
    End With

Please can some help.

Thank You
 
Upvote 0
Still without any suggestions, so this "Silver Surfer" is going to try another site.

Shame because I have always found the volunteers on here so very helpful in the past.
 
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,126
Members
452,303
Latest member
c4cstore

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top