+ Reply to Thread
Results 1 to 3 of 3

reference a sheet with variable?

Hybrid View

pgag45 reference a sheet with... 05-15-2007, 02:28 PM
mikerickson I'm not sure what you are... 05-15-2007, 03:47 PM
cbanks in case its not sheet 1 05-15-2007, 04:08 PM
  1. #1
    Registered User
    Join Date
    05-15-2007
    Posts
    4

    reference a sheet with variable?

    Hi there,

    I was wondering if there was any way to reference a sheet with a variable name instead of needing to know the exact name of the sheet?

    I am trying to open an individual sheet into my workbook with the code:

    Dim sFileName As String
    'Show the open dialog and pass the selected _
    file name To the String variable "sFileName"
    sFileName = Application.GetOpenFilename
    'They have cancelled.
    If sFileName = "False" Then Exit Sub
         
    Workbooks.Open fileName:=sFileName
    Sheets("EVENTS").Copy After:=Workbooks("NLeapGIS10.xls").Sheets(ThisWorkbook.Sheets.Count)
    Workbooks("EVENTS.xls").Close
    That works fine, if the sheet the person is trying to open is called "EVENTS". However I would like to implement something like

    Sheets(sFileName)

    to open the name of the sheet the same as the file name.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I'm not sure what you are trying to do. Does it help to know that the last three lines do the same thing?

    Dim sheetName As String
    sheetName = "sheet1"
    
    Dim mySheet As Worksheet
    Set mySheet = ThisWorkbook.Sheets("sheet1")
    
    ThisWorkbook.Sheets("sheet1").Activate
    ThisWorkbook.Sheets(sheetName).Activate
    mySheet.Activate

  3. #3
    Registered User
    Join Date
    05-04-2006
    Posts
    24

    in case its not sheet 1

    if your sheet is labeled the same name as your workbook then this will work..

    Dim sheetname As String
    Dim i As String
    Dim j As String
    Dim k As String
    Dim sFileName As String
    
    j = 1
    i = ""
    
    'Show the open dialog and pass the selected _
    file name To the String variable "sFileName"
    sFileName = Application.GetOpenFilename
    'They have cancelled.
    If sFileName = "False" Then Exit Sub
    Workbooks.Open Filename:=sFileName
    
    
    Do Until k = "\"
    i = Right(sFileName, j)
    k = Left(i, 1)
    DoEvents
    j = j + 1
    Loop
    
    j = j - 1
    sheetname = Right(i, j)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1