Excel宏助手

64 浏览
4 试用
0 购买
Aug 26, 2025更新

帮助用户为Excel问题创建宏代码

示例1

```vba
Sub 创建预算表()
    Dim ws As Worksheet
    Dim i As Integer
    Dim 月份() As String
    Dim 开始行 As Integer
    
    ' 定义月份数组
    月份 = Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月")
    
    ' 创建新的工作表
    Set ws = ThisWorkbook.Sheets.Add
    ws.Name = "预算表"

    ' 设置标题
    ws.Cells(1, 1).Value = "月份"
    ws.Cells(1, 2).Value = "收入"
    ws.Cells(1, 3).Value = "开支"
    ws.Cells(1, 4).Value = "盈亏"
    ws.Cells(1, 5).Value = "备注"
    
    ' 填充月份数据
    开始行 = 2
    For i = 0 To UBound(月份)
        ws.Cells(开始行 + i, 1).Value = 月份(i)
    Next i
    
    ' 设置公式计算盈亏
    For i = 开始行 To 开始行 + 11
        ws.Cells(i, 4).Formula = "=B" & i & "-C" & i ' 盈亏 = 收入 - 开支
    Next i
    
    ' 添加总计行
    ws.Cells(开始行 + 12, 1).Value = "总计"
    ws.Cells(开始行 + 12, 2).Formula = "=SUM(B" & 开始行 & ":B" & 开始行 + 11 & ")" ' 收入总计
    ws.Cells(开始行 + 12, 3).Formula = "=SUM(C" & 开始行 & ":C" & 开始行 + 11 & ")" ' 开支总计
    ws.Cells(开始行 + 12, 4).Formula = "=B" & 开始行 + 12 & "-C" & 开始行 + 12 ' 盈亏总计

    ' 设置格式
    ws.Columns("A:E").AutoFit
    ws.Range("A1:E1").Font.Bold = True
    ws.Range("A1:E1").Interior.Color = RGB(200, 200, 200)
    ws.Range("B2:D" & 开始行 + 12).NumberFormat = "0.00"
    ws.Range("A" & 开始行 + 12 & ":E" & 开始行 + 12).Font.Bold = True
    
    MsgBox "预算表已创建完成!", vbInformation
End Sub
```

示例2

```vba
Sub CreatePivotTable()
    Dim ws As Worksheet
    Dim pivotWs As Worksheet
    Dim dataRange As Range
    Dim pivotTable As PivotTable
    Dim pivotCache As PivotCache
    Dim pivotTableName As String
    Dim lastRow As Long
    Dim lastCol As Long
    
    ' Set up the source data sheet
    Set ws = ThisWorkbook.Worksheets("SalesData") ' Replace with the name of your sales data sheet
    
    ' Determine last row and column of the data range
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    
    ' Define data range
    Set dataRange = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
    
    ' Add new worksheet for the Pivot Table
    On Error Resume Next
    Set pivotWs = ThisWorkbook.Worksheets("PivotTable")
    On Error GoTo 0
    If pivotWs Is Nothing Then
        Set pivotWs = ThisWorkbook.Worksheets.Add
        pivotWs.Name = "PivotTable"
    Else
        pivotWs.Cells.Clear
    End If
    
    ' Create Pivot Cache
    Set pivotCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=dataRange)
    
    ' Set the name for Pivot Table
    pivotTableName = "SalesPivotTable"
    
    ' Create Pivot Table
    Set pivotTable = pivotCache.CreatePivotTable(TableDestination:=pivotWs.Cells(1, 1), TableName:=pivotTableName)
    
    ' Configure Pivot Table fields
    With pivotTable
        ' Add Row field
        .PivotFields("Date").Orientation = xlRowField
        .PivotFields("Date").Grouping GroupBy:=Array("Months", "Years")
        
        ' Add Column field
        .PivotFields("Region").Orientation = xlColumnField
        
        ' Add Value field
        .PivotFields("Revenue").Orientation = xlDataField
        .PivotFields("Revenue").Function = xlSum
        .PivotFields("Revenue").NumberFormat = "$#,##0.00"
        
        ' Add additional Value field for Quantity if needed
        .PivotFields("Quantity").Orientation = xlDataField
        .PivotFields("Quantity").Function = xlSum
    End With
    
    ' Auto-fit columns on the pivot sheet
    pivotWs.Cells.Columns.AutoFit
    
    MsgBox "Pivot table successfully created.", vbInformation
End Sub
``` 

Make sure to replace `"SalesData"` and column names (e.g., `"Date"`, `"Region"`, `"Revenue"`, `"Quantity"`) with the actual worksheet name and field names from your dataset.

示例3

