用VBA将A表F列G列H列的内容从第3行开始复制到B表的B4,B5,D5单元格中,然后再以B表的B4+face命名为新的excel保存,直至最后一行
我现在的写的是这个,可是怎么都运行不了,不是不能连续命名,就是跳出来的内容不对,有大能能帮忙看看吗?我自己整了几个礼拜了😂
Sub GenerateCoverReports()
Dim wbA As Workbook '时间登记表
Set wbA = Workbooks.Open("D:\xx\xx\A.xlsx") 'A
Dim wbB As Workbook 'B
Set wbB = Workbooks.Open("D:\xx\xx\B.xlsx") 'B
Dim wsA As Worksheet
Set wsA = wbA.Sheets("汇总") ' 修改为A表实际名称
Dim lastRow As Long, i As Long
Dim wsB As Worksheet
Set wsB = wbB.Sheets("sheet1") '修改为B表实际名称
Dim newWB As Workbook
Dim savePath As String
Dim fileName As String
' 设置文件路径和表名(根据实际情况修改)
savePath = "D:\xx\xx\自动生成" '保存路径结尾需包含反斜杠
Application.ScreenUpdating = False
'获取F列最后一行
lastRow = wsA.Cells(wsA.Rows.Count, "F").End(xlUp).Row
For i = 3 To lastRow ' 处理F3-最后一行
' 复制B表到新工作簿
wsB.Copy
Set newWB = ActiveWorkbook
With newWB.Sheets(1)
' 更新单元格值
.Range("B4").Value = wsA.Range("F" & i).Value
.Range("B5").Value = wsA.Range("G" & i).Value
.Range("D5").Value = wsA.Range("H" & i).Value
' 生成文件名
fileName = .Range("F" & i).Value & " cover.xlsx"
' 删除可能存在的非法文件名字符
fileName = Replace(fileName, ":", "")
fileName = Replace(fileName, "\", "")
fileName = Replace(fileName, "/", "")
fileName = Replace(fileName, "?", "")
fileName = Replace(fileName, "*", "")
fileName = Replace(fileName, "[", "")
fileName = Replace(fileName, "]", "")
End With
' 保存并关闭新工作簿
On Error Resume Next ' 跳过已存在文件的错误
MkDir savePath ' 如果目录不存在则创建
On Error GoTo 0
newWB.SaveAs savePath & fileName
newWB.Close False
Next i
Application.ScreenUpdating = True
MsgBox "处理完成"
End Sub