Excel/vba: How to skip to next sheet if error? - excel

I'm trying to Find "HFM" from every worksheet and if there is one paste some names after that. So the problem is how to avoid adding the names to the sheet when there is no "HFM" in the sheet?
I first had the code without On Error Resume Next but then there is runtime error. When adding that it copies the names to all sheets. Also it returns runtime error if started from sheet that doesn't have "HFM". So how do I fix this?
Dim ws As Worksheet
For Each ws In Worksheets
Cells.Find(What:="HFM", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
On Error Resume Next
ActiveCell.Offset(0, 3).Select
ActiveCell.FormulaR1C1 = "name1"
' etc.

Instead of On Error Resume Next you will have to declare a variable proceed based on result. An Example below.
Dim ws As Worksheet
For Each ws In Worksheets
' Search for a text
Set MyObjectvie = ws.Cells.Find(What:="HFM", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not MyObjectvie Is Nothing Then
' if the text is found do the task
MyObjectvie.Offset(0, 3).Value = "name1"
End If
Next ws

Related

For loop each workbook

I'm trying to loop through every worksheet in my file and if certain word is found in workbook delete that cell with other eleven cells below.
I give up. My code doesn't work. Can't figure out why.
Can someone help me please?
Sub forEachWs()
Dim ws As Worksheet
Dim find As Range
For Each ws In ActiveWorkbook.Worksheets
Sheets(ws).Select
Set find = Cells.find(What:="nieusprawiedliwiona", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not find Is Nothing Then find.Activate
Range(Selection, Selection.Offset(11, 0)).Select
Selection.EntireRow.Delete
Next ws
End Sub
Ok. I got it working.
Sub forEachWs()
Dim ws As Worksheet
Dim find As Range
For Each ws In Worksheets
MsgBox (ws.Name)
ws.Select
Set find = Cells.find(What:="nieusprawiedliwiona", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not find Is Nothing Then find.Activate
Range(Selection, Selection.Offset(11, 0)).Select
Selection.EntireRow.Delete
Next ws
End Sub
Try without the select/activate:
Sub forEachWs()
Dim ws As Worksheet
Dim find As Range
For Each ws In ActiveWorkbook.Worksheets
Set find = ws.Cells.find(What:="nieusprawiedliwiona", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not find Is Nothing Then find.Resize(12,1).EntireRow.Delete
Next ws
End Sub

Run Macro in all worksheets2

I have data in a sheet and I want to keep a specific section and delete the rest unwanted data based on a specific text search.specific text is dynamic.
So based on Activecell using offset I delete the portion above the specific text and want delete the below portion as well. (Say for example row 56-61 only the data I need, will not be same rows in all sheets)
the current code runs for one worksheet and stops in the second sheet
Sub Test999()
Dim ws As Worksheet, f As Range
For Each ws In Worksheets
Set f = ws.Cells.Find(What:="abc", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
ws.Range(f.Offset(-2, 0),
ws.Range("A2")).EntireRow.Delete
ws.Range(f, Selection.End(xlDown)).Select
Selection.End(xlDown).Select
ws.Range(ActiveCell.Offset(2, 0),
ws.Range("A500")).EntireRow.Delete
End If
Next ws
End Sub
I want to run this code to all worksheets
There was issue in the Range Selection.
Also be sure that ActiveCell corresponds to the correct cell you want to search from, because activecell can be different in each sheet.
Sub Test999()
Dim ws As Worksheet, f As Range
For Each ws In Worksheets
'Debug.Print ws.Name
Set f = ws.Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
ws.Activate
ws.Range(f.Offset(-2, 0), ws.Range("A2")).Select
ws.Range(f.Offset(-2, 0), ws.Range("A2")).EntireRow.Delete
f.Select
ws.Range(f, Selection.End(xlDown)).Select
Selection.End(xlDown).Select
ws.Range(ActiveCell.Offset(3, 0), ws.Range("A500")).EntireRow.Delete
End If
Next ws
End Sub

Ms Excel Replace value with the average of the previous and next values

I'm working with hourly weather data in Excel that has each hour of every day of the year along with the corresponding temperature value that was recorded.
Some of the values weren't recorded, and instead show up as just an "M" on the spreadsheet. For example, A32 = 28, A33 = M, A34 = 30. I want to replace that "M" with a formula to take the average of the previous and next values. I know how to do this manually, but I am having difficulty writing a Macro to find all the M's in the spreadsheet, then auto-replace it as stated above.
My main obstacle is getting excel to use the correct values when replacing the "M".
Here is my code
Sub MReplace()
'
' MReplace Macro
'
'
ActiveCell.Select
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(-8, 1).Range("A1").Select
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Replace What:="M", Replacement:="[****This is what I am having difficulty with****]", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
, SearchFormat:=False).Activate
End Sub
I have heard of something that you can put in to the code that can address the selected cell. I think it's cell() but I am not sure. Maybe that is a way to get it to work better?
Try this code:
Sub MReplace()
Dim ws As Worksheet
Dim cel As Range
Dim firstAddress As String
Set ws = ActiveSheet
Set cel = ws.Range("A:A").Find("M")
If Not cel Is Nothing Then
firstAddress = cel.Address
Do
cel.Value = (cel.Offset(1) + cel.Offset( -1)) / 2
Set cel = ws.Range("A:A").FindNext(cel)
hr = False
If Not cel Is Nothing Then
If cel.Address <> firstAddress Then
hr = True
End If
End If
Loop While hr
End If
End Sub
It loops through all the cells containing "M" and replaces it with the average of the one on the right and the one on the left. It will error on any that are in the first column as there is no column to the left.

VBA Looping through Worksheets fails when Cells.Find is performed as code

Sub Macro1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
ActiveCell.Value = "xyz"
Next ws
End Sub
I am using the following code through loop through all the worksheets in an Excel workbook. I want to look for "abc" in a worksheet. If I find "abc" on a particular worksheet I want the value of the cell below it to be set as "xyz".
Example if "abc" is found on Cell A2 of Worksheet1, I want Cell A3 to be "xyz". Similarly, If "abc" is found on cell B4 of Worksheet2, I want Cell B5 to be "xyz". This must be done for the entire workbook. However the looping through the entire workbook does not happen and the code just loops through worksheet1.
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
ActiveCell.Value = "xyz"
Next ws

Find method raise error 91 in excel Macro

I use a find method in the Excel macro for searching VLOOKUP in a cell, my goal is I need to know which formula that does not contain VLOOKUP, my method was running well, until in a cell there was
no VLOOKUP and macro kept debugging with the Run time error '91'
My question is how should I write the macro correctly so I wont get the debug until the activecell
contains *, below is my macro:
Sub findvlookup()
Do While Not ActiveCell.Value = "*"
If Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate Then
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub
Thanks for the help
You don't need a loop to find the first occurrence of your 'What:=" string
Dim fndVL as Range
fndVL = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not fndVL is Nothing Then
MsgBox "Found it"
Else
NsgBox "Not found"
End If
If you want to find subsequent instances then have a look at FindNext and possibly use this within a loop.
Sub findvlookup()
Dim rngFind As Range
Do While Not ActiveCell.Value = "*"
Set rngFind = Selection.Find(What:="VLOOKUP", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rngFind Is Nothing Then
ActiveCell.Offset(1, 0).Select
Else
Exit Sub
End If
Loop
End Sub
You are using Find method that technical details are Here.
You can find (in example) that:
Common Err.Numbers (9, 91) : the specified text wasn't in the target workbook.
And if you have a cell with an error value you will have another type of errors: like #Value

Resources