I have a sheet that contain table (produced from jasper report query). This table will be the source of my pivot table. The Pivot is created using external connection (From Microsoft Query). since the source table need to defined before before can be used in Micrososft Query, could anyone show me how to do it programatically?

INFO:

  1. There are 2 documents here, first is protected source data and the second is Pivot document.
  2. The data is dynamic and the table contain header

Is there any way to define table area using excel programatically with dynamic data?

share|improve this question

feedback

3 Answers

up vote 1 down vote accepted

To answer your comments from the two previous answers (whose, in my opinion, fit your need).

Here is a way to define named range with vba:

Dim Rng1 As Range 
'Change the range of cells (A1:B15) to be the range of cells you want to define
Set Rng1 = Sheets("Sheet1").Range("A1:B15") 
ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1 

Source

Here is a way to create a table with vba (remember it will only work on Excel 2007 or higher):

Sub CreateTable()
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$1:$D$16"), , xlYes).Name = _
        "Table1"
        'No go in 2003
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub

Source

share|improve this answer
feedback

If you use a table (defined) , you can call table object sample

Sub DefineTable()

Dim tbl As ListObject

Set tbl = Sheets("Plan1").ListObjects(1)

tbl.Range.Select

End Sub

Otherwise is create a dynamic range using a name for example

=OFFSET(Plan1!A1;0;0;counta(Plan1!A:A);counta(Plan1!1:1))

Select a name to this range, and in your pivot define a range at =NameOfInterval

[]'s

share|improve this answer
I need to define the table/ area a name, how this could be achived? – indrap Sep 20 '11 at 6:22
feedback

You can use a dynamic range name which expands automatically to your data size - no VBA required

See Debra's write up at http://www.contextures.com/xlpivot01.html#Dynamic

share|improve this answer
I need this using VBA because this will be inserted inside my VBA Application. – indrap Sep 20 '11 at 6:20
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.