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
AI 员工
完全使用deepseek自动填写多个Excel工作表
使用DeepSeek自动生成Excel VBA代码,实现批量填写工作表的高效工作流程。 核心内容: 1. DeepSeek生成VBA代码,自动化填写Excel工作表 2. 根据汇总信息在多个模版工作表中查找并填写数据 3. 根据特定项的条目数生成多个工作表,自动化处理复杂Excel任务
立即咨询 JOTO
JOTO 提供覆盖企业智能体规划与搭建、AI 平台私有化部署、RAG 知识工程、AI 安全治理、FDE 驻场共创及持续运营优化的全周期 AI 落地服务,帮助企业把验证中的 AI 能力转化为安全、可控、可持续迭代的生产力。 联系 JOTO 获取 AI 落地咨询
想把这些做法用到你的业务里?
留下你的场景和痛点,我们帮你判断从哪一步开始。
联系我们