Search if Sheet already exist in activeworkbook Overflow error

Nikhil Damle

New Member
Joined
Jul 20, 2011
Messages
2
I have written macro which loops through all rows in workbook and search if sheet already exist in another workbook. Here is the exact code

orderno = ThisWorkbook.Sheets("Sheet1").Range("V" & activerow).Value
Workbooks.Open (workdir & "Material Account\" & VendorName & ".xls")
Dim sh As Worksheet, iExist As Boolean
iExist = True
Set sh = Nothing
Set sh = Worksheets(orderno) 'Here is where err occur
If Err Then iExist = False
If iExist = True Then 'Sheet Exist

Else 'Sheet Does Not Exist

End if
ActiveWorkbook.Save
ActiveWorkbook.Close

This code generates overflow error(Run time error 6) and adding On Error Resume Next Does not search sheet. How to rectify this or is there any other way to search sheet in another workbook.
Thanks in Advance
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
thanks VoG and taurean for reply
Here activerow = ActiveCell.Row
taurean i checked the link and it worked for me:)
But if sheetname is not constant then it should be declared before using like

Dim mysheetname as string
activerow = ActiveCell.Row
orderno = ThisWorkbook.Sheets("Sheet1").Range("V" & activerow).Value
mysheetname = orderno
If Not SheetExists(MySheetName) Then
MsgBox(MySheetName & " doesn't exist!")
Else
Sheets(MySheetName).Activate
End If

but when mysheetname is directly used(see below code) compile error ByRef argument type mismatch occur.

activerow = ActiveCell.Row
orderno = ThisWorkbook.Sheets("Sheet1").Range("V" & activerow).Value
mysheetname = orderno
If Not SheetExists(MySheetName) Then
MsgBox(MySheetName & " doesn't exist!")
Else
Sheets(MySheetName).Activate
End If
 
Upvote 0
I think, you need to be doubly sure that you have selected correct Sheet i.e. Sheet1 while running the code. See if the below code with added checks works for you:
Code:
Public Sub CheckSheet()
Dim MySheetName As String
Dim lActiveRow As Long
'Checking if we are on the right sheet i.e. Sheet1
If ActiveSheet.Name <> "Sheet1" Then MsgBox "Not on the correct sheet!": Exit Sub
lActiveRow = ActiveCell.Row
MySheetName = ThisWorkbook.Sheets("Sheet1").Range("V" & lActiveRow).Value
'Checking if at least non-blank info is provided on the active row.
If MySheetName = "" Then MsgBox "There's no Sheet Name Specified in Column V!": Exit Sub
If Not SheetExists(MySheetName) Then
MsgBox (MySheetName & " doesn't exist!")
Else
Sheets(MySheetName).Activate
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top