excel批量修改多个工作簿文件中同一位置的数据
时间:2022-06-02 10:51:08
最近,有位朋友咨询:“有很多Excel文件是从软件中直接生成的,有些数据**.**KN是软件自动填写上去的,但是因为数据单位的问题,每次都需要将其手动更改为***.*KN。一般来说,都是几十个这样的文件,手动修改的工作量很大,所以想请教一下,看有没有简便的方法。”
他给的工作簿中,要修改两个位置的数据,分别为单元格G27和G54,其格式都是**.**KN,将其修改为***.*KN。如下所示。
使用VBA可以完成上述任务。代码如下:
Sub DatasArrange()
Dim strPath As String
Dim strName As String
Dim Wb As Workbook
Dim rng1 As Range
Dim rng2 As Range
‘获取文件夹路径和工作簿
strPath = ThisWorkbook.Path &”\”
strName = Dir(strPath &”*.xls*”)
On Error Resume Next
Application.ScreenUpdating = False
‘遍历文件夹中的工作簿
‘代码所在的工作簿除外
Do While strName <> “”
If strName <> ThisWorkbook.NameThen
Set Wb = Workbooks.Open(strPath& strName)
‘要修改的单元格
‘可根据实际调整
Set rng1 = Range(“G27”)
Set rng2 = Range(“G54″)
‘传递到修改值的子过程
ModifyDatas rng1, rng2
‘关闭并保存工作簿
Wb.Close True
End If
‘获取下一个工作簿
strName = Dir
Loop
Application.ScreenUpdating = True
End Sub
‘修改所接收的单元格中的值
Sub ModifyDatas(rng1 As Range,rng2 As Range)
On Error Resume Next
‘加一个判断条件,防止每运行一次就更改单元格值
If Mid(rng1, Len(rng1) – 3, 1) <>”.” Then
rng1.Value = Left(rng1.Value,Len(rng1.Value) – 2) * 1 & “KN”
End If
If Mid(rng2, Len(rng2) – 3, 1) <>”.” Then
rng2.Value = Left(rng2.Value,Len(rng2.Value) – 2) * 1 & “KN”
End If
End Sub
DatasArrange过程遍历工作簿所在文件夹中除本工作簿以外的所有工作簿,将工作簿中当前工作表单元格G27和G54中的值进行修改,关闭并保存修改后的工作簿。
运行DatasArrange过程,批量修改文件夹中的工作簿,达到所示的效果。
代码图片版如下:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel2016怎么不显示屏幕提示?
![](https://img.aspxhome.com/file/2023/1/40211_0s.jpg)
word中的拆分功能有什么作用
![](https://img.aspxhome.com/file/2023/8/19068_0s.jpg)
Excel 2010数据已筛选项怎么汇总?
![](https://img.aspxhome.com/file/2023/6/41496_0s.jpg)
Exce怎么画彩色的可变化的心形图案?
![](https://img.aspxhome.com/file/2023/3/a141773_0s.jpg)
如何在word2019文档中如何添加批注呢?
![](https://img.aspxhome.com/file/2023/3/35123_0s.jpg)
如何将 txt 记事本文档插入到 word 文件内容中
![](https://img.aspxhome.com/file/2023/1/33131_0s.jpg)
word中怎样插入flash动画实现播放的效果?
![](https://img.aspxhome.com/file/2023/6/29066_0s.png)
word文档输入数以和英文字母出现乱码方框该怎么办?
![](https://img.aspxhome.com/file/2023/6/28406_0s.jpg)
win10音响耳机怎么同时使用
![](https://img.aspxhome.com/file/2023/6/53006_0s.png)
word字体放大变大教程
![](https://img.aspxhome.com/file/2023/3/33653_0s.jpg)
Excel内容怎么保存为图片 Excel表格内容保存为图片的方法
![](https://img.aspxhome.com/file/2023/6/39636_0s.png)
Microsoft Project软件怎么根据国家节假日设置工作日?
![](https://img.aspxhome.com/file/2023/2/15402_0s.jpg)
利用WPS表格的"重复项"功能解决数据重复输入问题
![](https://img.aspxhome.com/file/2023/6/53526_0s.jpg)
Word中进行设置每章不同页眉的操作方法
Excel可以打勾的方框怎么插入
![](https://img.aspxhome.com/file/2023/3/37193_0s.jpg)
Word中2007版进行设置奇偶页码左右分布的操作方法
WPS怎么放大文字
![](https://img.aspxhome.com/file/2023/8/a163908_0s.png)
iOS 14都会有哪些彩蛋?值得期待吗?
![](https://img.aspxhome.com/file/2023/3/a194603_0s.png)
excel2007自动填充单元格
如何给Excel添加背景图片(图文教程)
![](https://img.aspxhome.com/file/2023/9/43169_0s.jpg)