Results 1 to 7 of 7

Thread: [RESOLVED] VBA dynamic table

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    7

    Resolved [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?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA dynamic table

    yes i am sure it is possible
    how do I implement it?
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    7

    Re: VBA dynamic table

    Quote Originally Posted by westconn1 View Post
    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.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    7

    Re: VBA dynamic table

    Quote Originally Posted by westconn1 View Post
    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

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    7

    Re: VBA dynamic table

    Quote Originally Posted by westconn1 View Post
    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
  •  



Click Here to Expand Forum to Full Width