+ Reply to Thread
Results 1 to 12 of 12

Insert Blank Rows and Copy Rows Above it with Data to These blank rows With Conditions

Hybrid View

pato225 Insert Blank Rows and Copy... 07-29-2015, 11:15 AM
Greg M Re: Insert Blank Rows and... 07-29-2015, 08:52 PM
pato225 Re: Insert Blank Rows and... 07-29-2015, 11:33 PM
event21 Re: Insert Blank Rows and... 07-29-2015, 09:38 PM
pato225 Re: Insert Blank Rows and... 07-29-2015, 11:39 PM
event21 Re: Insert Blank Rows and... 07-30-2015, 12:17 AM
Greg M Re: Insert Blank Rows and... 07-30-2015, 08:17 AM
pato225 Re: Insert Blank Rows and... 07-30-2015, 10:41 AM
JOHN H. DAVIS Re: Insert Blank Rows and... 07-30-2015, 08:58 AM
Steve7913 Re: Insert Blank Rows and... 07-30-2015, 09:17 AM
pato225 Re: Insert Blank Rows and... 07-30-2015, 10:47 AM
Greg M Re: Insert Blank Rows and... 07-30-2015, 12:35 PM
  1. #1
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,671

    Re: Insert Blank Rows and Copy Rows Above it with Data to These blank rows With Conditions

    Hi there,

    The following changes should do what you need:

    
    
    
    Sub pins()
    
        Const sTEST_COLUMN  As String = "E"
        Const sTEST_VALUE_1 As String = "112"
        Const sTEST_VALUE_2 As String = "@@@"
        Const sTEST_VALUE_3 As String = "###"
        Const sSHEET_NAME   As String = "Sheet1"
        Const iSTART_ROW    As Integer = 5
    
        Dim rTestCell       As Range
        Dim rLastCell       As Range
        Dim wks             As Worksheet
    
        Set wks = Worksheets(sSHEET_NAME)
    
        Set rLastCell = wks.Range(sTEST_COLUMN & iSTART_ROW).End(xlDown)
    
        Set rTestCell = rLastCell
    
        Do While rTestCell.Row >= iSTART_ROW
    
            If rTestCell.Value = sTEST_VALUE_1 Or _
               rTestCell.Value = sTEST_VALUE_2 Or _
               rTestCell.Value = sTEST_VALUE_3 Then
    
                   rTestCell.EntireRow.Offset(1, 0).Insert
    
            End If
    
    '       Specify the cell above unless the current cell is at the top of the worksheet
            If rTestCell.Row > 1 Then
                  Set rTestCell = rTestCell.Offset(-1, 0)
            Else: Exit Do
            End If
    
        Loop
    
        Set rTestCell = rLastCell  '   The location of rLastCell is automatically updated as new rows are inserted
    
        Do While rTestCell.Row >= iSTART_ROW
    
            If rTestCell.Value = sTEST_VALUE_1 Or _
               rTestCell.Value = sTEST_VALUE_2 Or _
               rTestCell.Value = sTEST_VALUE_3 Then
    
                rTestCell.EntireRow.SpecialCells(xlCellTypeVisible).Copy
                wks.Paste Destination:=rTestCell.EntireRow.Offset(1, 0)
    
            End If
    
    '       Specify the cell above unless the current cell is at the top of the worksheet
            If rTestCell.Row > 1 Then
                  Set rTestCell = rTestCell.Offset(-1, 0)
            Else: Exit Do
            End If
    
        Loop
    
    End Sub

    Well done for noticing that the first row wasn't tested in the previous version of my code!

    iSTART_ROW is the number of the row from which testing should begin - I think that 5 is probably the correct value for your worksheet.

    These lines are included as a general "safety net", but are probably not required in this instance.

    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M

  2. #2
    Registered User
    Join Date
    01-15-2013
    Location
    Louisville, USA
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert Blank Rows and Copy Rows Above it with Data to These blank rows With Conditions

    Thank you Greg, I will run this and It looks like I might mark this thread resolved soon.

+ 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] insert blank rows every nth row and copy down data to fill blank rows
    By surpass in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-06-2014, 02:55 PM
  2. Replies: 4
    Last Post: 09-16-2014, 10:48 AM
  3. How to create a macro to insert blank rows and copy data into blank rows?
    By zodiack101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2013, 01:18 PM
  4. Copy up to blank cell, transpose, and insert rows without overwriting data below
    By scooter7 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-10-2013, 10:48 PM
  5. [SOLVED] Delete blank rows between data rows, shift rows up, then repeat
    By excelactuary in forum Excel General
    Replies: 2
    Last Post: 03-11-2013, 11:53 AM
  6. [SOLVED] Macro to insert blank rows so that the total number of rows with data is equal to 1021
    By nsm1411 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-07-2013, 10:25 AM
  7. Replies: 1
    Last Post: 05-19-2011, 09:53 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