set object doesn't work

Flavien

Board Regular
Joined
Jan 8, 2023
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hello!

Could someone explain to me why the 1st syntax of the code works, but the 2nd doesn't ?
Just to clarify, I thought to remove the apostrophe.

This syntax works:
VBA Code:
Workbooks(chemin).Worksheets("Modification").Range("A4:BH4").Copy: Workbooks(chemin2).Worksheets("Cartouche").Range("A3:BH3").PasteSpecial Paste:=xlPasteValues

This one doesn't :
VBA Code:
CS.OS.Range("A4:BH4").Copy: CD.OD.Range("A3:BH3").PasteSpecial Paste:=xlPasteValues

VBA Code:
Sub Collecte()

Dim CA As String 'déclare la variable CA (Chemin d'Accès)
Dim CD As Workbook 'déclare la variable CD (Classeur Destination)
Dim OD As Worksheet 'déclare la variable OD (Onglet Destination)
Dim FS As String 'décalre la variable FS (Fichier Source)
Dim CS As Workbook 'déclare la variable CS (Classeur Source)
Dim OS As Worksheet 'déclare la variable OS (Onglet Source)
Dim Ext, Ext2 As Variant


'   Récupérer le nom du classeur actif
    chemin = ThisWorkbook.Name


'Désactive la mise à jour de l'écran pour accélérer l'exécution
    Application.ScreenUpdating = False

'chemin d'accès au dossier de stockage des classeurs de FNC qu'on souhaite mettre à jour
    CA = "ESSAIS" & "\"

'   FNC Recherchée
    Ext = ThisWorkbook.Worksheets("Accueil").Range("Concat_Num_FNC").Value


'   ouvrir le classeur selon lien écrit dans Tableau FNC
    VLKUP1 = WorksheetFunction.VLookup(Ext, Worksheets("Tableau FNC").Range("Tableau_FNC"), 59, False) 'lien classeur FNC pour pièce jointe
    Workbooks.Open Filename:=VLKUP1

'   Récupérer le nom du classeur que l'on souhaite ouvrir
    chemin2 = ActiveWorkbook.Name

    Set CD = Workbooks(chemin2) 'définit la classeur destination CD
    Set OD = CD.Sheets("Cartouche") 'définit l'onglet destination OD

    Set CS = Workbooks(chemin) 'définit la classeur source CS
    Set OS = CS.Worksheets("Modification") 'définit l'onglet source OS (à adapter à ton cas, ici j'ai mis le premier onglet)


'   Copier les données modifiées vers le classeur FNC à modifier
'    CS.OS.Range("A4:BH4").Copy: CD.OD.Range("A3:BH3").PasteSpecial Paste:=xlPasteValues


    Workbooks(chemin).Worksheets("Modification").Range("A4:BH4").Copy: Workbooks(chemin2).Worksheets("Cartouche").Range("A3:BH3").PasteSpecial Paste:=xlPasteValues


'   Placer une apostrophe dans les cellules vides

    For Each Cell In Worksheets("Cartouche").Range("A3:BH3")
    If Cell.Value = "" Then
    Cell.Value = "'"

    End If
    Next Cell

'   Copier les données modifiées vers l'onglet "Tableau FNC" de ce classeur



End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
It should be
VBA Code:
OS.Range("A4:BH4").Copy: OD.Range("A3:BH3").PasteSpecial Paste:=xlPasteValues
as both OS & OD are already associated to there respective workbooks.
 
Upvote 0
Solution
It should be
VBA Code:
OS.Range("A4:BH4").Copy: OD.Range("A3:BH3").PasteSpecial Paste:=xlPasteValues
as both OS & OD are already associated to there respective workbooks.
Thank you very much Fluff! It works perfectly!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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