Add 2 rows via VBA Help Needed

gizmo0202

New Member
Joined
Apr 5, 2011
Messages
2
I need to take the data from one spreadsheet that looks like this:

karen 1 3 2 1
craig 2 1 1 1
karen 1 2 1 1

and create a new set of rows that look like this:

karen 2 5 3 2
craig 2 1 1 1

There is no real order to the names - just that they may appear 2 or 3 times in the list.

Thanks for your help!!!
 

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
gizmo0202,


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEF
1karen1321
2craig2111
3karen1211
4
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABCDEF
1karen2532
2craig2111
3
Results





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub SumData()
' hiker95, 04/05/2011
' http://www.mrexcel.com/forum/showthread.php?t=541214
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, LC As Long, LR2 As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
Set wR = Worksheets("Results")
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
LC = w1.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
w1.Rows(1).Insert
w1.Range("A1") = "A"
w1.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wR.Columns(1), Unique:=True
w1.Rows(1).Delete
wR.Rows(1).Delete
LR2 = wR.Cells(Rows.Count, 1).End(xlUp).Row
wR.Range(wR.Cells(1, 2), wR.Cells(LR2, LC)).Formula = "=SUMIF(Sheet1!$A:$A,$A1,Sheet1!B:B)"
Application.ScreenUpdating = True
End Sub


Then run the SumData macro.
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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