Sum values in cells based on a text in another cell

lunatu

Board Regular
Joined
Feb 5, 2021
Messages
77
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hi,

Im copying rows from workbook1 to workbook2 with the code below. Now I would like to modify the code so that if there is Group name in cell B, it would sum all those customers premium who are part of that group. For example like in the picture below.

1641988490684.png


VBA Code:
Sub copysales()

Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

Set wb = Workbooks("Workbook2.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow
Application.ScreenUpdating = True
For rowno = 2 To lRow

If (ThisWorkbook.Sheets("Sheet1").Range("F" & rowno) = Won Then
For colno = 3 To 5
If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) > 0 Then
ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sales").Range("A" & rowToCopy) 'To copy customer name
ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sales").Range("B" & rowToCopy) 'To copy product name
ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy
wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
rowToCopy = rowToCopy + 1

End If
Next
End If
Next
End Sub

Any ideas how to modify the code? :)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Im copying rows from workbook1 to workbook2 with the code below.
I don't think that you are actually using the code posted as it contains an invalid code line. ;)
However, in (a copy of ) your workbook, try making this change

Rich (BB code):
wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
wb.Sheets("Sales").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
 
Upvote 0
I don't think that you are actually using the code posted as it contains an invalid code line. ;)
However, in (a copy of ) your workbook, try making this change

Rich (BB code):
wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
wb.Sheets("Sales").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
Ohe yes, there is a typo :) Thanks for spotting it, below corrected code:

VBA Code:
Sub copysales()

Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

Set wb = Workbooks("Workbook2.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sales").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow
Application.ScreenUpdating = True
For rowno = 2 To lRow

If (ThisWorkbook.Sheets("Sheet1").Range("F" & rowno) = Won Then
For colno = 3 To 5
If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) > 0 Then
ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sales").Range("A" & rowToCopy) 'To copy customer name
ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sales").Range("B" & rowToCopy) 'To copy product name
ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy
wb.Sheets("Sales").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
rowToCopy = rowToCopy + 1

End If
Next
End If
Next
End Sub
 
Upvote 0
I think that you may have missed my point about the code as this line is still invalid ..



.. but did my suggested change to the other line produce the result you wanted?
Hmmm, silly question but change do you mean? I dont see what is the difference there?
 
Upvote 0
I dont see what is the difference there?
The change that I was suggesting was to change the sheet name in the code line shown from "Sheet1" to "Sales"

However, in (a copy of ) your workbook, try making this change

Rich (BB code):
wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
wb.Sheets("Sales").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium

The invalid line that I was referring to, which is a separate issue, was this one:
1642155332057.png


  • It has three opening parentheses and only two closing parentheses.
  • I also believe that Won near the end of the line should be enclose in quote marks: "Won"
 
Upvote 0
The change that I was suggesting was to change the sheet name in the code line shown from "Sheet1" to "Sales"



The invalid line that I was referring to, which is a separate issue, was this one:
View attachment 55209

  • It has three opening parentheses and only two closing parentheses.
  • I also believe that Won near the end of the line should be enclose in quote marks: "Won"
I see, thanks (maybe instead of writing the code straight in here should copy it from the macro...?‍♀️). Below corrected code. But my original question is still open.

VBA Code:
Sub copysales()

Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

Set wb = Workbooks("Workbook2.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sales").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow
Application.ScreenUpdating = True
For rowno = 2 To lRow

If ThisWorkbook.Sheets("Sheet1").Range("F" & rowno) = "Won" Then
For colno = 3 To 5
If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) > 0 Then
ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sales").Range("A" & rowToCopy) 'To copy customer name
ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sales").Range("B" & rowToCopy) 'To copy product name
ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy
wb.Sheets("Sales").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy premium
rowToCopy = rowToCopy + 1

End If
Next
End If
Next
End Sub
 
Upvote 0
In Workbook 2, why are the first 3 "Customer names" actually "Group names"?
If there is a Group Name in column B, I want the Group name to copied not the individual customers names. But if the Group Name cell is empty I want to copy the customer name from column A.
 
Upvote 0
In the results, why is the premium for Group 1, Product 2 1000 and not 2000 per the highlighted values below?

1642419743548.png
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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