Solved

VBA code issue refernce to worksheet

Posted on 2014-04-02
8
175 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 48

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 48

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 48

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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…

705 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

21 Experts available now in Live!

Get 1:1 Help Now