-
Jan 5th, 2021, 06:54 AM
#1
Thread Starter
New Member
[RESOLVED] VBA dynamic table
I am new to VBA, but I have some programming knowledge. My question is if it is possible to automatically create a table with as many columns as a value typed in a textbox located into one of the project's sheet? If the answer is yes, then how do I implement it?
-
Jan 5th, 2021, 02:52 PM
#2
Re: VBA dynamic table
yes i am sure it is possible
what sort of a table?
which application?
table in a word document?
database table in access?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 5th, 2021, 04:11 PM
#3
Thread Starter
New Member
Re: VBA dynamic table
Originally Posted by westconn1
yes i am sure it is possible
what sort of a table?
which application?
table in a word document?
database table in access?
I have to do in Excel a table that creates the number of columns using the number that you type in the textbox located in the Excel Sheet. For example if I type the number 5 in Texbox, 5 table colummns will be created.
-
Jan 6th, 2021, 03:51 AM
#4
Re: VBA dynamic table
you can try like
Code:
Set t = ActiveSheet.ListObjects.Add(xlSrcRange, ActiveCell.Resize(, textbox1), , xlNo)
t will return a listobject object variable
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 7th, 2021, 06:29 AM
#5
Thread Starter
New Member
Re: VBA dynamic table
Originally Posted by westconn1
you can try like
Code:
Set t = ActiveSheet.ListObjects.Add(xlSrcRange, ActiveCell.Resize(, textbox1), , xlNo)
t will return a listobject object variable
I tried to use your code in my Command button. The textbox and the button are in Sheet1 and I tried to make the output appear in Sheet2. It worked but I got an error Type mismatch because of the textbox1 and the table is created randomly in Sheet2.
Here is the code:
Private Sub CommandButton1_Click()
Dim t As Worksheet
Sheet2.Activate
Set t = Sheet2.ListObjects.Add(xlSrcRange, ActiveCell.Resize(, TextBox1), , xlNo)
End Sub
-
Jan 7th, 2021, 03:14 PM
#6
Re: VBA dynamic table
you should specify a specific cell rather than activecell, also you need to fully qualify the textbox, to which sheet or form it is on
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 7th, 2021, 05:17 PM
#7
Thread Starter
New Member
Re: VBA dynamic table
Originally Posted by westconn1
you should specify a specific cell rather than activecell, also you need to fully qualify the textbox, to which sheet or form it is on
Thank you, it's working now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|