【问题标题】:Is there any way to define table area using excel programatically with dynamic data?有什么方法可以通过动态数据以编程方式使用 excel 定义表格区域?
【发布时间】:2019-03-07 15:06:56
【问题描述】:

我有一张表,其中包含一个表格(由 jasper 报告查询生成)。该表将是我的数据透视表的来源。 Pivot 是使用外部连接(来自 Microsoft Query)创建的。由于源表需要先定义才能在 Micrososft Query 中使用,任何人都可以告诉我如何以编程方式进行操作吗?

信息:

  1. 这里有 2 个文档,第一个是受保护的源数据,第二个是 Pivot 文档。
  2. 数据是动态的,表格包含标题。

有没有办法通过动态数据以编程方式使用 excel 定义表格区域?

【问题讨论】:

    标签: excel excel-formula vba


    【解决方案1】:

    从前两个答案中回答您的 cmets(在我看来,这符合您的需要)。

    这是一种使用 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

    这是一种使用 vba 创建表格的方法(请记住,它仅适用于 Excel 2007 或更高版本):

    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

    【讨论】:

      【解决方案2】:

      如果使用表(已定义),则可以调用表对象示例

      Sub DefineTable()
      
      Dim tbl As ListObject
      
      Set tbl = Sheets("Plan1").ListObjects(1)
      
      tbl.Range.Select
      
      End Sub
      

      否则使用名称创建动态范围,例如

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

      为此范围选择一个名称,并在您的枢轴中定义一个范围为 =NameOfInterval

      []的

      【讨论】:

      • 我需要为表格/区域定义一个名称,如何实现?
      【解决方案3】:
      Public Function CopyDist() As Variant
      On Error Resume Next
      CopyDist = 0
      '   RemoveTableStyle
      Dim oSh As Worksheet
      Set oSh = ActiveSheet
      '    Set oSh =  'Sheets("Sheet1")
      Dim oNewRow As ListRow
      
      Dim myfirstrow As Integer
      Dim mylastrow As Integer
      Dim myfirstcolumn As Integer
      Dim myValue As Variant
      
      
      myfirstrow = ActiveCell.Row + 1
      mylastrow = ActiveCell.Row + 1
      myfirstcolumn = ActiveCell.Column
      Cells(myfirstrow, myfirstcolumn).Select
      Cells(myfirstrow, myfirstcolumn).Clear
      
      oSh.Range("$A$1:$D$16").Select
      oSh.ListObjects.Add(xlSrcRange, oSh.Range("$A$1:$D$16"), , xlYes).Name = "Table1"
      'No go in 2003
      oSh.ListObjects("Table1").TableStyle = "TableStyleLight2"
      ' CreateTable
      If oSh.ListObjects.Count > 0 Then
        myValue =oSh.ListObjects.Count 
      End If
      RemoveTableStyle
      CopyDist = 1
      End Function
      

      【讨论】:

        【解决方案4】:

        如果您不知道范围大小,以下是处理方法:首先获取最后一行/列的索引引用。然后使用索引创建“Table1”

        Dim lngLastColumn as Long
        Dim lngLastRow as Long
        Set oxlSheet = oxlWB.Worksheets(1) '''or whichever sheet you need    
        
        With oXlSheet
                lngLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
                lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                .ListObjects.Add(xlSrcRange, .Range(.Cells(1, 1), .Cells(lngLastRow, lngLastColumn)), , xlYes).Name = "Table1"
            End With
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-13
          • 1970-01-01
          • 2010-12-25
          相关资源
          最近更新 更多