Results 1 to 9 of 9
  1. #1
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Thumbs down How to call a changing Name?

    I am trying to get the name of the sheet to copy a piece of data from one sheet to another. Sort of like using 3 tables to copy a cell into another table. It fails on the bold red code with Run-time error 1004; Application defined or object defined error.



    I have tried Worksheets(WkSht.Name).Cells......, Sheets(.Name).Cells......, WkSht.Name.Cells......, Worksheets(sSheet).cells where sSheet=WkSht.name, Activesheet.cells...... and other nuances I could think of.
    If there was a single worksheet then you would use: Worksheets("SheetName").Cells(x, 2) = d2(y, 2) easy peasy.


    Code:
    Sub HBRolestoSheets()
    Dim WkBk As Workbook,  WkSht As Worksheet, WkSht2 As Worksheet, WkSht3 As Worksheet
    Dim Rng As Range, Rng2 As Range, Rng3 As Range, d1 As Variant, d2 As Variant, d3 As Variant, x As Long, y As Long, z As Long
    
    
    
        strTab = "PC22V2"
        
    Set WkBk = ThisWorkbook
        Set WkSht = Worksheets(strTab)
        Set WkSht2 = Worksheets("Baseline")
        Set Rng = WkSht.UsedRange
        Set Rng2 = WkSht2.UsedRange
        
        d1 = Rng.Value
        d2 = Rng2.Value
        x = 1
        y = 1
        
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    
    
        For Each WkSht In Worksheets
            With WkSht
                Select Case WkSht.Name
                    Case Is = "Baseline", "System Count", "", strTab
                    Case Else
    
    
                        For y = 2 To UBound(d2)
                        z = 1
                            For x = 2 To UBound(d1)
                                If d1(x, 2) = d2(y, 1) Then
                                    z = d2(y, 6)
                                ElseIf d2(y, 6) = "" Then
                                    z = 6
                                End If
                                    ActiveSheet.Cells(z, 3) = d1(x, 14)
                        Next x
                    Next y
                End Select
            End With
        Next WkSht
    
    
    
        ActiveSheet.Range("C1").EntireColumn.AutoFit
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
    
    
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Put a break point on the code and step through if the used range is not too large. My guess is that at some point, z becomes zero, which would not work too well for Cells(z, 3).
    Note that UsedRange includes formatting, so if cell font/border etc. is formatted, it becomes part of the range whether it contains data or not. That might be part of your problem.
    If the range is large, you might want to debug.print in the loop whatever makes sense to you, such as range addresses or array values. When it errors and stops, you might be able to pinpoint the range/value/whatever that points to the cause.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Thanks Micron,
    Below fixed the issue. I was using debug.print previously. it was a count and an empty cell issue. I ensured the cells were filled.

    Code:
    Sub AddRoleToWkSht()
    
        Set WkSht = Worksheets(strTab)
        Set WkSht2 = Worksheets("Baseline")
        Set Rng = WkSht.UsedRange
        Set Rng2 = WkSht2.UsedRange
    
    
        d1 = Rng.Value
        d2 = Rng2.Value
        IPCnt = 1
        y = 1
        x = 1
        
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    
    
         For Each WkSht In Worksheets
            With WkSht
                Select Case WkSht.Name
                    Case "Baseline", "Acronyms", "UniqueRefer1", "UniqueRefer", "System Count", strTab, ""
                    Case Else
                        For x = 2 To UBound(d1)
                            For y = 2 To UBound(d2)
                                If d1(x, 13) <> d2(y, 1) Or d1(x, 13) <> WkSht.Name Then GoTo Skip
                                    If d1(x, 13) = d2(y, 1) And d1(x, 13) = WkSht.Name Then IPCnt = IPCnt + 1
                                        Worksheets(WkSht.Name).Cells(d2(y, 6) + 3 + IPCnt, 3) = d1(x, 14)
    Skip:
                            Next y
                        Next x
                        IPCnt = 1
                        Sheets(WkSht.Name).Range("C1").EntireColumn.AutoFit
                End Select
            End With
        Next WkSht
    
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayStatusBar = True
      
    End Sub

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I get the idea of Select Case rather than wordy If statements. Another use of Select Case could be
    Case Is <> "Baseline", "Acronyms", "UniqueRefer1", "UniqueRefer", "System Count", strTab, ""

    then you don't need the Case Else. You don't think that calculating the last row with data would be better than just filling the used range with data?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Remember I am just a self-taught newbie with a lot of assistance from here and internet combing. I like the case <> and will implement. Thanks
    The UsedRange vs Cells(Rows.Count, 1).End(xlUp)? I understand the xlUP is more accurate than used range. Once I get the basic of my self induced project i will tweak it and probably change it.

    Thanks again

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You're welcome.
    I think 99.99% of suggestions I post are meant to be strictly informative/educational and never meant to be criticism.
    I am just a self-taught newbie with a lot of assistance from here and internet combing.
    Ditto. I took no vba courses. When I started I didn't know how to raise a message box, so I never mock anyone else's efforts. If I see an opportunity to enhance someone else's code writing, I usually take it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Did you try using the CodeName?
    Code:
    Sub getAllCodeNames()
        Dim sh As Worksheet
        For Each sh In Worksheets
            Debug.Print sh.CodeName
        Next sh
    End Sub
    A worksheet's name is a caption. CodeName is the real name of the sheet and it's handy, for example, ActiveSheet does not give you intellisense until you declare it as a worksheet, but in VBA you can refer to it by just typing its CodeName.

    Code:
    Sub codeNameStuff()
        MsgBox "codename is " & Hoja1.CodeName & ", name is " & Hoja1.Name
    End Sub

  8. #8
    Thompyt is offline Expert
    Windows 10 Access 2016
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Edgar,
    Nice info about the CodeName. I tested it and it gives the sheet## name you can see in the explorer window.

    Micron,
    I was not taking your comments as criticism. If any it was constructive. Appreciate this sites knowledge.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    CodeName is certainly better than sheet index if they can move around, or sheet name if they can change names, but I don't see much benefit in CodeName wrt your post. It's good to know about the option though. Don't overlook the fact that Sheets is not the same as Worksheets.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Call Log DB
    By Tradesman in forum Database Design
    Replies: 77
    Last Post: 08-29-2019, 05:54 AM
  2. Need help not sure what to call it :-(
    By jscalem in forum Database Design
    Replies: 8
    Last Post: 08-30-2017, 12:45 PM
  3. What do you call this?
    By mrmims in forum Access
    Replies: 4
    Last Post: 11-11-2015, 12:43 PM
  4. Call and phone
    By drunkenneo in forum Programming
    Replies: 0
    Last Post: 08-28-2013, 12:49 AM
  5. Not even sure what to call this one
    By eizquierdo in forum Queries
    Replies: 2
    Last Post: 03-14-2013, 12:08 PM

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