code to work with newly added table row

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a button that once pressed, adds rows to a table. How do I ensure each newly added row is .69cm height?

VBA Code:
Sub Addlines()
    Application.EnableEvents = False
    'ActiveSheet.Unprotect Password:="CSSadmin"
        Dim ws As Worksheet, x As Long, tbl As ListObject, n As Long
       On Error GoTo cancelled:
       n = InputBox("How many lines would you like to add ?")
        Set ws = ActiveSheet
        Set tbl = ws.ListObjects("CSS_quote")
        'add 5 rows
        For x = 1 To n
            'add a row at the end of the table
            tbl.ListRows.Add
           ' ActiveSheet.ListObjects("CSS_quote").DataBodyRange.Columns(12).Value = 1 - 0.1 * ActiveSheet.chkIncrease.Value
            Range("A:A").NumberFormat = "dd/mm/yyyy"
        Next x
    tbl.Range.Cells(tbl.ListRows.Count - n + 2, 1).Select
    'ActiveSheet.Protect Password:="CSSadmin"
    Application.EnableEvents = True
cancelled:
    Exit Sub
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this

VBA Code:
Sub Addlines_2()
  Dim n As Variant, tbl As ListObject, x As Long
  
  n = Application.InputBox("How many lines would you like to add ?")
  If n = "" Or Not IsNumeric(n) Or n = False Then Exit Sub
  
  Set tbl = ActiveSheet.ListObjects("CSS_quote")
  'ActiveSheet.Unprotect Password:="CSSadmin"
  Application.EnableEvents = False
  For x = 1 To n
    tbl.ListRows.Add
    tbl.ListRows(tbl.ListRows.Count).Range.RowHeight = Application.CentimetersToPoints(0.69)
  Next x
  Range("A:A").NumberFormat = "dd/mm/yyyy"
  tbl.Range.Cells(tbl.ListRows.Count - n + 2, 1).Select
  Application.EnableEvents = True
  'ActiveSheet.Protect Password:="CSSadmin"
End Sub
 
Upvote 0
Thanks Dante, does the code you provided do everything that my code does?
 
Upvote 0
That's right, but I added the code to modify row height.
Try and tell me.
 
Upvote 0

Forum statistics

Threads
1,215,553
Messages
6,125,483
Members
449,233
Latest member
Deardevil

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