Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA code issue refernce to worksheet

Posted on 2014-04-02
8
Medium Priority
?
197 Views
Last Modified: 2014-04-02
Hi all,

I am getting a (subscript out of range) error with the following code and despite my best efforts, cannot seem to work out what is causing it.

I am wanting to ensure I reference the actual Sheet Number of the workbook so that if someone changes the sheetname, the code will not bug out.

Dim wkb As Workbook
    Dim wks1 As Worksheet
    Dim wks2 As Worksheet
    Dim rng As Range
    
    Set wkb = Application.ThisWorkbook
          
    'turn off updates to speed up code execution ->
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
    End With
           
    Set wks1 = wkb.Sheets("Sheet9") 
    Set wks2 = wkb.Sheets(Sheet17) 

Open in new window


Any ideas?

TA
0
Comment
Question by:discogs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 39971617
HI
with ""
Set wks2 = wkb.Sheets("Sheet17")  

Open in new window

Regards
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39971619
Set wks2 = wkb.Sheets("Sheet17")
0
 

Author Comment

by:discogs
ID: 39971628
Errors on both guys. Subscript out of range...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39971637
Do you have a sheet whose name is displayed as Sheet17?

Is this the line which is giving an error?
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 39971640
Hi,

pls try fuirst select the wbk
 
Set wks2 = Sheet17

Open in new window

Regards
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1000 total points
ID: 39971656
yes you can  reference the actual Sheet Number of the workbook  like below ,
Try giving your numbers , I tried like below and working fine

 Set wks1 = wkb.Sheets(1)
    MsgBox (wks1.Name)
    Set wks2 = wkb.Sheets(2)
    MsgBox (wks2.Name)
0
 
LVL 52

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 1000 total points
ID: 39971671
Hi,

Or you could use a function to find the name of the sheet

Function fNameOfSheet(Wb As Workbook, CodeName As String) As String
    fNameOfSheet = Wb.VBProject.VBComponents(CodeName).Properties("Name").Value
End Function

Open in new window


Dim wkb As Workbook ' do not forget to declare
Set wks2 = wkb.Sheets(fNameOfSheet(wkb, "Sheet17"))

Open in new window

Regards
0
 

Author Closing Comment

by:discogs
ID: 39971681
Great stuff guys. It works. Both solutions split 50/50 as they both worked for me. Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question