VBA Problem error 91 - Azure Virtual Desktop - excel

We use our excel reports in our virtual desktop. Now we are testing with our reports in Azure Virtual Desktop and there's a excel report coming out of a error:
Cells.Find(What:="zzz", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
I don't have much experience with this so i don't see what i have to change.
I need some help :)
Thank you in advance.

You should activate cell only after successful search.
Set result = Cells.Find(What:="zzz", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If result is Nothing Then
MsgBox "Nothing found"
Else
result.Activate
End If

Related

want to change a code of ( Cells.Find(What:="28513" ) to Cells.Find(What:="textbox1.value" in a form

this is the code i use by macro i want to change the "28513" to textbox1 and "1509" to textbox2
can u please advice
Private Sub CommandButton1_Click()
Cells.Find(What:="28513", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(What:="28537", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="28537", Replacement:="1509", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Find(What:="28537", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
End Sub
i reach to a solution and gonna share with the community
Private Sub CommandButton1_Click()
Cells.Find(What:=TextBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:=TextBox1.Value, Replacement:=TextBox2.Value, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub

Finding and replacing one column in current sheet on Mac

I found the below code, it works good when "search in sheet".
But sometimes, when I open the excel, it default shows "search worksheet". Please see the image.
Then the code will replace entire sheets instead of Columns("V").
Any additional code can control this thing happens?
Thank you in advance.
Sub ReplaceTitleMs()
Columns("V").Replace What:="Ms ", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
So here is my currently code:
Sub Test_Replace()
Worksheets("Sheet4").Columns("B").Replace What:="XXX", _
Replacement:="KKK", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Worksheets("Sheet4").Columns("B").Replace What:="SIN", _
Replacement:="OOO", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub
When select "search in worksheet" in the menu, and run the code.

When I perform a "Cells.Find" after selecting a column the search unexpectedly leaves the selected column.

When I perform a "Cells.Find" after selecting a column the search unexpectedly leaves the selected column.
I would expect the Find to remain in the column I selected just like when using the 'Find" function in Excel.
'Select first row of data set locations
ActiveWorkbook.Worksheets("Sheet1").Activate
Cells.EntireColumn("C").Select
Set First = Cells.Find(What:=ww_from, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Thanks in advance for your help.
Scott
Cells.Find( is looking at the whole sheet not just what is selected. If you only want to search in column C then use this in place of all you have given:
With ThisWorkbook.Worksheets("Sheet1")
Set First = .Column("C").Find(What:=ww_from, After:=.Range("C1"), LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
End With
If you do not want to change Selection, consider:
ActiveWorkbook.Worksheets("Sheet1").Activate
Dim r as Range
Set r = Columns(3).Cells
Set First = r.Find(What:=ww_from, After:=r(1), LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)

Condensing Find/Replace Excel script

I am looking to condense my script as i still have a long ways to go and even with copying and pasting this will take me a long time. I am just looking to condense the Find/Replace Functions
Function ZoneChanges()
Dim MyCell As range
Worksheets("Sheet1").Activate
Set MyCell = Application.InputBox(Prompt:="Select a cell", Type:=8)
MyCell.Replace What:="EE", Replacement:="DA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
MyCell.Replace What:="EF", Replacement:="DB", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
MyCell.Replace What:="EG", Replacement:="DC", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
MyCell.Replace What:="EH", Replacement:="DD", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Function
Thank you!
If you have to do multiple find and replace, you could put all the values in arrays and run a loop. The problem is, that this would be slower than what you have now. But, purely to shorten the code you could do this.
Function ZoneChanges()
Dim MyCell As Range
Dim arrWhat, arrRep, i As Long
Worksheets("Sheet1").Activate
Set MyCell = Application.InputBox(prompt:="Select a cell", Type:=8)
arrWhat = Array("EE", "EF", "EG", "EH"): arrRep = Array("DA", "DB", "DC", "DD")
For i = LBound(arrWhat) To UBound(arrWhat)
MyCell.Replace What:=arrWhat(i), Replacement:=arrRep(i), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next
End Function

Getting Runtime Error 91

I have this find in a loop. It works fine when there is a "0" but when there are no more "0" I get Runtime Error 91. Any thoughts?
Cells.Find(What:="0", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
You could do this:
Dim rngFound As Range
Set rngFound = Sheets("WhateverSheet").Cells.Find(What:="0", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rngFound Is Nothing Then
'you found the value - do whatever
Else
' you didn't find the value
End if
Ah, or just take out the .Activate at the end. You want the cell range, not activating it.

Resources