I am learning excel vba through an online course.
One of the exercises requires that you you find and replace text in a selected cell for all sheets. Here is my attempt at a solution using a For each loop.
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
This code replaces the value for only one sheet.
Can you please help my figure out why this method is not working? The solution is completely different to I cannot get an answer from that.