Consulting

Results 1 to 6 of 6

Thread: Insert Two Blank Row if Condition Met

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    Insert Two Blank Row if Condition Met

    Hi Team


    Need your help in below Situaton.


    In Column B check for each cell , if cell value contain (Rohit,Virat,Dhoni) then
    top of that cell Two Blank Row needs to be inserted


    and Immediate blank row of top of Cell it should be given Thin Border line,


    I have attached workbook which contain Input and Output File,
    I want Output in Inputs files range only,


    Thanks in advance for your valueable time and help.


    Regards,
    mg.
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Why doesn't Dhoni in cell C11 have blank rows above it?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I think your 'Output' missed a Dhoni in C6

    Option Explicit
    Sub Format()
        Dim rName As Range
        
        With Worksheets("Input File")
            Set rName = .Cells(.Rows.Count, 3).End(xlUp)
        
            Do While rName.Row > 1
        
                Select Case rName.Value
                    Case "Rohit", "Virat", "Dhoni"
                        With rName
                            .EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                            .EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                            With .Offset(-2, -1).Resize(1, 3).Borders(xlEdgeBottom)
                                .LineStyle = xlContinuous
                                .Weight = xlMedium
                            End With
                        End With
                                        
                        Set rName = rName.Offset(-3, 0)
                    
                    Case Else
                        Set rName = rName.Offset(-1, 0)
                End Select
            Loop
        
        End With
      
    End Sub
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Hi Paul,

    Thanks you so much . lovely used offset and resize.
    One more small help how to pass dynamically these names from Sheet3.range("a2:a"&lr).value
    for line Case "Rohit", "Virat", "Dhoni"

    Regards,
    mg,

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Try this

    Option Explicit
    
    Sub Format()
        Dim rName As Range, rNames As Range
        Dim aryNames As Variant
        Dim sNames As String
        
        Set rNames = Worksheets("Sheet3").Range("A1:A3")    '   <<<<<<<<< Change
        aryNames = Application.WorksheetFunction.Transpose(rNames)
        
        sNames = Join(aryNames, "#")
        
        With Worksheets("Input File")
            Set rName = .Cells(.Rows.Count, 3).End(xlUp)
            
            Do While rName.Row > 1
                        
                If InStr(sNames, rName.Value) > 0 Then
                    With rName
                        .EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                        .EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                        With .Offset(-2, -1).Resize(1, 3).Borders(xlEdgeBottom)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                        End With
                    End With
                                    
                    Set rName = rName.Offset(-3, 0)
                    
                 Else
                       Set rName = rName.Offset(-1, 0)
                End If
            Loop
        
        End With
      
    End Sub
    
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Thanks you Sir,

Posting Permissions

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