|
楼主 |
发表于 2021-7-2 15:11
|
显示全部楼层
Sub 工作簿批量重命名()
On Error Resume Next '当程序出错时继续执行下一句
Dim FileName, i As Integer '声明变量,由于GetOpenFilename的返回值是数组,变量只能用变体型
Dim wb As Workbook
Dim ShName As String
'创建一个打开文件的对话框,允许多选,然后将返回值赋予变量fileToOpen
FileName = Application.GetOpenFilename("EXCEL文件,*.xls;*.xlsx", , "请选择EXCEL文件", , True)
If Err.Number > 0 Then Exit Sub '如果有错误,那么结束过程(单击了“取消”键时才会有错误)
For i = 1 To UBound(FileName) '遍历数组,Ubound函数用于计算数组中的数据个数
'ThisWorkbook.Sheets(1).Cells(i, 1) = FileName(i) '将数组中的文件名称逐一入写入到单元格中
Workbooks.Open (FileName(i))
Set wb = ActiveWorkbook
' ThisWorkbook.Sheets(1).Cells(i, 2) = wb.Sheets(1).Name
' ThisWorkbook.Sheets(1).Cells(i, 3) = wb.Sheets(1).Cells(4, 1)
ShName = wb.Sheets(1).Name & wb.Sheets(1).Cells(4, 1) & Timer
'ThisWorkbook.Sheets(1).Cells(i, 4) = ShName
' wb.SaveAs "d:\ssx\excelnew\" & ShName & ".xls" '另存到d:\ssx\excelnew\
' wb.SaveAs ShName & ".xls" '另存到当前文件夹下
' wb.SaveAs "C:\Users\Administrator\Desktop\ssx\excelnew\" & ShName & ".xls"
wb.Close
Name FileName(i) As ShName & ".xls" '重命名
Next i
End Sub
|
|