Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate price average in textbox on userform

0

Hi

I try populating price average in textbox 6 after merge QTY in textbox4 and writing ID in textbox1 based on column D .

but it just merge without calculate average!

my problem seems in this line

TextBox6.Text = Format(Application.Average(MyPrAv), "#,##0.00")

I hope somebody help me.

thanks

Answer
Discuss

Answers

0
Selected Answer

Leopard

Average is an Excel worksheet function (not an Application feature) so your line:

TextBox6.Text = Format(Application.Average(MyPrAv), "#,##0.00")

should read:

TextBox6.Text = Format(WorksheetFunction.Average(MyPrAv), "#,##0.00")

BUT your variable MyPrAv is a single number, accumulated via the line:

If j = 6 Then MyPrAv = MyPrAv + a(i, j)

so it make no sense to average that, a single number.

You probably need to count the number of matches and (assuming that is>0) divide the accumulated number by that count.

Revision #1 (17 June 2024)

Looking at this again, there's no need for the variable MyPrAv the avarage price paid can be caculaded from existing variables MyTotal and MyTotall as follow (changes in bold and withing the attached revised file)

Sub FilterData()

Dim lindex&
Dim MyTotal As Double, MyTotal1 As Double ', MyPrAv As Double ' don't use
Dim fDate As Date, tDate As Date
Dim txt1 As String, txt2 As String, txt3 As String
Dim i As Long, j As Long, k As Long, iMax As Long

ListBox1.Clear: MyTotal = 0

On Error Resume Next
fDate = DateValue(TextBox2.Text)
tDate = DateValue(TextBox3.Text)
On Error GoTo 0

ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

For i = 1 To UBound(a)
  If TextBox1 = "" Then txt1 = a(i, 4) Else txt1 = TextBox1
  If (LCase(a(i, 4)) Like LCase(txt1) & "*") And ((tDate = 0 And fDate = 0) Or (DateValue(a(i, 1)) >= fDate And DateValue(a(i, 1)) <= tDate)) Then
      k = k + 1
      For j = 1 To 7
          If j = 1 Then  'Change the 1 in this line for the column number where you have the date
              b(k, j) = Format(a(i, j), "dd/mm/yyyy")
          Else
              b(k, j) = a(i, j)
              If j = 5 Then MyTotal = MyTotal + a(i, j)
              'If j = 6 Then MyPrAv = MyPrAv + a(i, j) ' do nothing for this
              If j = 7 Then MyTotal1 = MyTotal1 + a(i, j)
          End If
      Next
  End If
Next
If k > 0 Then ListBox1.List = b
With ListBox1
    .ColumnWidths = "70;100;90;150;80"
    For i = 0 To .ListCount - 1
        .List(i, 4) = Format(.List(i, 4), "#,##0.00")
        .List(i, 5) = Format(.List(i, 5), "#,##0.00")
        .List(i, 6) = Format(.List(i, 6), "#,##0.00")
    Next i
End With
TextBox4.Text = Format(MyTotal, "#,##0.00")
TextBox5.Text = Format(MyTotal1, "#,##0.00")
' TextBox6.Text = Format(Application.Average(MyPrAv), "#,##0.00") ' replace with line below
TextBox6.Text = Format(MyTotal1 / MyTotal, "#,##0.00") ' divide summed price by summed quantity

End Sub

I tried this for item 1200R20 G580 JAP and it gave the correct avearge of 2,484.70 with no data in textboxes 2 and 3 (FD and TD).

Revision #2 (17 June 2024)

Given the user clarified the requirement is the average BATCH price (not the true avarage unit price), the SECOND attached file has the following code (changes in bold):

Sub FilterData()

Dim lindex&
Dim MyTotal As Double, MyTotal1 As Double, MyPrAv As Double
Dim fDate As Date, tDate As Date
Dim txt1 As String, txt2 As String, txt3 As String
Dim i As Long, j As Long, k As Long, iMax As Long
Dim p As Long

ListBox1.Clear: MyTotal = 0

On Error Resume Next
fDate = DateValue(TextBox2.Text)
tDate = DateValue(TextBox3.Text)
On Error GoTo 0

ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

p = 0 ' reset batch counter

For i = 1 To UBound(a)
  If TextBox1 = "" Then txt1 = a(i, 4) Else txt1 = TextBox1
  If (LCase(a(i, 4)) Like LCase(txt1) & "*") And ((tDate = 0 And fDate = 0) Or (DateValue(a(i, 1)) >= fDate And DateValue(a(i, 1)) <= tDate)) Then
      k = k + 1

      For j = 1 To 7
          If j = 1 Then  'Change the 1 in this line for the column number where you have the date
              b(k, j) = Format(a(i, j), "dd/mm/yyyy")
          Else
              b(k, j) = a(i, j)
              If j = 5 Then MyTotal = MyTotal + a(i, j)
              If j = 6 Then
                MyPrAv = MyPrAv + a(i, j) ' add batch prices
                p = p + 1 ' count batches
              End If
              If j = 7 Then MyTotal1 = MyTotal1 + a(i, j)
          End If
      Next
  End If
