Basic VBA code generating Run-time error '91': Object variable or With block variable not set

Anonymous
2024-03-06T23:13:43+00:00

I'm having some issues with a code generating a 'Run-time error '91': Object variable or With block variable not set' message.

I created a new sub to simplify and test only the offending lines, and it's still generating the error. I'm really struggling to figure out what the issue is here.

I've used this same line of code (with different range and find criteria) elsewhere and it has worked fine.

Sub TESTING()

Dim ws1 As Worksheet, ws2 As Worksheet 

Set ws1 = ThisWorkbook.Worksheets(Sheet2.Name) 'Cost Tracker tab 

Set ws2 = ThisWorkbook.Worksheets(Sheet8.Name) 'Lookups tab 

ws2.Activate 

Dim Test1 As Long 

Test1 = ws2.Range("S1:Z1").Find("7DC07J").Column   << ERROR HERE

End Sub

Thanks

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2024-03-07T06:50:26+00:00

    Test1 = ws2.Range("S1:Z1").Find(What:="ABC123", LookIn:=xlValues).Column-18

    1 person found this answer helpful.
    0 comments No comments
Answer accepted by question author
  1. Anonymous
    2024-03-07T06:18:37+00:00

    You may try this one.

    =====

    Sub TESTING1()

    Dim ws2 As Worksheet, ws3 As Worksheet 
    
    Set ws2 = ThisWorkbook.Worksheets(Sheet8.Name) 'Lookups tab 
    
    ws2.Activate 
    

    For i = 19 To 26

    If Cells(1, i) = "ABC123" Then

    test = i

    End If

    Next i

    End Sub

    =====

    OR

    =========================

    Sub TESTING1()

    Dim ws2 As Worksheet, ws3 As Worksheet 
    
    Set ws2 = ThisWorkbook.Worksheets(Sheet8.Name) 'Lookups tab 
    
    ws2.Activate 
    
    Dim Test1 As Long 
    
    Test1 = ws2.Range("S1:Z1").Find(What:="ABC123", LookIn:=xlValues).Column 
    

    End Sub

    =======================

    1 person found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-03-07T02:06:26+00:00

    Your code works at my side. Could you share us a test workbook to debug your code?

    For sharing a sample workbook, upload to OneDrive or any other cloud drives. Then post a link here.

    Why a sample file is important for troubleshooting. How to do it. - Microsoft Community

    0 comments No comments
  2. Anonymous
    2024-03-07T04:13:25+00:00

    7DC07J

    Not found?

    0 comments No comments
  3. Anonymous
    2024-03-07T05:23:36+00:00

    Hi,

    I created a sample file but I ran into troubles trying to share it. I'm not sure my work allows sharing externally. But in the process I made a change, which resulted in the code working, but I'm still not sure why it wasn't working before. And it's not a fix for me as I need the file set up in a certain way.

    To elaborate:

    The VBA code that refers to S1:Z1 in the Lookups tab is referencing a named range (CodesInUse) that's spilled horizontally as headings. When I pasted over that formula as values, the macro worked. So, for some reason, the VBA code doesn't like that I'm referencing an array (or maybe that I'm referencing a named range?).

    The named range is in another tab, which isn't defined in the macro.

    Image

    Any idea why this would be the case. I'm pretty new to VBA, and figuring it out as I go along, so I'm pretty clueless on this one.

    Thanks

    0 comments No comments