本帖最后由 peng2006332 于 2023-5-6 14:48 编辑
1.下列代码是GPT给的编程,本人没有一点VBA基础 实在看不懂。也不会改动套用吗? 2.有大神帮编程个VBA吗? ~~谢谢了 -----------------------------------------------------------------------------------------------------
Sub splitData() DimsourceSheet As Worksheet DimlastRow As Long Dim i AsLong Dimmaterial As String Dimapproval As String Dimreport As String DimtargetWorkbook As Workbook DimtargetSheet As Worksheet DimtargetRow As Long
'设置源工作表 SetsourceSheet = ThisWorkbook.Sheets("SourceSheet")
'获取源工作表中的最后一行 lastRow =sourceSheet.Cells(Rows.Count, 1).End(xlUp).Row
'循环遍历每一行,并提取所需的资料 For i = 2To lastRow '从第二行开始,因为第一行通常是标题行 '提取料号,品名,图号和材料 material = sourceSheet.Cells(i, 2).Value
'提取批准书号 approval = sourceSheet.Cells(i, 5).Value
'提取量测报告 report = sourceSheet.Cells(i, 6).Value
'根据料号创建一个新工作簿 Set targetWorkbook = Workbooks.Add targetWorkbook.SaveAs Filename:=material & ".xlsx"
'设置目标工作表 SettargetSheet = targetWorkbook.Sheets("Sheet1") targetRow= 2 '从第二行开始,因为第一行通常是标题行
'将数据复制到目标工作表中 targetSheet.Cells(1, 1).Value = "料号" targetSheet.Cells(1,2).Value = "批准书号" targetSheet.Cells(1, 3).Value = "量测报告" targetSheet.Cells(targetRow, 1).Value = material targetSheet.Cells(targetRow, 2).Value = approval targetSheet.Cells(targetRow, 3).Value =report
'在目标工作表中创建一个新表格,并应用指定的表格样式 targetSheet.ListObjects.Add(xlSrcRange,targetSheet.Range("A1:C" & targetRow), , xlYes).Name ="ExtractedData" targetSheet.ListObjects("ExtractedData").TableStyle ="TableStyleMedium2"
'保存目标工作簿并关闭它 targetWorkbook.Save targetWorkbook.Close Next i End Sub |