Consulting

Results 1 to 7 of 7

Thread: Solved: Insert row above next empty cell within a column

  1. #1

    Solved: Insert row above next empty cell within a column

    Hi everyone,

    I am having a serious problem with one of my codes. Its holding up my entire project and my boss is growing impatient.

    Any help will be appreciated!!!

    I am trying to develop a code that looks up a specific value ('ComboBox2'). It should then move over one column and down one row (offset (1,1)). At this point I need the code to search downward for the first empty cell and insert a row above it.

    The code that I have is:

    [vba]
    Private Sub Update_Click()
    Dim RowCount As Long
    Dim c As Range

    Set c = Columns(1).Find(ComboBox2.Value, lookat:=xlWhole)
    If c.Offset(1, 2) = "" Then
    Set c = c.Offset(1, 1)
    Else
    '''''' insert row after
    Set c = c.End(xlDown)(2)
    c.Offset(1).EntireRow.Insert
    End If

    c.Offset(, 1) = Me.TextBox1a
    c.Offset(, 4) = Me.TextBox2a
    c.Offset(, 7) = Me.TextBox3a
    c(2).EntireRow.Insert
    End Sub[/vba]

    I welcome any level of guidance.

    Thanks in advance for your time!!!

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ......
    deleted. double post.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    not sure. perhaps...

    [vba]Sub Update_Cl()
    Dim RowCount As Long
    Dim c As Range

    Set c = Columns(1).Find(ComboBox2.Value, lookat:=xlWhole)

    If Not c Is Nothing Then
    Set c = c.Offset(1, 1)
    If c.Offset(1, 0) <> "" Then
    Set c = c.End(xlDown)
    End If
    c.Offset(1, 0).EntireRow.Insert
    c.Offset(, 1) = Me.TextBox1a
    c.Offset(, 4) = Me.TextBox2a
    c.Offset(, 7) = Me.TextBox3a
    c(2).EntireRow.Insert
    Else
    MsgBox "Search item not found!", vbCritical
    Exit Sub
    End If
    End Sub
    [/vba]
    ps: not tested. work on a backup.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Mancubus - Thank you very much for your time.

    I am still not able to figure out how to correct the code. I have provided an attachment with your suggested code applied.

    If possible, can you please pin point where my error lies.

    I am very greatful for any assistance.

    Thanks again!
    Attached Files Attached Files

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.
    test the below code on a backup of your file.
    i added msgbox to display the range address.
    activate sht GC. hit Alt+F8 for macros then doubleclick "update" to see the ranges change in desired order. if stg goes wrong, adjust the "set c =" and "entirerow.insert" offset values.
    test for, eg, combobox2.Value = 5 and 9.

    and last:
    c(2).EntireRow.Insert
    do you really want to insert 2 additional rows after last c?

    [VBA]
    Set c = Columns(1).Find(ComboBox2.Value, lookat:=xlWhole)

    If Not c Is Nothing Then
    MsgBox c.Address 'delete this line after test
    Set c = c.Offset(1, 1)
    MsgBox c.Address 'delete this line after test
    If c.Offset(1, 0) = "" Then
    c.Offset(1, 0).EntireRow.Insert
    Else
    Set c = c.End(xlDown)
    MsgBox c.Address 'delete this line after test
    c.EntireRow.Insert
    End If
    c.Offset(, 1) = Me.TextBox1a
    c.Offset(, 4) = Me.TextBox2a
    c.Offset(, 7) = Me.TextBox3a
    c(2).EntireRow.Insert
    Else
    MsgBox "Search item not found!", vbCritical
    Exit Sub
    End If
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Thank you very much!

    With you help I wound up with the below code:

    [VBA] Set c = Columns(1).Find(ComboBox2.Value, lookat:=xlWhole)

    If Not c Is Nothing Then
    Set c = c.Offset(1, 1)
    c.Offset(0, 0).EntireRow.Insert
    c.Offset(-1, 1) = Me.TextBox1a
    c.Offset(-1, 4) = Me.TextBox2a
    c.Offset(-1, 7) = Me.TextBox3a

    Else
    MsgBox "Search item not found!", vbCritical
    Exit Sub
    End If
    End Sub[/VBA]

    It works great! Much appreciated!!!

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    glad it helped.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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