Macro to create a list length n

TestableEmu263

New Member
Joined
May 19, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
I would appreciate some help with this problem: I want to be able to make a table of n entries long where n is the length based on some cell value. Example. If I want a list of 60 items, I want to be able to type 60 in one cell and then it generate a table 60xm (where m is a predetermine value that stays constant.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Not really sure what you are after. Is it something like the following?

Book1
ABCDEFGHIJ
11*2=2table lengthOperatorm value
22*2=460*2
33*2=6
44*2=8
55*2=10
66*2=12
77*2=14
88*2=16
99*2=18
1010*2=20
1111*2=22
1212*2=24
1313*2=26
1414*2=28
1515*2=30
1616*2=32
1717*2=34
1818*2=36
1919*2=38
2020*2=40
2121*2=42
2222*2=44
2323*2=46
2424*2=48
2525*2=50
2626*2=52
2727*2=54
2828*2=56
2929*2=58
3030*2=60
3131*2=62
3232*2=64
3333*2=66
3434*2=68
3535*2=70
3636*2=72
3737*2=74
3838*2=76
3939*2=78
4040*2=80
4141*2=82
4242*2=84
4343*2=86
4444*2=88
4545*2=90
4646*2=92
4747*2=94
4848*2=96
4949*2=98
5050*2=100
5151*2=102
5252*2=104
5353*2=106
5454*2=108
5555*2=110
5656*2=112
5757*2=114
5858*2=116
5959*2=118
6060*2=120
61
Sheet1


If Yes then you could use code something like the following:

VBA Code:
Sub Test()
'
    Dim mValue          As Double
    Dim TableRow        As Long
    Dim TableRows       As Long
    Dim Operator        As String
'
    TableRows = Range("G2").Value2
    Operator = Range("H2").Value2
    mValue = Range("I2").Value2
'
    ReDim ResultArray(1 To TableRows, 1 To 5) As Variant
'
    For TableRow = 1 To UBound(ResultArray, 1)
        ResultArray(TableRow, 1) = TableRow
        ResultArray(TableRow, 2) = Operator
        ResultArray(TableRow, 3) = mValue
        ResultArray(TableRow, 4) = "="
'
        Select Case Operator
            Case "*": ResultArray(TableRow, 5) = TableRow * mValue
            Case "/": ResultArray(TableRow, 5) = TableRow / mValue
            Case "+": ResultArray(TableRow, 5) = TableRow + mValue
            Case "-": ResultArray(TableRow, 5) = TableRow - mValue
        End Select
    Next
'
    Range("A1").Resize(UBound(ResultArray, 1), UBound(ResultArray, 2)) = ResultArray
'
    With Columns("A:E")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
End Sub
 
Upvote 0
If you want to create a new table, start from cell A1, with 60 rows (in cell G1 = 60) and 4 columns (m=4, for instant)
VBA Code:
Option Explicit
Sub creatTable()
Dim m&, myTable As ListObject
m = 4 ' assum m =4
'Assum table start from cell A1
Set myTable = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1", Cells(Range("G1").Value, m)), , xlYes)
End Sub
1653014035261.png
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,861
Members
449,411
Latest member
adunn_23

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