|
- Private Sub CommandButton1_Click()
- Dim myPath$, myFile$, AK As Workbook, aRow%, tRow%, i As Integer
- Dim m1, m2
- Application.ScreenUpdating = False '冻结屏幕,以防屏幕抖动
- sInt = Application.InputBox(Prompt:="输入起始行数", Type:=1)
- If Len(Trim(sInt)) > 0 Then
- m1 = sInt
- Else
- MsgBox Prompt:="您没有输入有效的行数" & Chr(10) _
- & "正确的月份格式为:(例:1)", Buttons:=vbOKOnly + vbInformation, _
- Title:="错误提示"
- Exit Sub
- End If
- sInt = Application.InputBox(Prompt:="输入起始行数", Type:=1)
- If Len(Trim(sInt)) > 0 Then
- m2 = sInt
- Else
- MsgBox Prompt:="您没有输入有效的行数" & Chr(10) _
- & "正确的月份格式为:(例:1)", Buttons:=vbOKOnly + vbInformation, _
- Title:="错误提示"
- Exit Sub
- End If
- myPath = ThisWorkbook.Path & "" '把文件路径定义给变量
- myFile = Dir(myPath & "*.xls") '依次找寻指定路径中的*.xls文件
- Do While myFile <> "" '当指定路径中有文件时进行循环
- If myFile <> ThisWorkbook.Name Then
- Set AK = Workbooks.Open(myPath & myFile) '打开符合要求的文件
- m = ActiveSheet.UsedRange.Rows.Count
- n = ActiveSheet.UsedRange.Columns.Count
- AK.ActiveSheet.Cells(m1, 1).Resize(m2 - m1 + 1, n).Replace What:="A", Replacement:="b", LookAt:=xlPart, MatchCase:=False, MatchByte:=False
- AK.ActiveSheet.Cells(m1, 1).Resize(m2 - m1 + 1, n).Replace What:="s", Replacement:="d", LookAt:=xlPart, MatchCase:=False, MatchByte:=False
- AK.ActiveSheet.Cells(m1, 1).Resize(m2 - m1 + 1, n).Replace What:="f", Replacement:="g", LookAt:=xlPart, MatchCase:=False, MatchByte:=False
- Workbooks(myFile).Close savechanges:=True '关闭源工作簿
- End If
- myFile = Dir '找寻下一个*.xls文件
- Loop
- Application.ScreenUpdating = True '冻结屏幕,此类语句一般成对使用
- MsgBox "替换完成,请查看!", 64, "提示"
- End Sub
复制代码 满意给个最佳
|
|