Results 1 to 5 of 5

Thread: [RESOLVED] Insert a cell based on condition

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2014
    Posts
    33

    Resolved [RESOLVED] Insert a cell based on condition

    Hi. I want to insert a cell based on what is found in column K.

    Here is my code, not sure how to tweak it.

    Code:
     
    
        Dim ws As Worksheet
        Dim LastRow As Long
        Dim j As Long
        
        Set ws = ActiveSheet
        LastRow = ws.Range("K" & Rows.Count).End(xlUp).Row
        
        For j = LastRow To 1 Step -1
            If Left(ws.Range("K" & j).Value, 2) = "PF" Then
                ws.Cells("K" & j).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        Next j
    also, i would kind of like if the cell was inserted with some text.

    Thanks alot.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Insert a cell based on condition

    Change this:

    Code:
    ws.Cells("K" & j).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    to this:

    Code:
    ws.Range("K" & j).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    if you want to move all cells in that row from K on to the right.

    What text do you want added?

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2014
    Posts
    33

    Re: Insert a cell based on condition

    The test would say "finance."

    Thanks!

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Insert a cell based on condition

    then add:

    Code:
    ws.range("k" & j).value="finance"
    after the line which inserts a blank cell.

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2014
    Posts
    33

    Re: Insert a cell based on condition

    thanks once again vbfbryce!

Posting Permissions

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



Click Here to Expand Forum to Full Width