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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
What is the value of LBound(aryResult)?
 
Upvote 0
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
 
Upvote 0
Solution
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
 
Upvote 0
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.
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,317
Members
448,564
Latest member
ED38

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