求EXCEL VBA批量修改文件名的代码示例 求vba代码批量修改文件夹下Excel指定单元格内容?

Excel \u8bf7\u5e2e\u52a9\uff0c\u600e\u4e48VBA\u6279\u91cf\u66f4\u6539\u6587\u4ef6\u540d

'\u65e0\u987b\u6253\u5f00\u6587\u6863\uff0c\u53ef\u4ee5\u66f4\u6539\u5404\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\u7684\u6587\u4ef6\u540d\uff0c\u5bf9\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u6587\u4ef6\u66f4\u540dDim fd As FileDialogDim i As IntegerDim strfiles As String, strextfilename As String, newfilename As StringSet fd = Application.FileDialog(msoFileDialogFolderPicker)If fd.Show = -1 Then strfiles = Dir(fd.SelectedItems(1) & "\*.*") strextfilename = Right(strfiles, Len(strfiles) - InStrRev(strfiles, ".") + 1) Do While strfiles "" i = i + 1 newfilename = "\u65b0\u6587\u4ef6\u540d" & i '\u547d\u540d\u89c4\u5219\u81ea\u5df1\u5b9a\u4e49 Name fd.SelectedItems(1) & "\" & strfiles As newfilename & strextfilename strfiles = Dir LoopEnd IfSet fd = Nothing

Excel\u600e\u6837\u5feb\u901f\u6279\u91cf\u5c06\u76f8\u540c\u8868\u5f0f\u591a\u6587\u4ef6\u6570\u636e\u4fee\u6539

办公室有个批量更改照片的 占个位置 明天上班再贴 半夜懒重写了。

目标:为学员照相 导入电脑并批量修改为学号加姓名

1、首先建立一个EXCEL表 其中第一个工作薄名称为照相顺序表 如下图

Sub 照片重命名()

If MsgBox("程序将重命名与本工作薄同目录下的所有照片文件,确认这样做么?", vbYesNo) <> 6 Then

Exit Sub

End If

Dim oldname As String '旧文件名变量oldname

Dim newname As String '新文件名变量newname

Dim photopath As String '路径变量photopath

Dim nophoto As String '错误提示变量nophoto

Dim i As Integer '循环变量i

photopath = ThisWorkbook.Path '为要修改的文件名路径复制为当前excel文件的路径

For i = 2 To Worksheets("照相顺序表").Range("a65536").End(xlUp).Row '开始循环 从“照相顺序表”工作薄的a2单元格开始

'为新文件名变量赋值为路径变量& \ & 照相顺序表工作薄中的a2&b2单元格内容加上扩展名.jpg

newname = photopath & "\" & Worksheets("照相顺序表").Cells(i, 1).Text & Worksheets("照相顺序表").Cells(i, 2).Text & ".jpg"

'为旧文件名变量赋值为路径变量& \ & 照相顺序表工作薄中的c2单元格内容&扩展名.jpg

oldname = photopath & "\" & Worksheets("照相顺序表").Cells(i, 3).Text & ".jpg"

'判断旧文件名是否在当前目录存在

If Dir(oldname) <> "" Then

Name oldname As newname '存在则改名

Else

nophoto = nophoto & Chr(10) & oldname‘不存在则将其赋值给错误提示变量并以回车分割累加

End If

Next i

If nophoto <> "" Then

MsgBox nophoto & Chr(10) & "图片不存在" ’存在错误提示则弹出错误提示框

End If

End Sub 

备注是刚添加的 希望有所帮助,另外求分谢谢。



你看看这个,可以直接用的
Sub 批量更名()
Dim mypath$, myfile$, newname$, firstname$, rn As Range, t As Boolean
On Error Resume Next
mypath = InputBox("请输入更名文件的文件夹", "输入路径", "C:\文件夹1")
myfile = Dir(mypath & "\" & "*.jpg")
Set rn = Range("C1:C" & [C65536].End(xlUp).Row)
Do While myfile <> "" And firstname <> myfile '电脑写磁盘的动作远比程序运行速度慢所以多加了一个条件能避免改过名的文件再次被更名
Set c = rn.Find(Split(myfile, ".")(0), lookat:=xlWhole)
If Not c Is Nothing Then
oldname = mypath & "\" & myfile
newname = c.Offset(0, -2).Value & c.Offset(0, -1).Value & ".jpg"
Name mypath & "\" & myfile As mypath & "\" & newname
If t = False Then firstname = newname: t = True
End If
myfile = Dir
Loop
End Sub

你要修改什么格式的文件??

扩展阅读:vba批量获取文件名并修改 ... excel批量打印vba代码 ... word vba批量替换 ... vba批量修改工作表名称 ... vba批量合并工作簿代码 ... vba批量修改excel文件 ... vba编程必背50个程序 ... vba编程从入门到精通教程 ... vba修改多个excel文件内容 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网