Hi,
I'm trying to calculate the weighted average with some criteria (period, store and product) of the Data file and for each average of each product of each store in a certain period, I wanted to paste it into another Workbook (Report) in the worksheets of the respective products.
The problem is that the code I am writing, is not calculating based on these criteria (returns 0.00), if someone can tell me where I am wrong (I am a beginner in VBA), I am grateful.
Detail, I do this via formula, the problem is that they are large files (600,000 lines) and have been too time consuming.
Follow the data for better understanding.
Sorry, I do not speak English, and I'm using google translator.
Thank you for your attention.
<tbody>
</tbody>
<tbody>
</tbody>
My code:
Private Sub CommandButton1_Click()
Call MedPond
End Sub
--------------------------------------------
Sub MedPond()
Application.ScreenUpdating = False
Dim Rng As Range
Dim Loja As String
Dim Prod As Integer
Dim Periodo As Date
Dim Quant As Integer
Dim Punit As Integer
Dim Q As Double
Dim P As Double
Dim PMP As Integer
Workbooks.Open ("C:\Users\Desktop\Relatorio.xlsx")
If Loja = "A" And Prod = "101" And Periodo = "01/01/2017" Then
Q = Rng.Cells(2, Quant)
P = Rng.Cells(2, Punit)
PMP = (Q * P) / Q
End If
Workbooks("Relatorio.xlsx").Worksheets("101").Cells(4, 2) = PMP
If Loja = "A" And Prod = "101" And Periodo = "01/02/2017" Then
Q = Rng.Cells(2, Quant)
P = Rng.Cells(2, Punit)
PMP = (Q * P) / Q
End If
Workbooks("Relatorio.xlsx").Worksheets("101").Cells(5, 2) = PMP
ActiveWorkbook.Save
ActiveWorkbook.Close
MsgBox "Concluído!"
Application.ScreenUpdating = True
End Sub
I intend to:
Calculate the weighted average for each product in each store each month;
Calculate the weighted average of each product in each month for all stores together;
Paste the result into the worksheet of each product.
In B4 it was to come to 5.52
In B5 it was to come the value of 6.00
Worlsheets Product 101
<tbody>
</tbody>
I intend do for worksheets Product 102,Product 103,Product 104 and Product 105
Thank you for your attention.
I'm trying to calculate the weighted average with some criteria (period, store and product) of the Data file and for each average of each product of each store in a certain period, I wanted to paste it into another Workbook (Report) in the worksheets of the respective products.
The problem is that the code I am writing, is not calculating based on these criteria (returns 0.00), if someone can tell me where I am wrong (I am a beginner in VBA), I am grateful.
Detail, I do this via formula, the problem is that they are large files (600,000 lines) and have been too time consuming.
Follow the data for better understanding.
Sorry, I do not speak English, and I'm using google translator.
Thank you for your attention.
Periodo | Store | Product | Amount | Unit Price | Amount x Unit Price | All Stores |
jan/17 | A | 101 | 13 | 6,00 | 78,00 | All |
jan/17 | A | 102 | 14 | 3,00 | 42,00 | All |
jan/17 | A | 102 | 15 | 3,00 | 45,00 | All |
jan/17 | B | 103 | 16 | 4,00 | 64,00 | All |
jan/17 | C | 102 | 12 | 3,00 | 36,00 | All |
jan/17 | E | 103 | 10 | 5,00 | 50,00 | All |
jan/17 | E | 105 | 15 | 4,00 | 60,00 | All |
jan/17 | A | 104 | 12 | 6,00 | 72,00 | All |
jan/17 | D | 104 | 14 | 3,00 | 42,00 | All |
jan/17 | A | 105 | 13 | 5,00 | 65,00 | All |
jan/17 | B | 105 | 12 | 2,00 | 24,00 | All |
jan/17 | C | 101 | 14 | 4,00 | 56,00 | All |
jan/17 | C | 105 | 13 | 5,00 | 65,00 | All |
jan/17 | B | 101 | 12 | 6,00 | 72,00 | All |
jan/17 | B | 101 | 15 | 4,00 | 60,00 | All |
jan/17 | A | 104 | 13 | 3,00 | 39,00 | All |
jan/17 | E | 105 | 16 | 3,00 | 48,00 | All |
jan/17 | D | 102 | 13 | 5,00 | 65,00 | All |
jan/17 | C | 102 | 14 | 6,00 | 84,00 | All |
jan/17 | E | 101 | 13 | 3,00 | 39,00 | All |
jan/17 | A | 101 | 12 | 5,00 | 60,00 | All |
jan/17 | B | 105 | 12 | 4,00 | 48,00 | All |
jan/17 | C | 104 | 13 | 3,00 | 39,00 | All |
jan/17 | C | 103 | 12 | 5,00 | 60,00 | All |
jan/17 | A | 102 | 13 | 2,00 | 26,00 | All |
jan/17 | D | 104 | 12 | 3,00 | 36,00 | All |
jan/17 | E | 101 | 14 | 2,00 | 28,00 | All |
fev/17 | B | 101 | 15 | 3,00 | 45,00 | All |
fev/17 | C | 105 | 12 | 3,00 | 36,00 | All |
fev/17 | C | 101 | 14 | 3,00 | 42,00 | All |
fev/17 | B | 101 | 15 | 4,00 | 60,00 | All |
fev/17 | B | 104 | 12 | 3,00 | 36,00 | All |
fev/17 | A | 105 | 14 | 3,00 | 42,00 | All |
fev/17 | E | 102 | 15 | 5,00 | 75,00 | All |
fev/17 | D | 102 | 12 | 6,00 | 72,00 | All |
fev/17 | C | 101 | 14 | 4,00 | 56,00 | All |
fev/17 | E | 101 | 15 | 5,00 | 75,00 | All |
fev/17 | A | 101 | 12 | 6,00 | 72,00 | All |
fev/17 | B | 105 | 14 | 4,00 | 56,00 | All |
fev/17 | C | 101 | 14 | 4,00 | 56,00 | All |
fev/17 | C | 101 | 13 | 5,00 | 65,00 | All |
fev/17 | A | 104 | 12 | 6,00 | 72,00 | All |
fev/17 | D | 105 | 12 | 4,00 | 48,00 | All |
fev/17 | E | 102 | 13 | 3,00 | 39,00 | All |
fev/17 | B | 102 | 12 | 3,00 | 36,00 | All |
fev/17 | C | 101 | 13 | 5,00 | 65,00 | All |
fev/17 | C | 101 | 12 | 6,00 | 72,00 | All |
fev/17 | A | 103 | 15 | 4,00 | 60,00 | All |
fev/17 | D | 102 | 12 | 2,00 | 24,00 | All |
<tbody>
</tbody>
<tbody>
</tbody>
My code:
Private Sub CommandButton1_Click()
Call MedPond
End Sub
--------------------------------------------
Sub MedPond()
Application.ScreenUpdating = False
Dim Rng As Range
Dim Loja As String
Dim Prod As Integer
Dim Periodo As Date
Dim Quant As Integer
Dim Punit As Integer
Dim Q As Double
Dim P As Double
Dim PMP As Integer
Workbooks.Open ("C:\Users\Desktop\Relatorio.xlsx")
If Loja = "A" And Prod = "101" And Periodo = "01/01/2017" Then
Q = Rng.Cells(2, Quant)
P = Rng.Cells(2, Punit)
PMP = (Q * P) / Q
End If
Workbooks("Relatorio.xlsx").Worksheets("101").Cells(4, 2) = PMP
If Loja = "A" And Prod = "101" And Periodo = "01/02/2017" Then
Q = Rng.Cells(2, Quant)
P = Rng.Cells(2, Punit)
PMP = (Q * P) / Q
End If
Workbooks("Relatorio.xlsx").Worksheets("101").Cells(5, 2) = PMP
ActiveWorkbook.Save
ActiveWorkbook.Close
MsgBox "Concluído!"
Application.ScreenUpdating = True
End Sub
I intend to:
Calculate the weighted average for each product in each store each month;
Calculate the weighted average of each product in each month for all stores together;
Paste the result into the worksheet of each product.
In B4 it was to come to 5.52
In B5 it was to come the value of 6.00
Worlsheets Product 101
Store A | Store B | Store C | Store D | Store E | All Stores | |
jan/17 | 0,00 | |||||
feb/17 | 0,00 | |||||
mar/17 | ||||||
apr/17 | ||||||
mai/17 | ||||||
jun/17 | ||||||
jul/17 | ||||||
ago/17 | ||||||
sep/17 | ||||||
oct/17 | ||||||
nov/17 | ||||||
dec/17 |
<tbody>
</tbody>
I intend do for worksheets Product 102,Product 103,Product 104 and Product 105
Thank you for your attention.