You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将VBA代码中的WSD设置为活动工作表而非指定静态工作表

修改VBA代码以指向当前活动工作表

Hey there! Let's adjust your VBA code so it targets the currently active worksheet instead of the fixed "1712" sheet. Here's how to do it:

核心修改步骤

  • 替换原来硬编码工作表名称的代码行:
    Set WSD = Worksheets("1712")
    
  • 改成指向当前活动工作表的代码:
    Set WSD = ActiveSheet
    

完整修改后的代码

Sub CreatePivot()
    Dim WSD As Worksheet
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range
    Dim FinalRow As Long
    
    ' 现在指向当前活动的工作表
    Set WSD = ActiveSheet
End Sub

简单说明

ActiveSheet是VBA的内置属性,会自动指向Excel窗口中当前被选中/激活的工作表。这样修改后,你的代码就不再绑定固定的工作表名称,灵活性更高——无论你运行宏时选中的是哪张工作表,代码都会对它生效。

内容的提问来源于stack exchange,提问作者CA Aman Ghotra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:32:09