XL 2016 VBA - Code pour écrire une plage dans une feuille d'un classeur fermé

Dudu2

XLDnaute Barbatruc
Bonjour
Quelqu'un aurait ça dans ses tiroirs ?
Cordialement

P.S. J'ai le code pour lire en classeur fermé besoin.
 
Dernière édition:
Solution
VB:
Option Explicit

Sub Test()
    Dim Fichier As String
    Dim Feuille As String
    Dim Cellule As String
    Dim Valeur As Variant
 
    Fichier = "F:\Téléchargements\Classeur1.xlsx"
    Feuille = "Feuil1"
    Cellule = "G3"
    Valeur = "Donnée XX"
 
    Call ÉcrireDansCelluleClasseurFermé(Fichier, Feuille, Cellule, Valeur)
End Sub

'----------------------------------------------------------------------
'Permet d'écrire dans une cellule d'une feuille d'un classeur fermé.
'Attention ! La cellule cible doit être dans le UsedRange de la feuille
'            ou être A1 si la feuille cible est vide.
'----------------------------------------------------------------------
Sub ÉcrireDansCelluleClasseurFermé(Fichier As String, Feuille As...

patricktoulon

XLDnaute Barbatruc
demo.gif
 

dysorthographie

XLDnaute Accro
le problème c'est que ça fonctionne que sur la colonne A [F1] quand le fichier es vide!
VB:
Sub test()
Dim cn As String, Sql As String
Const Fichier = "C:\Myrep\Classeur vide.xlsx", AvecTitre = False
cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";"""
Sql = "Insert Into [Feuil1$A1:C100] ([F1],[F2],[F3]) values(?,?,?);"

Set a = ExecuteRequete(Sql, cn, "[F1]", "TOTO", "[F2]", "1234", "[F3]", "titi")
End Sub
Function ExecuteRequete(Sql As String, cn As String, ParamArray Param() As Variant) As Object
Dim I As Integer
With CreateObject("ADODB.Command")
    .ActiveConnection = cn
    .CommandType = 1
    .CommandTimeout = 500
     .CommandText = Sql
    For I = LBound(Param) To UBound(Param) Step 2
        Set prm = CreateObject("ADODB.Parameter")
        prm.Name = Param(I): prm.Value = Param(I + 1): prm.Type = 12
        .Parameters.Append prm
    Next
  
    Set ExecuteRequete = .Execute
End With
End Function
 

dysorthographie

XLDnaute Accro
si non Patrick test ma méthode sur ton classe structuré!
VB:
Sub test()
Dim Cn As String, Sql As String
Const Fichier = "C:\Myrep\1 exemple ecriture dans fichier fermé\1 exemple ecriture dans fichier fermé\exemple ADO ajouter une ligne dans un range ou un tableau structuré\destination.xlsx", AvecTitre = True
Cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";"""
Sql = "Insert Into [Feuil1$] ([nom], [prenom],[Tel]) values(?,?,?);
Set a = ExecuteRequete(Sql, Cn, "[nom]", "TOTO", "[prenom]", "1234", "[Tel]", "060606")

Sql = "select * from [Feuil1$] where [nom]=?"
Set a = ExecuteRequete(Sql, Cn, "[nom]", "TOTO")

End Sub
Function ExecuteRequete(Sql As String, Cn As String, ParamArray Param() As Variant) As Object
Dim I As Integer
With CreateObject("ADODB.Command")
    .ActiveConnection = Cn
    .CommandType = 1
    .CommandTimeout = 500
     .CommandText = Sql
    For I = LBound(Param) To UBound(Param) Step 2
        Set prm = CreateObject("ADODB.Parameter")
        prm.Name = Param(I): prm.Value = Param(I + 1): prm.Type = 12
        .Parameters.Append prm
    Next
 
    Set ExecuteRequete = .Execute
End With
End Function
 
Dernière édition:

Dudu2

XLDnaute Barbatruc
Ok, pas mal de trucs m'échappent dans ton code car je ne connais pas du tout ADODB.
J'ai quand même tenté de généraliser ton code, histoire d'avoir un utilitaire.
Il y aurait surement d'autres éléments à paramétrer dans cette approche "utilitaire"...
 

Pièces jointes

  • Écrire dans tableau de classeur fermé sans l'ouvrir.xlsm
    26.7 KB · Affichages: 1

Discussions similaires

Statistiques des forums

Discussions
312 215
Messages
2 086 337
Membres
103 191
dernier inscrit
camiux