You are here:

Excel/Need help in Macros

Advertisement


Question

Macros Prob
AA      BB      CC
A1      61B     1
A2      61B     1
A3      61B     1
               3<-- Addition

A4      71C     2
A5      71C     2
A6      71C     2
               6<-- Addition


I have a code which segregates the Data in the column BB when it encounters unique value    
and iserts 2 blank rows.

I am looking forward to modify the  code which will Sum the Values in the column CC and would

display them in the inserted row for each type of unique value. ( shown by "Addition")

Can you help me to solve the same.

Please find the code below and the image attached for your ready reference.

Thanks |Ankit Shah |Peoplesoft Consultant |India.

Working Code:
-------------

Sub InsertingRows()

Dim R As Range, enc As Boolean, I As Integer

Set R = ActiveSheet.Range("A1:C100")
I = R.Rows.Count

For I = 3 To 40

If R.Cells(I, 2) <> R.Cells(I - 1, 2) Then

R.Cells(I, 2).Select
ActiveCell.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
 ActiveCell.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cell(I, 2).Value = "61B"
     I = I + 2
  End If
Next I

End Sub


Answer
Sub insertAndSum()
   Dim insertAbove As Range, rngToSum As Range
   Dim rngResult As Range
   Set insertAbove = Range("B2")
   Do
       Do
           Set insertAbove = insertAbove.Offset(1)
       Loop Until insertAbove <> insertAbove.Offset(-1)
       insertAbove.Range("a1:a2").EntireRow.Insert
       Set rngResult = insertAbove.Offset(-2, 1)
       Set rngToSum = Range(rngResult.Offset(-1).End(xlUp), _
           rngResult.Offset(-1))
       rngResult.Formula = "=SUM(" & rngToSum.Address & ")"
   Loop Until insertAbove = ""
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Stuart Resnick

Expertise

I can answer any question relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment

Experience

As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.

©2009 About.com, a part of The New York Times Company. All rights reserved.