+ Reply to Thread
Results 1 to 5 of 5

Copy data into multiple rows using macros and insert zero

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2014
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007
    Posts
    2

    Copy data into multiple rows using macros and insert zero

    Hi All,

    I'm new to Macros and below is my requirement.I need to split my data into multiple rows based on count
    and the first row should have the value but the other rows should have a value as zero

    Input

    Count Value
    1 400
    2 101
    3 300
    4 450

    Output

    Count Value
    1 400
    2 101
    2 0
    3 300
    3 0
    3 0
    4 450
    4 0
    4 0
    4 0

    Any advise

    Thanks in advance

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Copy data into multiple rows using macros and insert zero

    Hi, DeepthiBas9,

    maybe like this (please use a copy of the worksheet for testing and adapt the ranges as needed):

    Sub EF1005692()
    Dim var As Variant
    Dim varTemp As Variant
    Dim lngCounter As Long
    Dim lngInCount As Long
    
    With ActiveSheet
      ReDim var(2 To .Range("A" & Rows.Count).End(xlUp).Row, 1 To 2)
      var = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value
      .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row).ClearContents
      For lngCounter = LBound(var, 1) To UBound(var, 1)
        With .Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
          .Value = var(lngCounter, 1)
          .Offset(0, 1).Value = var(lngCounter, 2)
        End With
        For lngInCount = 1 To var(lngCounter, 1) - 1
          With .Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            .Value = var(lngCounter, 1)
            .Offset(0, 1).Value = 0
          End With
        Next lngInCount
      Next lngCounter
    End With
    End Sub
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Copy data into multiple rows using macros and insert zero

    Try this:

    InsertRows.xlsm
    1. Reply to thread and inform if suggestion was helpful or not
    2. Click on the star (=Add Reputation) if you think someone helped you
    3. Mark [SOLVED] to this thread if solution was found. (On Menu "Thread Tools" > "Mark this thread as solved")

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy data into multiple rows using macros and insert zero

    Maybe:

    Sub DeepthiBas9()
    Dim x As Long
    Dim i As Long
    Application.ScreenUpdating = False
    For i = Range("A" & Rows.count).End(3)(1).Row To 3 Step -1
        x = Range("A" & i).Value - 1
            Range("B" & i).offset(1).Resize(x).EntireRow.Insert
            Range("B" & i).offset(1).Resize(x).Value = 0
            Range("A" & i).offset(1).Resize(x).Formula = "=R[-1]C"
            Range("A" & i).offset(1).Resize(x).Value = Range("A" & i).offset(1).Resize(x).Value
    Next i
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    04-21-2014
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Copy data into multiple rows using macros and insert zero

    Thank You so much everyone for your quick responses!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro - copy and insert multiple rows
    By yedry101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-09-2013, 06:25 PM
  2. Macros to insert 2 blank row and autosum a column after every 200 rows in data range.
    By sachinsingh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-22-2013, 09:55 AM
  3. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM
  4. Macros to insert row& copy formula on multiple sheets
    By Mysore in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-19-2012, 08:12 PM
  5. Copy paste data, insert rows, insert data and change . to -
    By xWiZardx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2010, 01:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1