Solved

VBA code issue refernce to worksheet

Posted on 2014-04-02
8
181 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 51

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
Technology Partners: 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 51

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 51

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

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

734 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