Consulting

Results 1 to 3 of 3

Thread: variable = worksheet (=>expected identifier)

  1. #1
    VBAX Newbie
    Joined
    Jul 2021
    Posts
    1
    Location

    Question variable = worksheet (=>expected identifier)

    First of all, sorry for my bad english


    So I have a macro that sets the worksheets of my workbook as variables names to prevent having debugging alert when an user uses the workbook and changes the name or the position of a sheet.


    For example, if my sheet's name is "BDD" when I code I will be refering to this sheet as: Sheets("'BDD").xxxxxx or sheets(1).xxxxxxx then if a user changes the name of the sheet my macros will not work anymore


    That's why in worbook_open I set:


    WB00 = activeworkbook
    WB00WS01 = first sheet
    WB00WS02 = second sheet


    on and on


    Therefore, when i code i refer to it as WB00WS01.xxxx


    So, my macro is:




        Sub initWB00()
            Set WB00 = ActiveWorkbook
            For Each W In WB00.Worksheets
                On Error Resume Next
                If W.CodeName = "Sheet1" Then
                    Set WB00WS01 = W
                End If
                If W.CodeName = "Feuil2" Then
                    Set WB00WS02 = W
                End If
                If W.CodeName = "Feuil3" Then
                    Set WB00WS03 = W
                End If
                If W.CodeName = "Feuil4" Then
                    Set WB00WS04 = W
                End If
                If W.CodeName = "Feuil5" Then
                    Set WB00WS05 = W
                End If
                If W.CodeName = "Feuil6" Then
                    Set WB00WS06 = W
                End If
                If W.CodeName = "Feuil7" Then
                    Set WB00WS07 = W
                End If
                If W.CodeName = "Feuil8" Then
                    Set WB00WS08 = W
                End If
                If W.CodeName = "Feuil9" Then
                    Set WB00WS09 = W
                End If
                If W.CodeName = "Feuil10" Then
                    Set WB00WS10 = W
                End If
            Next W
        End Sub
    It works as it is, but as you can see, it's pretty limited (if I want to go up to WB00WS100 for example) and it's very trivial


    Then, I had the idea to loop a variable "i" that will give the number of the sheet i.e "WB00WS" & i = "sheet" & i




    Sub initWB00()
    Dim i As Integer
        Set WB00 = ActiveWorkbook
        For Each W In WB00.Worksheets
            For i = 1 To 20
            If W.CodeName = "Feuil" & i Then
                Set "WB00WS0"&i = W
            End If
            Next i
        Next W
    End Sub
    If I do it like that I have the expected identifier error


    I assume it's because I've put a string and a variable to the left of an equality but otherwise I can't imagine how can I write a loop that makes what I need to


    Do you have any idea ?


    Thanks in advance !!

  2. #2
    can you use Collection?
    you can add Item to collection:

    Dim col As New Collecton

    Sub initWB00()
    Dim i As Integer
    Set WB00 = ActiveWorkbook
    For Each W In WB00.Worksheets
    For i = 1 To 20
    If W.CodeName = "Feuil" & i Then
    col.Add W, "WB00WS0" & i
    End If
    Next i
    Next W
    End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    For example, if my sheet's name is "BDD" when I code I will be referring to this sheet as: Sheets("'BDD").xxxxxx or sheets(1).xxxxxxx then if a user changes the name of the sheet my macros will not work anymore

    Why not just stay with the Codename for the worksheets in your macros. That way if doesn't matter what the user does (unless they delete a sheet)


    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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