Shenzar
New Member
- Joined
- Jan 8, 2021
- Messages
- 5
- Office Version
- 2019
- Platform
- Mobile
- 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?
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