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:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How are you adding the New sales months?
 
Upvote 0
Are those columns being inserted before column number 250?
 
Upvote 0
In that case I don't understand what you are saying, as this formula
Code:
Cells(yrow, 250).Formula = "=average($B$" & yrow & ":$IN$" & yrow & ")"
should average all of the data in columns B:IN
 
Upvote 0
At the moment it only averages the data that is there before i run the macro. An example would be if i was running a test on my sheet and had some mock data such as

June 2019 - 300 sold
July 2019 - 400 sold

Then i run the macro and the =average gives 350 as it should. But lets say i add a new month (August 2019 - 500 sold.) The result in the masterlist should be 400, but stays as 350. And because i increment the row each time there is now way to update by running the macro again either.
 
Upvote 0
If the product already exists then surely you should be adding the new sales to the existing row, not creating a new row for it.
 
Upvote 0
I do add to the same row. The new row is for any new products. But once the product is added, the macro moves the active row down to prepare for new products
 
Upvote 0
Are you entering the data like


Book1
ABCDEFGHIJ
1Jan-19Feb-19Mar-19Apr-19May-19Jun-19Jul-19Aug-19Sep-19
2Apples100500200
3Oranges300450100600
4Pears200300700150
List
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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