Я изучаю Excel VBA через онлайн-курс.
Одно из упражнений требует, чтобы вы нашли и заменили текст в выбранной ячейке для всех листов. Вот моя попытка решения с использованием цикла For для каждого.
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
Этот код заменяет значение только для одного листа.
Не могли бы вы помочь мне понять, почему этот метод не работает? Решение полностью отличается от того, что я не могу получить ответ от этого.