VBA to insert the data into the first row of the table?

cyrous0425

New Member
Joined
May 30, 2018
Messages
31
Good day guys. My problem is every time I run the macro the data is always inserted at the end of my table. Is there a way to insert it always at first then move down? Here's the code by the way:


eyfue85


Code:
Private Sub Submit_Btm_Click()
Dim n As Long
With Sheets("Record")
    
    n = .Range("B:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    
    .Cells(n, "C") = TextBox1.Value
    .Cells(n, "J") = CDate(TextBox2.Value)
    .Cells(n, "K") = TextBox3.Value
    .Cells(n, "L") = TextBox4.Value
End With
End Sub



Cy
Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi there. Assuming your table has headers in row 1 and data starts in row 2, this should do it:
Code:
Private Sub Submit_Btm_Click()
Dim n As Long
With Sheets("Record")

    .Rows("2:2").Insert Shift:=xlDown
    n = 2
    
    .Cells(n, "C") = TextBox1.Value
    .Cells(n, "J") = CDate(TextBox2.Value)
    .Cells(n, "K") = TextBox3.Value
    .Cells(n, "L") = TextBox4.Value
End With
End Sub
 
Last edited:
Upvote 0
Hi there. I should have added the option to copy the formats from the line below, so the code needed is:
Code:
Private Sub Submit_Btm_Click()
Dim n As Long
With Sheets("Record")

    .Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    n = 2
    
    .Cells(n, "C") = TextBox1.Value
    .Cells(n, "J") = CDate(TextBox2.Value)
    .Cells(n, "K") = TextBox3.Value
    .Cells(n, "L") = TextBox4.Value
End With
End Sub
 
Upvote 0
Sorry. Please see my second post (#3 above) - I forgot to tell it to copy format from below.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top