如何在不同工作表中的特定范围上运行宏?

时间:2013-06-11 13:13:18

标签: excel vba

我有一个宏读取范围内的一些数据,并询问我是否要更改它,然后更改任何其他以这种方式相同的单元格。它只能在一张纸上工作,如何更改它以便它搜索所有其他纸张相同的范围?

我这样写,但我不知道为什么其他表中的其他数据不会改变

Sub standardize()
For Each ws In ActiveWorkbook.Worksheets
For Each Title In Range("O1:O2000").Cells
Dim des As String
des = Title.Value
If Left(des, 1) <> "*" And Title.Value <> 0 Then
Dim MyDataObj As New DataObject
MyDataObj.SetText des
MyDataObj.PutInClipboard
newtitle = Application.InputBox(prompt:=des, Title:=" Input The description as you      like!", Type:=2)
If newtitle <> False Then
 For Each cate In ActiveWorkbook.Worksheets.Range("O1:O2000").Cells
 If titl.Value = des Then
 titl.Value = "*" & newtitle
 End If
 Next titl
 Next cate
 End If
 End If
 Next Title
 Next ws
 End Sub

1 个答案:

答案 0 :(得分:0)

编辑1:添加了更好的代码

我删除了你的ws_loop并把它放在一个循环遍历所有工作表的新循环的中间。我还在顶部添加了一个选项,用于在工作表和最后的msgbox之间切换。

Sub standardize()
Dim WS_Count As Integer
Dim I As Integer

' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.
For I = 1 To WS_Count
 ActiveWorkbook.Worksheets(I).select


''''Below is your code without the ws loop

For Each Title In Range("O1:O2000").Cells
Dim des As String
des = Title.Value
If Left(des, 1) <> "*" And Title.Value <> 0 Then
Dim MyDataObj As New DataObject
MyDataObj.SetText des
MyDataObj.PutInClipboard
newtitle = Application.InputBox(prompt:=des, Title:=" Input The description as you      like!", Type:=2)
If newtitle <> False Then
For Each cate In ActiveWorkbook.Worksheets.Range("O1:O2000").Cells
If titl.Value = des Then
titl.Value = "*" & newtitle
End If
Next titl
Next cate
End If
End If
Next Title

MsgBox ActiveWorkbook.Worksheets(I).Name

Next I
End Sub