Using Adodb By number

Shenzar

New Member
Joined
Jan 8, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Mobile
  2. Web
Hello,

I will have a question, my code works fine by name of sheets, but I changed it to specify sheet number of target workbook, but it doesn't work

Is it possible to copy sheets by number and not by name

For example instead of saying leaves ("function")
say sheets (1)

Because the name of the leaves may vary but the number will always be the same

Do you have any ideas?

VBA Code:
Sub Datas()

    Dim CN As ADODB.Connection, Fichier As String, Req As String, Req2 As String
    Dim F1 As ADODB.Recordset, F2 As ADODB.Recordset
    Dim FeuilMor As Integer, FeuilDiner As Integer, chemin As String
    Dim ShMor As Worksheet, ShDiner As Worksheet
    
    'Number Sheets 1 and 3 - Target File
    FeuilMor = 1
    FeuilDiner = 3
    
   'Set Sheets 1 ans 3 - Target File
    Set ShMor = Worksheets(FeuilMor)
    Set ShDiner = Worksheets(FeuilDiner)
    
    Set F1 = New ADODB.Recordset
    Set F2 = New ADODB.Recordset

    Set CN = New ADODB.Connection
    
    chemin = ThisWorkbook.Path & "\"
    Fichier = chemin & Dir(chemin & "effectifs*.xls")
    
    With CN
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=NO"""
        .Open
    End With

    'SQL Request - Target File
    Req = "SELECT * FROM [" & ShMor & "$]"
    Req2 = "SELECT * FROM [" & ShDiner & "$]"

    Set F1 = CN.Execute(Req)
    Set F2 = CN.Execute(Req2)
    
    'Paste the response of the request
    Feuil2.Range("A1").CopyFromRecordset F1
    Feuil3.Range("A1").CopyFromRecordset F2

    CN.Close

    Set CN = Nothing

'    Kill ThisWorkbook.Path & "\effectifs*.xls"

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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