微信扫码
添加专属顾问
我要投稿
使用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+中大型企业
2025-12-15
人人都能看懂的AI手册:谷歌用 10 个业务场景,讲清AI智能体怎么提效
2025-12-11
招了个AI设计员工,我的一人公司终于配齐了设计部!【附6大用法】
2025-12-11
四个实践案例:快消头部企业纷纷开始将AI应用到业务场景,成效显著!
2025-12-08
咨询 | BCG:如何Build企业级Agent(内附PDF报告)
2025-12-07
悦点科技任鑫琦:收入过亿的 toB Agent 正在重构人机协作
2025-12-05
Anthropic工程师自曝:我每天上班,就是在努力让自己失业
2025-12-04
Claude 母公司内部万字报告:AI 把工程师变成了包工头
2025-12-04
不止于酷炫:B端AI产品的本质--一场关于效率与成本的精准革命
2025-09-22
2025-10-21
2025-10-29
2025-11-30
2025-09-30
2025-10-16
2025-12-04
2025-11-29
2025-12-04
2025-12-01