VBA to add new row at top of table - keep number formatting & size

pg1955

New Member
Joined
Jul 13, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello! I have successfully used a command button to add in a row to the first position in a table, but am looking to also keep the first column formatted as a date. The purpose would be to keep the most recent entry at the top always, to keep wrap text on and to keep the row auto-sizing to the length of text.

This is the current code which is just to add the row for the note:

Private Sub CommandButton1_Click()

ThisWorkbook.Sheets("Notes").Visible = True
ThisWorkbook.Sheets("Notes").Select

ThisWorkbook.Sheets("Notes").Range("A10").Select
ActiveCell.EntireRow.Insert Shift:=xlDown

Sheets("Notes").Range("A10:B10").Select
Selection.Borders.Weight = xlThin

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi ,

you could maybe add this code to the bottom of your command button if I understood well ?

VBA Code:
Selection.NumberFormat = "dd/mm/yyyy"
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Columns.AutoFit
 
Upvote 0
Solution
YES Thank you! I decided instead of the number format to just use "today" because I want all selections time stamped in column A. This is what I ended up with
Private Sub CommandButton1_Click()

ThisWorkbook.Sheets("Notes").Visible = True
ThisWorkbook.Sheets("Notes").Select

ThisWorkbook.Sheets("Notes").Range("A10").Select
ActiveCell.EntireRow.Insert Shift:=xlDown

Sheets("Notes").Range("A10:B10").Select
Selection.Borders.Weight = xlThin

ThisWorkbook.Sheets("Notes").Range("A10").Select
Selection.Formula = "=today()"

With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Columns.AutoFit

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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