Results 1 to 6 of 6
  1. #1
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36

    Access to Excel Using the Worksheet's Codename

    I wanted to put whatever data in a specific excel worksheet that I wanted to reference by code name (not by index or it's sheet name on the tab). So I wrote the following code which I swear worked for a couple test runs but now it won't even show the code name of the worksheet anymore in the debug.print screen. Am I just losing my mind or what?



    Option Compare Database
    Option Explicit


    Code:
    Sub test()
    
    
    Dim xl As Object
    Dim wkb As Object
    Dim wks As Object
    Dim strPath As String
    Dim strWksCode As String
    
    
    strPath = "C:\Users\Me\Desktop\test.xlsx"
    
    
    Set xl = CreateObject("Excel.Application")
    
    
    xl.Visible = True
    xl.UserControl = True
    
    
    Set wkb = xl.Workbooks.Open(strPath)
    
    
    For Each wks In wkb.Worksheets          'This is the only way I found to refer to a worksheet by its code name...code name doesn't work anymore.
        Debug.Print wks.CodeName
        If wks.CodeName = "Sheet2" Then
            Set wks = xl.Worksheets(wks.Name)
            wks.Select
            Exit For
        End If
    Next wks
    
    
    wks.Range("A2") = "test"
    wkb.Save
    
    
    Set wks = Nothing
    'objWbk.Close
    Set wkb = Nothing
    'objExc.Quit
    Set xl = Nothing
    
    
    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Found a thread that might help:
    https://stackoverflow.com/questions/...using-codename
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,903
    So have you walked through the code line by line?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36
    Quote Originally Posted by Welshgasman View Post
    So have you walked through the code line by line?
    I have walked through the code. When I use the .Name everything works just fine. The block of code looks like this:

    Code:
    For Each wks In wkb.Worksheets
        Debug.Print wks.Name
        If wks.Name = "Sheet2" Then
            Set wks = xl.Worksheets(wks.Name)
            wks.Select
            Exit For
        End If
    Next wks

    When I change out the .name with .codename property nothing appears in the immediate window when using debug.print...I'm guessing since nothing is assigned to the property, it errors out once hitting the wks.Range("A2")... since nothing is assigned as the worksheet. The block of code looks like this:

    Code:
    For Each wks In wkb.Worksheets         
        Debug.Print wks.CodeName
        If wks.CodeName = "Sheet2" Then
            Set wks = xl.Worksheets(wks.Name)
            wks.Select
            Exit For
        End If
    Next wks
    I decided to use the .codename property after reading similar threads like Gicu recommended. What I don't understand is I swear I had the .codename property working in my code before and I was verifying it under the debug.print property but now nothing registers anymore. I'm not sure if anyone has had similar problems or has had experience troubleshooting something like this?
    Last edited by neuk; 06-01-2021 at 09:53 AM.

  5. #5
    neuk is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    36
    Ok guys. I got it to work. What I did is that I created a new excel application and manually changed the code name to something I wanted instead of the default Sheet1, Sheet2, Sheet3 etc. - it looks like I couldn't read the codename without first changing it from the default codename. Not sure if that's the exact reason, or why...but that's what I found in my testing.

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Thanks for sharing!
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing a specific Excel worksheet into Access
    By jeffhanner10@gmail.com in forum Import/Export Data
    Replies: 2
    Last Post: 09-18-2019, 07:07 AM
  2. Import an Excel worksheet into Access?
    By Dave Lambert in forum Import/Export Data
    Replies: 1
    Last Post: 09-18-2018, 01:52 PM
  3. Refer to excel sheet's codename from access
    By SteveApa in forum Programming
    Replies: 8
    Last Post: 04-06-2018, 02:21 PM
  4. Update Access database from excel worksheet.
    By peres.br in forum Import/Export Data
    Replies: 3
    Last Post: 12-12-2016, 10:09 AM
  5. Replies: 7
    Last Post: 09-24-2013, 04:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums