title: How to create a table using vba?
body: <p>I'm writing a simple code to get some key figures from a database import/export. I want to make it user friendly, basically &quot;one buttton click&quot; gives the result.</p>
  <p>When I wrote the code, I use Alt+F11  and &quot;Play&quot; =&gt; it works. But when I inserted the Active X-Control &quot;CommandButton1_Click&quot; the macro stops.</p>
  <p>Here is where (first line):</p>
  <pre><code>         ActiveSheet.ListObjects.Add(xlSrcRange, Range(&quot;A$1:$Be$1500&quot;), , xlYes).Name = _
      &quot;Table1&quot;
  
  Range(&quot;Table1[#All]&quot;).Select
  ActiveSheet.ListObjects(&quot;Table1&quot;).TableStyle = &quot;TableStyleLight1&quot;
  </code></pre>
  <p>This is <strong>exactly the same code which works with the play button</strong>.</p>
  <p>I get the error message:</p>
  <blockquote>
  <p>&quot;Run-time Error 1004:</p>
  <p>The worksheet range for the table data must be on the same sheet as the table beeing created.&quot;</p>
  </blockquote>
  <p>Does anyone have an idea?</p>
  <p>An improvement to my code would be, to have the table length variable: There might be more rows in one set, and fewer in another data set.</p>