Error 91: with block and setting worksheet name

r3dal3rt

New Member
Joined
May 2, 2014
Messages
25
Hi forum, I am having a weird problem that I've been struggling for a couple of weeks now.

I have a list of employee names in Column A and a a For loop that sets the worksheet name ("wsName") to each cell value (employee names).
The macro works fine for the first 13 employees; on the 14th employee, I get Error 91. For whatever reason, on employee 14, it is not setting "wsName". The 14th employee name doesnt have any symbols or numbers.

Code:
Dim wsSummary, wsName As Worksheet
Dim sName As String

Set wsSummary = ThisWorkbook.Worksheets("Summary")

With wsSummary[INDENT]For i = 1 to 25[/INDENT]
[INDENT]        sName = .Range("A" & i).Value[/INDENT]
[INDENT=2]Set wsName = ThisWorkbook.Worksheets(sName)

With wsName[/INDENT]
[INDENT=3]'Some copy and paste stuff
End With[/INDENT]
[INDENT]
Next i[/INDENT]
End With
End Sub

Thanks in advance for your help!
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Check that the sheet name & Employee name are the same & check that there are no leading/trailing spaces.
Also what is the length of that employee's name.
 
Upvote 0
Check that the sheet name & Employee name are the same & check that there are no leading/trailing spaces.
Also what is the length of that employee's name.

Hey Fluff, I also have a macro before this macro that checks for sheets. If the sheet doesn't exist (i.e. new employee), it would create the sheet. The employee name is 12 characters. I used LEN() to check there are no leading/trailing spaces.

Thanks!
 
Upvote 0
If you try this what does the msgbox say?
Code:
Sub Chk()
MsgBox "|" & Sheets("Summary").Range("A14").Value & "|"
End Sub
 
Last edited:
Upvote 0
Just another thought. I deleted the 14th employee's name (so the 15th employee is now 14th), and I get the same error. then I deleted a few more names...
It seems like this error with not setting wsName is happening on the 14th employee regardless of the string.
 
Upvote 0
If you try this what does the msgbox say?
Code:
Sub Chk()
MsgBox "|" & Sheets("Summary").Range("A14").Value & "|"
End Sub

Message box shows |Jack Smith|

Just another thought. I deleted the 14th employee's name (so the 15th employee is now 14th), and I get the same error. then I deleted a few more names...
It seems like this error with not setting wsName is happening on the 14th employee regardless of the string.
 
Last edited:
Upvote 0
Does the data natively come from excel? or is it extracted from another source? Potentially could be something like a no breaking space in the name rather than an ordinary space.
 
Upvote 0
Does the data natively come from excel? or is it extracted from another source? Potentially could be something like a no breaking space in the name rather than an ordinary space.

Data is native (i.e. names are manually entered into the spreadsheet).
 
Upvote 0
You shouldn't get an error 91 if the sheet name were incorrect - you'd get a subscript out of range error (error 9).

Which line actually causes the error, and what is the code in the loop?
 
Upvote 0
Forgive a stupid question, but bearing in mind this comment
Just another thought. I deleted the 14th employee's name (so the 15th employee is now 14th), and I get the same error. then I deleted a few more names...
It seems like this error with not setting wsName is happening on the 14th employee regardless of the string.

Do you have more than 13 sheets + the "Summary" sheet in that workbook?
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

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