PDA

View Full Version : Else If - Do Nothing



ChrisAch
07-25-2016, 03:11 AM
Morning all.

I have the below code which finds an error code in a pivot sheet, then shows data, and renames that tab inline with the code.

However ... these codes change daily.

Im trying to understand where I would put else if, in order to skip if this code is not found and move onto the next code...

Or a smarter way of writing the below example code.




Sheets("Chris Error Code List").Select



Cells.Find(What:="009", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate


ActiveCell.Offset(columnOffset:=1).Activate Selection.ShowDetail = True
Sheets(4).Name = "009"


Thank you

p45cal
07-25-2016, 04:12 AM
along the lines of:?
Dim rng As Range
Sheets("Chris Error Code List").Select
Set rng = Cells.Find(What:="009", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
rng.Offset(columnOffset:=1).Activate
Selection.ShowDetail = True
ActiveSheet.Name = "009"
End If
Lots of error codes? Perhaps (untested):

Dim rng As Range
ErrorCodeList = Array("009", "008", "007", "666", "111")
For Each ErrorCode In ErrorCodeList
Set rng = Sheets("Chris Error Code List").Cells.Find(What:=ErrorCode, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
rng.Offset(columnOffset:=1).ShowDetail = True
ActiveSheet.Name = ErrorCode
End If
Next 'ErrorCode

ChrisAch
07-26-2016, 09:05 AM
That works perfectly.

Thank you :-)