Next
If k > 0 Then ListBox1.List = b
With ListBox1
    .ColumnWidths = "70;100;90;150;80"
    For i = 0 To .ListCount - 1
        .List(i, 4) = Format(.List(i, 4), "#,##0.00")
        .List(i, 5) = Format(.List(i, 5), "#,##0.00")
        .List(i, 6) = Format(.List(i, 6), "#,##0.00")
    Next i
End With
TextBox4.Text = Format(MyTotal, "#,##0.00")
TextBox5.Text = Format(MyTotal1, "#,##0.00")
If p > 0 Then
    TextBox6.Text = Format(MyPrAv / p, "#,##0.00") ' calculate average batch price
    Else
    TextBox6.Text = ""
End If

End Sub
Discuss

Discussion

Hi John,
WorksheetFunction.Average(MyPrAv)
ok , but doesn't change anything.
You probably need to count the number of matches and (assuming that is>0) divide the accumulated number by that count.
I'm not sure 
do you mean this?  
TextBox6.Text = Format(WorksheetFunction.Application.Average(MyPrAv / count + 1), "#,##0.00")

by the way I don't receive notification by email !
leopard (rep: 94) Jun 16, '24 at 10:43 pm
Leopard.  I'm away from my computer today so can't check your code but if MyPrAv is just a number, (accumulated as you find matches) you can't apply an average to it.

If you haven't used p as a variable, these are the kind of changes I meant (from memory), in bold where ... indicates your existing code:
Dim p as Long
...
p=0
...(start loop)
If j=6 Then
   MyPrAv=   MyPrAv +a(...
   p = p + 1
End If
...(end loop)..
If p>0 Then TextBox6.Text= Format(MyPrAv / p, "#,##0.00")
...


You'll need to work them into your code.

On email notifications, I haven't had any since October 2022! I've mentioned it to Don a few times and he says some people get email alerts, others not. I think some people miss the Answers we give because of this problem.

@Don - any closer to resolving the issue above? 
John_Ru (rep: 6417) Jun 17, '24 at 3:24 am
ok I tested your suggestion, will change the average but it's wrong .
I select the ITEM will show  403.333 , but the right should be 2420 !
leopard (rep: 94) Jun 17, '24 at 6:53 am
Leopard, got to my PC and checked- see correction in Answer under Revision #1 (17 June 2024) plus file.
John_Ru (rep: 6417) Jun 17, '24 at 8:57 am
thanks John,
I realized your way, but I would show  price average and compare with your way(is better to show accurate result) to show decreasing result and  inform the user why price avarage is not accurate.
leopard (rep: 94) Jun 17, '24 at 11:34 am
Leopard

Not sure what you mean by "decreasing result and  inform the user why price avarage is not accurate" but my calculation is the the average unit price paid across the filtered list i.e. the total price divided by the total unit quantity.so an accurate average.

Did you want something different? Like the average price charged per batch (irrespective of the quantity in that batch)? i.e. (Price 1 + --- price n)/n ? That's what I alluded to in one of my discussion points above but somewhat meaningless from a statistical veiwpoint.

Please explain.
John_Ru (rep: 6417) Jun 17, '24 at 12:57 pm
Example :
in textbox1 will take this brand 1200R20 G580 JAP 
the price average =2500+2580+2350+2250/4=2420. this is what i want
your way Divide TOATL/QTY=2,695,899.500/1,085.00=2,484.700 , I don't this way.
leopard (rep: 94) Jun 17, '24 at 1:09 pm
Thanks Leopard- so you do want the average batch price then (not the true average unit price). I'll revise my Answer / file (and your userform) later. 
John_Ru (rep: 6417) Jun 17, '24 at 1:28 pm
See Revision #2 (17 June 2024) to my Answer plus the second file.
John_Ru (rep: 6417) Jun 17, '24 at 1:50 pm
it's excellent!
by the way I see your updating and  you have ever guided me  how do that, but not really what my bad  when shows error in price average , almost I did as you !
anyway thank you for your help me.
leopard (rep: 94) Jun 17, '24 at 3:10 pm
Thanks for selecting my Answer, Leopard. I hope all works well now (sorry but I really don't understand the wording of your last Discussion point above) 
John_Ru (rep: 6417) Jun 17, '24 at 3:20 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login