+ Reply to Thread
Results 1 to 4 of 4

Trying to insert rows.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-28-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    2

    Question Trying to insert rows.

    All,

    I'm trying to insert rows between some printed bar codes.

    I've uploaded the Excel file along with a word doc that illustrates what I'm trying to accomplish.

    The Excel file has 2 tabs. One is named "Main" and the other is named "Traveler".
    On the "Main" tab, there is a "Create Files" button, when this is pressed, it creates files outside of the
    Excel spreadsheet, but also populates the "Traveler" tab with file names along with bar codes.
    To facilitate the scanning of the bar codes, I'd like to insert one or two blank rows in between each one.
    A contractor created the Excel file to create our "Main" programs which are utilized in a CNC manufacturing cell.
    I've done some minor edits to it, but I'm unable to figure out how to insert the blank rows.
    Note that I have very little VB knowledge, so I'm looking for some help trying to figure this out.
    Any help would be appreciated.

    Thanks,
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960

    Re: Trying to insert rows.

    Option Explicit
    Sub InsertRows()
        Dim LastRow As Long, _
            NewRow  As Long
        
        Const FirstRow As Long = 9
        
        LastRow = Cells(Rows.Count, "a").End(xlUp).Row
        
        For NewRow = LastRow To FirstRow Step -1
            Range("A" & NewRow).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next NewRow
    End Sub
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    10-28-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Trying to insert rows.

    Thanks for the reply.
    When I use this code, it inserts multiple rows.
    Anywhere from 10 too 18, doesn't seem to be consistent.
    How can I contain it to just add 2 rows between each bar code line?

    Thanks,

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960

    Re: Trying to insert rows.

    Well, unless your actual sheet is different from the posted sample, the original code can't insert random numbers of rows. It only pushes the currently active row down one row. The new code copies the first two blank rows below the data and pastes it in pushing the last row down two, moves up the the next row, copies the two blanks just pasted... tested on the sample provided.

    Option Explicit
    Sub InsertRows()
        Dim LastRow As Long, _
            NewRow  As Long
        
        Const FirstRow As Long = 9
        
        LastRow = Cells(Rows.Count, "a").End(xlUp).Row
        
        'start at the last row
        For NewRow = LastRow To FirstRow Step -1
            
            'copy the first two blank rowsbelow the data
            Range("A" & NewRow).Offset(1, 0).Resize(2).EntireRow.Copy
            
            'insert the two blank rows
            Range("A" & NewRow).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next NewRow
        Application.DataEntryMode = False
        Range("A1").Select
    End Sub
    Attached Files Attached Files
    Last edited by protonLeah; 10-31-2013 at 10:56 PM.

+ 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] VBA codes to insert the rows and copy the first entire row text and insert sheet
    By PRADEEPB270 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2013, 05:05 AM
  2. Insert header , hide rows and insert page breaks
    By dwx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2013, 10:10 AM
  3. Insert header , hide rows and insert page breaks
    By dwx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2013, 03:28 AM
  4. Insert new row & prompt to ask how many rows to insert
    By lady_kay in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-03-2013, 04:34 AM
  5. [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

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