```vba
Sub 差异数据校准及错误标注()

    ' 获取用户选择的两张表的工作表
    Dim ws表1 As Worksheet
    Dim ws表2 As Worksheet
    Dim ws结果 As Worksheet
    Dim rng表1 As Range
    Dim rng表2 As Range
    Dim dict表1 As Object
    Dim dict表2 As Object

    ' 创建字典对象
    Set dict表1 = CreateObject("Scripting.Dictionary")
    Set dict表2 = CreateObject("Scripting.Dictionary")

    ' 提示用户选择表1范围
    On Error Resume Next
    Set rng表1 = Application.InputBox("请选择表1的对比数据范围(包含标题行)", Type:=8)
    If rng表1 Is Nothing Then Exit Sub
    On Error GoTo 0

    ' 提示用户选择表2范围
    On Error Resume Next
    Set rng表2 = Application.InputBox("请选择表2的对比数据范围(包含标题行)", Type:=8)
    If rng表2 Is Nothing Then Exit Sub
    On Error GoTo 0

    ' 将表1数据存入字典,以第一列为Key,其他列用数组存储为Value
    Dim r As Range
    For Each r In rng表1.Rows
        If r.Row > rng表1.Cells(1, 1).Row Then ' 忽略标题行
            If Not dict表1.exists(r.Cells(1, 1).Value) Then
                dict表1.Add r.Cells(1, 1).Value, Application.Transpose(Application.Transpose(r.Cells.Value))
            End If
        End If
    Next r

    ' 将表2数据存入字典,以第一列为Key,其他列用数组存储为Value
    For Each r In rng表2.Rows
        If r.Row > rng表2.Cells(1, 1).Row Then ' 忽略标题行
            If Not dict表2.exists(r.Cells(1, 1).Value) Then
                dict表2.Add r.Cells(1, 1).Value, Application.Transpose(Application.Transpose(r.Cells.Value))
            End If
        End If
    Next r

    ' 创建新的工作表存放校准结果
    Set ws结果 = ThisWorkbook.Sheets.Add
    ws结果.Name = "校准结果"
    
    ' 写入结果标题
    ws结果.Cells(1, 1).Value = "唯一标识"
    ws结果.Cells(1, 2).Value = "表1数据"
    ws结果.Cells(1, 3).Value = "表2数据"
    ws结果.Cells(1, 4).Value = "校准结果"

    Dim i As Long
    i = 2 ' 从第二行开始写入校准结果

    ' 遍历表1,查找表2中是否存在
    Dim key As Variant
    For Each key In dict表1.keys
        ws结果.Cells(i, 1).Value = key
        ws结果.Cells(i, 2).Value = Join(dict表1(key), ", ")

        If dict表2.exists(key) Then
            ws结果.Cells(i, 3).Value = Join(dict表2(key), ", ")
            If Join(dict表1(key), ", ") = Join(dict表2(key), ", ") Then
                ws结果.Cells(i, 4).Value = "一致"
            Else
                ws结果.Cells(i, 4).Value = "不一致"
            End If
        Else
            ws结果.Cells(i, 3).Value = "表2中不存在"
            ws结果.Cells(i, 4).Value = "缺失数据"
        End If
        i = i + 1
    Next key

    ' 遍历表2,查找表1中是否存在但未处理
    For Each key In dict表2.keys
        If Not dict表1.exists(key) Then
            ws结果.Cells(i, 1).Value = key
            ws结果.Cells(i, 2).Value = "表1中不存在"
            ws结果.Cells(i, 3).Value = Join(dict表2(key), ", ")
            ws结果.Cells(i, 4).Value = "缺失数据"
            i = i + 1
        End If
    Next key

    ' 自动调整列宽
    ws结果.Columns.AutoFit

    MsgBox "差异校准完成!结果已生成在工作表“校准结果”中。", vbInformation

End Sub
```

适用用户

财务人员

通过提示词快速生成自动化Excel宏代码,轻松打造预算表、核对数据并生成标准报表,提高财务工作效率。

数据分析师

自动化完成枯燥重复的数据整理任务,快速生成数据透视表和分析图表,节省分析时间,让结果更直观。

办公人员

无需编程基础,即可利用自动化代码处理复杂表格任务,提升日常办公效率,减少手工操作时间。

教育与培训从业者

轻松制作教学案例及课件,比如生成示例数据表格或自动化模板,提高教学和演示效率。

软件开发者

利用快捷宏代码生成,快速实现复杂Excel插件功能开发,提高工作效率并提升产品交付速度。

解决的问题

帮助用户快速高效地解决与Excel相关的自动化操作问题,通过提供精准的宏代码编写服务,提升工作效率,减少手动操作中的错误,从而实现办公任务的智能化与便捷化。

特征总结

快速生成定制化Excel宏代码,轻松解决特定财务问题,无需手动编写。
支持多种场景化需求,涵盖数据处理、报表生成、自动化操作等多领域应用。
智能优化代码逻辑,确保宏代码高效、稳定运行,从而节省大量时间。
通过简单设置参数即可生成专业级解决方案,完全适配不同的应用场景。
无需编程经验,小白用户也能轻松实现复杂的Excel自动化任务。
完美支持财务场景,如自动化生成预算表、核对报表数据等,提高财务工作效率。
一键调用功能强大,快速满足用户需求,缩短问题解决时间。
代码输出符合标准规范,可无缝应用于实际工作,大幅降低重复劳动。
极具灵活性,通过设置目标内容,可应对个性化问题需求。
内置贴心引导式设计,让用户快速上手,提供流畅的使用体验。

如何使用购买的提示词模板

1. 直接在外部 Chat 应用中使用

将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。

2. 发布为 API 接口调用

把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。

3. 在 MCP Client 中配置使用

在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。

20 积分
平台提供免费试用机制,
确保效果符合预期,再付费购买!

您购买后可以获得什么

获得完整提示词模板
- 共 55 tokens
- 2 个可调节参数
{ 语言 } { 目标 }
自动加入"我的提示词库"
- 获得提示词优化器支持
- 版本化管理支持
获得社区共享的应用案例
限时免费

不要错过!

免费获取高级提示词-优惠即将到期

17
:
23
小时
:
59
分钟
:
59
摄影
免费 原价:20 限时
试用