vba_monkey
Board Regular
- Joined
- Dec 18, 2013
- Messages
- 112
Hello,
I am trying to write a macro that enters a Vlookup into a cell only if a corresponding tab exists on another workbbok to compare it to. I'm doing this to try to avoid the 'Select Sheet' Dialogue from appearing if there is no corresponding sheet on the other workbook.
This is what I have so far (the Function comes from here Excel VBA If WorkSheet("wsName") Exists - Stack Overflow
The code works in that it doesnt enter a Vlookup if the sheet doesnt exists but it also doesnt recognise if the sheet does exist so I think the issue is with setting the SheetToFind.
Any ideas please?
Also posted here VBA - Enter Formula only if Sheet Exists
I am trying to write a macro that enters a Vlookup into a cell only if a corresponding tab exists on another workbbok to compare it to. I'm doing this to try to avoid the 'Select Sheet' Dialogue from appearing if there is no corresponding sheet on the other workbook.
This is what I have so far (the Function comes from here Excel VBA If WorkSheet("wsName") Exists - Stack Overflow
Code:
Sub MySub()
Dim PreviousReport As String
If SheetExists(PreviousReport & ActiveSheet.Name) Then
LastRow = .Cells(Rows.Count, 3).End(xlUp).Row
ActiveSheet.Range("I7:I" & LastRow).Value = "=IFERROR(IF(VLOOKUP($B7,'[" & PreviousReport & "]" _
& ActiveSheet.Name & "'!$B:$J,8,0)="""","""",VLOOKUP($B7,'[" & PreviousReport & "]" & ActiveSheet.Name & "'!$B:$I,8,0)),"""")"
Else
End If
End Sub
Function SheetExists(SheetToFind As String) As Boolean
Dim Sheet As Object
SheetExists = False
For Each Sheet In Worksheets
If SheetToFind = Sheet.Name Then
SheetExists = True
Exit Function
End If
Next Sheet
End Function
The code works in that it doesnt enter a Vlookup if the sheet doesnt exists but it also doesnt recognise if the sheet does exist so I think the issue is with setting the SheetToFind.
Any ideas please?
Also posted here VBA - Enter Formula only if Sheet Exists