我正在通过在线课程学习excel vba。
其中一项练习要求您在所有工作表的选定单元格中找到并替换文本。这是我尝试使用For Each循环的解决方案。
Sub Find_Replace_Selected_Value()
' Initialize Variables
Dim inputRange As Range
Dim searchString As String
Dim msgboxResp As VbMsgBoxResult
Dim msgboxRespAll As VbMsgBoxResult
Dim myWorkSheet As Worksheet
' Ask The user if they want the word replaced in all sheets.
msgboxRespAll = MsgBox("Would you like to apply changes to all sheets?", vbYesNoCancel, "Apply to All")
Select Case msgboxRespAll
' if the user only want to apply changes to current sheet.
Case vbNo
' Ask User to select a range to search for.
Set inputRange = Application.InputBox("Please select a range with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
searchString = InputBox("What would you like to replace " & inputRange.Value & " with?", "Replace")
' Perform Find and Replace
Call Cells.Replace(inputRange.Value, searchString)
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
Call MsgBox("This functionality is scheduled for a later release.", vbOKOnly, "Not Available")
End If
' if the user wants to apply changes to all sheets.
Case vbYes
' Ask User to select a range to search for.
Set inputRange = Application.InputBox("Please select a range with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
searchString = InputBox("What would you like to replace " & inputRange.Value & " with?", "Replace")
' Perform Find and Replace
For Each myWorkSheet In Sheets()
myWorkSheet.Activate
Call Cells.Replace(inputRange.Value, searchString)
Next
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
Call MsgBox("This functionality is scheduled for a later release.", vbOKOnly, "Not Available")
End If
End Select
End Sub
此代码仅替换一张纸的值。
您能帮我弄清楚为什么这种方法不起作用吗?解决方案完全不同于我无法从中得到答案。
答案 0 :(得分:0)
对于多页版本,请声明另一个变量以保存输入范围的初始值。
Sub Find_Replace_Selected_Value()
' Initialize Variables
Dim inputRange As Range
Dim searchString As String
Dim replaceString As String
Dim msgboxResp As VbMsgBoxResult
Dim msgboxRespAll As VbMsgBoxResult
Dim myWorkSheet As Worksheet
' Ask The user if they want the word replaced in all sheets.
msgboxRespAll = MsgBox("Would you like to apply changes to all sheets?", _
vbYesNoCancel, "Apply to All")
Select Case msgboxRespAll
' if the user only wants to apply changes to current sheet.
Case vbNo
' Ask User to select a range to search in.
Set inputRange = Application.InputBox("Please select a range " _
& "with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
replaceString = InputBox("What would you like to replace " _
& inputRange.Value & " with?", "Replace")
' Perform Find and Replace
Cells.Replace inputRange.Value, replaceString
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", _
vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
MsgBox "This functionality is scheduled for a later release.", _
vbOKOnly, "Not Available"
End If
' if the user wants to apply changes to all sheets.
Case vbYes
' Ask User to select a range to search in.
Set inputRange = Application.InputBox("Please select a range " _
& "with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
replaceString = InputBox("What would you like to replace " _
& inputRange.Value & " with?", "Replace")
' Assign the value from inputRange to a variable.
searchString = inputRange.Value
' Perform Find and Replace
For Each myWorkSheet In Worksheets
myWorkSheet.Cells.Replace searchString, replaceString
Next
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", _
vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
MsgBox "This functionality is scheduled for a later release.", _
vbOKOnly, "Not Available"
End If
End Select
End Sub
答案 1 :(得分:-1)
问题出在范围变量inputRange上。您正在将此设置为有效工作表的范围参考。因此,当它遍历每张纸时,它是为每张纸参考该范围。因此,实际上,您是用相同的东西替换每张纸上的每个值。
我将inputRange从范围变量更改为简单的字符串变量。这样,对于每个循环它都是静态的。因此,当从输入框获取值时,只需将inputRange更改为inputString并删除“ set”关键字即可。
尝试一下:
Sub Find_Replace_Selected_Value()
' Initialize Variables
Dim inputRange As Range
Dim inputString as String
Dim searchString As String
Dim msgboxResp As VbMsgBoxResult
Dim msgboxRespAll As VbMsgBoxResult
Dim myWorkSheet As Worksheet
' Ask The user if they want the word replaced in all sheets.
msgboxRespAll = MsgBox("Would you like to apply changes to all sheets?", vbYesNoCancel, "Apply to All")
Select Case msgboxRespAll
' if the user only want to apply changes to current sheet.
Case vbNo
' Ask User to select a range to search for.
Set inputRange = Application.InputBox("Please select a range with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
searchString = InputBox("What would you like to replace " & inputRange.Value & " with?", "Replace")
' Perform Find and Replace
Call Cells.Replace(inputRange.Value, searchString)
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
Call MsgBox("This functionality is scheduled for a later release.", vbOKOnly, "Not Available")
End If
' if the user wants to apply changes to all sheets.
Case vbYes
' Ask User to select a range to search for.
inputString = Application.InputBox("Please select a range with a value to find and replace.", "Find", , , , , , 8)
' Ask User to input a value to replace with.
searchString = InputBox("What would you like to replace " & inputString & " with?", "Replace")
' Perform Find and Replace
For Each myWorkSheet In Sheets()
myWorkSheet.Activate
Call Cells.Replace(inputString, searchString)
Next
' Ask User to Repeat
msgboxResp = MsgBox("Would you like to find and replace again?", vbYesNo, "Repeat")
' If yes, tell user that it's a future update.
If msgboxResp = vbYes Then
Call MsgBox("This functionality is scheduled for a later release.", vbOKOnly, "Not Available")
End If
End Select
End Sub