VBA Self updating data?

Christian S

New Member
Joined
Aug 6, 2019
Messages
10
Hi Everyone,

I was hoping for some help with a piece of code that keeps tripping me up. I work for a store and have lots of pages relating to different kinds of product. I'm attempting to add them all to one "master list" that contains data for a pivot table. New products get added via a userform that allows input of the product name and the first month that the product was sold (months are pre-loaded into a combobox and new months can be added when needed.) These new product are automatically added to the bottom row of the master list. Data will be inputted for each product in the list at the end of each month, and the master list contains an "average sold per month" column for this data. However, once the macro has been run and a product added it will only give an average of the data already included. Any new sales months added will not affect the data in the master list or resulting pivot table. Does anyone know a way to set this to update as new data is entered? I've included my userform code below for reference. Many thanks in advance.
Code:
Private Sub btn_AddProduct_Click()


Dim i As Integer, CurrentSheet As String, yrow As Integer, AvgSales As Variant, FirstMonth As String, FirstMonthSales As Variant


CurrentSheet = ActiveSheet.Name


Call FindNextCell
ActiveCell.Value = txt_Name


yrow = ActiveCell.Row


FirstMonth = combo_FirstMonth.Value
 ycol = (Range("Episodes").Find(FirstMonth).Column)


FirstMonthSales = Application.InputBox("How Many Sold In First Month", "First Month Sales", , , , , , 1)
 Cells(yrow, ycol).Value = FirstMonthSales


Cells(yrow, 250).Formula = "=average($B$" & yrow & ":$IN$" & yrow & ")"
  
 AvgSales = Cells(yrow, 250).Value


With Worksheets("Pivot Data").Select
Dim FirstCell As String
Dim j As Integer
FirstCell = "A1"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop


xrow = ActiveCell.Row


Cells(xrow, 2).Value = txt_Name.Value
Cells(xrow, 1).Value = CurrentSheet
Cells(xrow, 3).Formula = AvgScore


End With


Worksheets(CurrentSheet).Select




End Sub
 
Last edited by a moderator:
Yes spot on. And if i ran the code in the original post with that data, apples would be added to my pivot data sheet master list with an average of 266.66. However, if i then added oct-19 with a total of 400, the result in column 250 would change to 300. However, my masterlist 'aveage per month' column would still show 266.66 and so would my pivot table.
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How is the "average per month" column getting updated?
 
Upvote 0
Well the problem is that it isn't updating. But these lines of my code should mean that it is...

Cells(yrow, 250).Formula = "=average($B$" & yrow & ":$IN$" & yrow & ")"
AvgSales = Cells(yrow, 250).Value

Cells(xrow, 3).Formula = AvgSales
 
Upvote 0
In the code you posted originally you have
Code:
Cells(xrow, 3).Formula = Avg[COLOR=#ff0000]Score[/COLOR]
 
Upvote 0
In that case if the formula is updating correctly, I can't see why the master wouldn't.
You can try making these two changes
Code:
AvgSales As [COLOR=#ff0000]Double[/COLOR]
Cells(xrow, 3).[COLOR=#ff0000]Value[/COLOR]= AvgSales
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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