Solved

VBA code issue refernce to worksheet

Posted on 2014-04-02
8
176 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 49

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

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 250 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 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now