【问题标题】:Replying to an excel info box from vba从 vba 回复 excel 信息框
【发布时间】:2016-06-07 06:59:32
【问题描述】:

我正在尝试使用以下代码将查询表从 vba 转换为 excel 表。

Set tableRange = Sheets("Fund Performance").Range(Range("C7"),Range("I7").End(xlDown))
On Error Resume Next:
    Sheets("Fund Performance").ListObjects.Add(xlSrcRange, tableRange, , xlYes).Name = Symbol
On Error GoTo 0

如果没有“On Error Resume Next”,我会收到错误 1004 表格不能与包含数据透视表报表、查询结果、受保护的单元格或其他表格的范围重叠。

如果我包含它,则不会显示错误,但也不会创建表。

对于处理从查询表创建 excel 表的这种或替代方法有什么建议吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    虽然这不能解决您问题的主要观点,但我想指出,定义您的 Sheets("Fund Performance").RangeRange objects 没有明确的工作表父母。当留下依赖ActiveSheet property来定义父工作表时,这可以产生一个,

            运行时错误 1004:
            应用程序定义或对象定义错误。

    最佳做法是显式定义所有父工作表引用以构建 Range 对象。

    'verbose method
    Set tableRange = Sheets("Fund Performance").Range(Sheets("Fund Performance").Range("C7"), Sheets("Fund Performance").Range("I7").End(xlDown))
    
    'with ... end with block method; improved readability, faster execution
    With Worksheets("Fund Performance").
        Set tableRange = .Range(.Range("C7"), .Range("I7").End(xlDown))
    End With
    

    在后一种With ... End With statement 方法中,注意使用.Range 而不是Range。前缀句点引用了在 With ... End With 块中注明的父工作表。这意味着父工作表引用只建立一次并重复使用。在前一种详细方法中,父工作表引用建立并重新建立 3 次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2019-07-28
      • 2011-03-08
      相关资源
      最近更新 更多