Run-time error '3265' - Item cannot be found in the collection corresponding to the requested name or ordinal.

Cuervo

New Member
Joined
Sep 19, 2016
Messages
1
Hi All,

I found a thread on this forum with the exact error I was getting and the person who started the thread in the next post just states "I got it." and never actually states what he got. So, I'm starting a new thread with the same issue. However I have some interesting twists, but first let's get the code up here.

Code:
Dim cnn As New ADODB.Connection
Dim rsXLS As New ADODB.Recordset

    Set TheWB = Workbooks("Cat_Data.xlsm")
    Set TheSheet = TheWB.Worksheets(1)

    xlsFile = "TheCatalog.xls"
    xlsPath = "C:\Catalog Files\"
    
    myConnString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & xlsPath & xlsFile & ";"
    myConnString = myConnString & "Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=YES;IMEX=1" & Chr(34) & ";"
    
    If cnn.State = adStateOpen Then
        cnn.Close
    End If
    cnn.ConnectionString = myConnString
    cnn.Open
    
    mySQL = "SELECT * FROM [Page 1$V:W]"
    RNG = "AF5"
    Set rsXLS = cnn.Execute(MySQL)
    TheSheet.Range(RNG).CopyFromRecordset rsXLS
    rsXLS.Close

    mySQL = "SELECT * from [Page 1$]"
    RNG = "AM"
    Set rsXLS = cnn.Execute(MySQL)
    rsXLS.MoveFirst
    While Not rsXLS.EOF
        amPrint = amPrint + 1
        rsValue = rsXLS(23)
        TheSheet.Range(RNG & amPrint).Value = rsValue
        rsXLS.MoveNext
     Wend
           
'Clean Up   
    If CBool(cnn.State And adStateOpen) Then cnn.Close
    Set connstring = Nothing
    Set rsXLS = Nothing

So, it seems simple, though I've never opened and Excel sheet for use as a DB before. All I want to do is open the "Catalog.xls" file read a couple of columns an put them in other columns in the "Cat_Data.xlsm" sheet. The first request works without issue. I can grab the V and W columns and paste the recordset to the rows I need them. The second request has been driving me crazy. It is the last column (column 'X') on the "Catalog.xls" sheet and Excel doesn't believe it exists.

If I run the code as is I get the error in the title 'Run-time error '3265' - Item cannot be found in the collection corresponding to the requested name or ordinal.' However, if I don't touch the code and I open the "Catalog.xls" sheet, it works. When I close the sheet, it stops working again. If I don't touch the code and add anything to column 'Y' on the "Catalog.xls" and close the sheet it works, but for some reason it doesn't work with the closed, unedited "Catalog.xls".

The SELECT * FROM [Page 1$] was because I couldn't get the VBA to give me just 'Column X' so for troubleshooting I grabbed the whole table and simply tried to get column X from the recordset by column number, but it doesn't work except in the situations I outlined above.

Any insight anyone can provide would be greatly appreciated. I'm sure it is something simple I'm missing but I just don't see it.

Thanks for your help.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top