Insert new rows from array values

ragont

New Member
Joined
Feb 2, 2014
Messages
5
I'm taking data collected from a userform into an array and using the array values to populate columns in a table. I'm finding that additional 'empty data' rows are being created after the array values have been properly populated to the column. I think it has something to do with the 'x' variable count + 1 I am using but not sure how to fix it.

Source Data
Userform collected aryResult (1,7,10,11,12,13,14)
longRowQty = UBound(aryResult) - LBound(aryResult)

Code

Sub Insertrows()

Dim CellTable As ListObject
Dim x As Long, i As Long
Dim ws As Worksheet

Set ws = Worksheets("Cells")
Set CellTable = ws.ListObjects("Celltable")

CellBlockDetails.Show
ResizeTable
x = ws.ListObjects("Celltable").DataBodyRange.Rows.Count 'Current table rows count

Range(CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Centre Code").Index), _
CellTable.DataBodyRange.Cells(x, CellTable.ListColumns("Centre Code").Index)) = strCentreCombo

Range(CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Cell Block Name").Index), _
CellTable.DataBodyRange.Cells(x, CellTable.ListColumns("Cell Block Name").Index)) = strCellBlock

Range(CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Construction").Index), _
CellTable.DataBodyRange.Cells(x, CellTable.ListColumns("Construction").Index)) = strCellorBunk

For i = LBound(aryResult()) To UBound(aryResult())
Range(CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Cell No.").Index), _
CellTable.DataBodyRange.Cells(x, CellTable.ListColumns("Cell No.").Index)) = aryResult(i)
x = x + 1
Next

End Sub

Sub ResizeTable()
Dim x, y, s, e As Long
With Worksheets("Cells").ListObjects("Celltable")
x = .DataBodyRange.Rows.Count 'Current table rows count
y = .DataBodyRange.Columns.Count - 1 'Current table columns count
s = .Range.Cells(1).Row 'Current table start row
e = .Range.Cells(1).Column 'Current table start column
.Resize Range(Cells(s, e), Cells(s + x + longRowQty, e + y).Address) 'Resize table
End With
End Sub

Table being populated
1617240911634.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,467
Office Version
  1. 365
Platform
  1. Windows
What is the value of LBound(aryResult)?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,467
Office Version
  1. 365
Platform
  1. Windows
Use this to define LongRowQty
VBA Code:
longRowQty = UBound(aryResult) - (LBound(aryResult) = 0)
and then for the loop at the end of your code use
VBA Code:
For i = LBound(aryResult) To UBound(aryResult)
   CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Cell No.").Index) = aryResult(i)
   x = x + 1
Next
 
Solution

ragont

New Member
Joined
Feb 2, 2014
Messages
5
That worked thanks Fluff. I did still end up with one extra row but putting "-1" at the end of my longRowQty formula fixed that.
longRowQty = UBound(aryResult) - (LBound(aryResult) = 0) - 1
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,467
Office Version
  1. 365
Platform
  1. Windows
That sounds as though you have a blank/null sting at the end of your array, but glad you sorted it & thanks for the feedback.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,969
Office Version
  1. 2016
Platform
  1. Windows
Use this to define LongRowQty
VBA Code:
longRowQty = UBound(aryResult) - (LBound(aryResult) = 0)
and then for the loop at the end of your code use
VBA Code:
For i = LBound(aryResult) To UBound(aryResult)
   CellTable.DataBodyRange.Cells(x - longRowQty + 1, CellTable.ListColumns("Cell No.").Index) = aryResult(i)
   x = x + 1
Next
If I am not mistaken, your loop can be replaced by this single line of code...
VBA Code:
CellTable.ListColumns("Cell No.").DataBodyRange(1).Resize(UBound(aryResult) - LBound(aryResult) + 1) = Application.Transpose(aryResult)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,361
Messages
5,635,812
Members
416,884
Latest member
leeshjay

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
Top