Consulting

Results 1 to 4 of 4

Thread: Inserting a New Row

  1. #1
    VBAX Regular
    Joined
    Sep 2008
    Posts
    29
    Location

    Inserting a New Row

    Hello..I am looking to insert a new row based on a criteria. In Col. 'K', I have values '0' and '-1'. I have the column sorted so that all the '0' are sorted first then follows the '-1'. What I want is right after the last row where cell value in COl. 'K' is '0', insert a new row and type in 'Sales' in Col. 'K'. For example:

    Before:

    FruitsDeptStockApple010Orane020Papaya030Mango-140Strawberry-150Banana-160

    After:

    FruitsDeptStockApple010Orane020Papaya030SALESMango-140Strawberry-150Banana-160

    Thank you in advance!

  2. #2
    VBAX Regular
    Joined
    Sep 2008
    Posts
    29
    Location

    Inserting a new line

    Attaching a sample copy.

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hey Suriyahi,

    I didn't insert a whole row, and of course adjust the sheet codename or tab name as req'd.

    Have a good day,

    Mark

    [VBA]
    Sub InsertSales()

    Dim strRow As String

    strRow = Sheet1.Range("K:K").Find(What:="-1", After:=Sheet1.Range("K1"), LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Row
    Sheet1.Range("J" & strRow & ":L" & strRow).Insert Shift:=xlDown
    Sheet1.Range("K" & strRow).Value = "SALES"

    End Sub
    [/VBA]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Your example uses column F not K as stated, so I have updated GTO's code to reflect this, and also to use a Long not a string (?) to store the found row, and added error handling in case it is not found

    [vba]

    Dim nRow As Long

    With Sheet1

    On Error Resume Next
    nRow = .Range("F:F").Find(What:="-1", _
    After:=.Range("F1"), _
    LookIn:=xlValues, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlNext).Row
    On Error GoTo 0
    If nRow > 0 Then

    .Range("E" & nRow).Resize(, 3).Insert Shift:=xlDown
    .Range("F" & nRow).Value = "SALES"
    End If
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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