Share via

VBA Insert Rows Below Instead of Above

Anonymous
2024-03-09T22:09:37+00:00

Hello.

I've inserted an ActiveX Controls Command Button so that I can insert a new row to my worksheet when I click on the Button. However, it does not insert a new row below as I would like it to; it inserts a new row above. Every time I have to insert a new row I want it to always end up being the very last row on my sheet. Is there a simple code that will solve the problem? Below is the coding I have so far:

Private Sub CommandButton1_Click()

Sheets("Spend").Range("D37").Select

ActiveCell.EntireRow.Insert Shift = xlDown

Sheets("Spend").Range("D37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("D37").Select

ActiveCell.Formula = "=LEFT(D37,3)&(VALUE(MID(D37,4,4))+1)&RIGHT(D37,1)"

Sheets("Spend").Range("E37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("F37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("G37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("H37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("I37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("J37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("K37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("L37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("M37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("N37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("O37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("P37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("Q37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("R37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("S37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("T37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("U37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("V37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

Sheets("Spend").Range("W37").Select

Selection.Borders.LineStyle = xlNone

Sheets("Spend").Range("X37").Select

Selection.Borders(xlEdgeBottom).Weight = xlThin

End Sub

Thank you in advance for your help!

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Anonymous
    2024-03-11T23:52:20+00:00

    Thank you "peiyezhu",

    This fix worked perfectly.

    However, the only problem now is figuring out how to format the newly added rows so that only certain cells are formatted. Now when I insert a new row there is a thin bottom border that is selected for the entire row (not a specific range). You can see what I mean in the image below. In rows 30-32, only certain columns have the thin bottom border (columns: D, F, H, J, L, N, O, P, Q, R, S, T, U, V, X). Is there a solution to fix this?

    I appreciate everyone's assistance in solving my issues.

    Thank you.

    0 comments No comments
  2. Anonymous
    2024-03-10T03:07:45+00:00

    Please, try replacing

    . Cells(4). Formula = "=LEFT(D" & lastRow + 1 & ",3)&(VALUE(MID(D" & lastRow + 1 & ",4,4))+1)&RIGHT(D" & lastRow + 1 & ",1)"

    With

    . .Cells(4). Formula = "=LEFT(D" & lastRow & ",3)&(VALUE(MID(D" & lastRow  & ",4,4))+1)&RIGHT(D" & lastRow & ",1)"

    0 comments No comments
  3. Anonymous
    2024-03-10T00:59:56+00:00

    Hi there

    It seems that a double quote is missing before column letter D in the inserting the formula part code

    Image

    Please, try replacing

    . Cells(4). Formula = "=LEFT(D" & lastRow + 1 & ",3)&(VALUE(MID(D" & lastRow + 1 & ",4,4))+1)&RIGHT(D" & lastRow + 1 & ",1)"

    With

    . Cells(4). Formula = "=LEFT("D" & lastRow + 1 & ",3)&(VALUE(MID("D" & lastRow + 1 & ",4,4))+1)&RIGHT("D" & lastRow + 1 & ",1)"

    Regards

    Jeovany

    0 comments No comments
  4. Anonymous
    2024-03-09T23:39:02+00:00

    Hi Shakiru,

    Thank you very much for your quick response. I did try your code. While it did insert another row on the bottom, it did not format the way I wanted it to. The formula in column "D" did not properly work either. Do you recommend another alternative? I took a couple of screenshots of what my spreadsheet looks like before and after I used your code if that helps.

    Before using the code (before clicking the blue "Insert Row" button):

    After using the code (after clicking the blue "Insert Row" button):

    Thank you so very much for your help!

    Best Regards.

    0 comments No comments
  5. Anonymous
    2024-03-09T23:04:56+00:00

    Hi Meagan S!

    I am Shakiru, an independent advisor and a user like you, and I am glad to be helping you out today.

    Please try this:

    Private Sub CommandButton1_Click() Dim lastRow As Long Dim ws As Worksheet

    ' Set the worksheet object Set ws = ThisWorkbook.Sheets("Spend")

    ' Find the last used row in column D lastRow = ws. Cells(ws. Rows.Count, "D"). End(xlUp). Row

    ' Insert a new row below the last used row ws. Rows(lastRow + 1). Insert Shift:=xlDown

    ' Apply formatting and formulas to the new row With ws. Rows(lastRow + 1) . Borders(xlEdgeBottom). Weight = xlThin . Cells(4). Formula = "=LEFT(D" & lastRow + 1 & ",3)&(VALUE(MID(D" & lastRow + 1 & ",4,4))+1)&RIGHT(D" & lastRow + 1 & ",1)" ' Add additional formatting as needed for other columns End With End Sub

    This code will find the last used row in column D, then insert a new row below it and apply formatting and formulas to the new row accordingly.

    Note: Make sure to replace "Spend" with the actual name of your worksheet if it's different.

    Please let me know how it goes and if you need any other assistance and I will be more than happy to help further.

    Thank you

    Best Regards, Shakiru

    0 comments No comments