微信扫码
添加专属顾问
我要投稿
使用DeepSeek自动生成Excel VBA代码,实现批量填写工作表的高效工作流程。 核心内容: 1. DeepSeek生成VBA代码,自动化填写Excel工作表 2. 根据汇总信息在多个模版工作表中查找并填写数据 3. 根据特定项的条目数生成多个工作表,自动化处理复杂Excel任务
Sub GenerateNewWorkbook()Dim srcWb As Workbook, destWb As WorkbookDim infoSheet As Worksheet, tempSheet As WorksheetDim criteria As String, lastRow As Long, i As Long, j As LongDim headerDict As Object, dataArr As Variant, filteredData As CollectionDim ws As Worksheet, newWs As Worksheet, cell As RangeDim fieldName As String, colIndex As Long, targetRow As LongDim rowData() As VariantSet srcWb = ThisWorkbookSet infoSheet = srcWb.Sheets("信息总表")criteria = InputBox("请输入需要筛选的使用单位名称:")' 获取信息总表数据lastRow = infoSheet.Cells(infoSheet.Rows.Count, "A").End(xlUp).rowdataArr = infoSheet.Range("A1:V" & lastRow).Value' 创建标题字典Set headerDict = CreateObject("Scripting.Dictionary")For j = 1 To UBound(dataArr, 2)headerDict(Trim(dataArr(1, j))) = jNext j' 筛选目标数据(存储整行数据)Set filteredData = New CollectionFor i = 2 To UBound(dataArr, 1)If dataArr(i, headerDict("使用单位名称")) = criteria ThenReDim rowData(1 To UBound(dataArr, 2))For j = 1 To UBound(dataArr, 2)rowData(j) = dataArr(i, j)Next jfilteredData.Add rowDataEnd IfNext iIf filteredData.Count = 0 Then Exit Sub' 创建新工作簿(不再设置SheetsInNewWorkbook)Set destWb = Workbooks.Add' ===== 处理委托单 =====For i = 1 To filteredData.CountsrcWb.Sheets("委托单").Copy After:=destWb.Sheets(destWb.Sheets.Count)Set newWs = destWb.ActiveSheetnewWs.Name = "委托单_" & i' 替换自动获取内容(新增设备代码格式处理)For Each cell In newWs.UsedRangeIf InStr(cell.Value, "自动获取") > 0 ThenfieldName = Split(cell.Value, "自动获取")(1)fieldName = Trim(fieldName)If headerDict.Exists(fieldName) ThencolIndex = headerDict(fieldName)cell.NumberFormat = "@" ' 强制设为文本格式cell.Value = CStr(filteredData(i)(colIndex)) ' 转换为字符串' 特殊处理设备代码(保留完整数字)If fieldName = "设备代码" Thencell.Value = "'" & CStr(filteredData(i)(colIndex)) ' 添加单引号保留格式End IfEnd IfEnd IfNext cell' 处理拟检测日期(修正无效引用)On Error Resume NextDim detecDate As Date' 提取检测时间并去除时间部分(如存在)Dim rawDate As StringrawDate = filteredData(i)(headerDict("检测时间"))If InStr(rawDate, " ") > 0 ThenrawDate = Split(rawDate, "")(0) ' 仅保留日期部分End IfdetecDate = DateAdd("m", -1, CDate(rawDate))' 找到H列最后一个非空单元格的下方插入新日期With newWsDim lastRowH As LonglastRowH = .Cells(.Rows.Count, "H").End(xlUp).row.Cells(lastRowH + 1, "H").Value = Format(detecDate, "yyyy-mm-dd")End WithOn Error GoTo 0Next i' ===== 处理附表 =====srcWb.Sheets("附表").Copy After:=destWb.Sheets(destWb.Sheets.Count)Set newWs = destWb.ActiveSheetnewWs.Name = "附表"targetRow = 5For i = 1 To filteredData.CountWith newWs' 设备代码特殊处理(C列).Cells(targetRow, 3).NumberFormat = "@".Cells(targetRow, 3).Value = "'" & CStr(filteredData(i)(headerDict("设备代码")))' 其他字段正常写入.Cells(targetRow, 1) = i.Cells(targetRow, 2) = filteredData(i)(headerDict("单位内编号")).Cells(targetRow, 4) = filteredData(i)(headerDict("载重量(kg)")).Cells(targetRow, 5) = filteredData(i)(headerDict("层站数")).Cells(targetRow, 6) = filteredData(i)(headerDict("速度(m/s)")).Cells(targetRow, 7) = filteredData(i)(headerDict("检测时间")).Cells(targetRow, 8) = filteredData(i)(headerDict("费用"))End WithtargetRow = targetRow + 1Next i' ===== 处理符合性声明=====srcWb.Sheets("符合性声明").Copy After:=destWb.Sheets(destWb.Sheets.Count)Set newWs = destWb.ActiveSheetnewWs.Name = "符合性声明"' 设备代码特殊处理(A列)targetRow = 7For i = 1 To filteredData.CountWith newWs.Cells(targetRow, 1).NumberFormat = "@".Cells(targetRow, 1).Value = "'" & CStr(filteredData(i)(headerDict("设备代码"))).Cells(targetRow, 2) = filteredData(i)(headerDict("产品编号")).Cells(targetRow, 3) = filteredData(i)(headerDict("登记证编号")).Cells(targetRow, 4) = filteredData(i)(headerDict("单位内编号"))End WithtargetRow = targetRow + 1Next i' 删除默认Sheet1(如果存在)On Error Resume NextApplication.DisplayAlerts = FalsedestWb.Sheets("Sheet1").DeleteApplication.DisplayAlerts = TrueOn Error GoTo 0' 保存destWb.SaveAs "Generated_Report.xlsx"MsgBox "处理完成!", vbInformationEnd Sub
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-06-18
我让AI替我活了一周。有些事,再也回不去了。
2026-06-17
工单闭环从半天到 6 分钟:我们把 AI Agent 编进了组织架构
2026-06-17
突破制造业效率“隐形天花板”,如何用WorkBuddy砍掉“重复劳动”?
2026-06-11
云原生 - AI Native 多智能体数字人架构实践
2026-06-10
那些跑通 AI 变革的团队做对了什么?
2026-06-10
从 OCR 到智能填单:一家企业如何用大模型把单据录入效率提升 5 倍
2026-06-10
咨询 | 未来的咨询顾问:从执行者,到一个真正的“Agency”
2026-06-08
腾讯打出企业Agent新底牌:WorkBuddy企业版抢占AI办公统一入口
2026-03-26
2026-03-25
2026-04-24
2026-03-25
2026-04-08
2026-04-08
2026-05-15
2026-04-01
2026-03-26
2026-05-15
2026-06-17
2026-06-10
2026-06-08
2026-05-29
2026-05-27
2026-05-26
2026-05-15
2026-05-15