markdown
# Excel 跨工作表批量修改数据指南
## 问题场景
假设存在多个工作表,需要将不同工作表中 **A列相同数值** 对应的 **B列数据** 统一修改为新值。
---
## 方法一:VBA 宏批量处理(推荐)
vba
Sub BatchUpdateCells()
Dim ws As Worksheet
Dim targetValue As String
Dim newValue As String
Dim lastRow As Long
Dim i As Long
' 设置要查找的值和替换的新值
targetValue = "原始值" ' 修改为需要查找的数值
newValue = "新值" ' 修改为目标数值
' 遍历所有工作表
For Each ws In ThisWorkbook.Worksheets
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' 遍历A列数据
For i = 1 To lastRow
If .Cells(i, 1).Value = targetValue Then
.Cells(i, 2).Value = newValue ' 修改B列数据
End If
Next i
End With
Next ws
MsgBox "批量修改完成!"
End Sub
### 操作步骤:
1. 按 `Alt+F11` 打开VBA编辑器
2. 插入新模块并粘贴代码
3. 修改 `targetValue` 和 `newValue`
4. 按 `F5` 运行宏
---
## 方法二:使用公式联动更新
### 步骤:
1. 创建主控表(如"数据源"工作表)
2. 在其他工作表B列使用公式:
excel
=IF(A2=数据源!A2, 数据源!B2, B2)
3. 通过主控表统一修改数据
---
## 方法三:查找替换技巧
1. 按 `Ctrl+H` 打开替换窗口
2. 输入查找内容和替换值
3. 点击 `Options` → 选择 `Workbook` 范围
---
## 注意事项
1. 操作前务必备份原始文件
2. 数据范围较大时推荐使用VBA方法
3. 公式法需要保持数据源位置对应