Beaucoup trop lent

XGuarden

New Member
Joined
Mar 8, 2006
Messages
3
Code:
Const sConnect As String = "driver={SQL Server};server=[removed by admin];uid=[removed by admin];pwd=[removed by admin];database=[removed by admin]"

Public Sub UpdateSQL()


    Dim Page As String
    Dim Cellule As String
    Dim Selection As String
    Dim Exclusion As String
    Dim Annee As String
    
    Annee = Sheets("Paramêtres").Range("B3")
    
Dim x As Integer

With Sheets("Paramêtres")
    For x = 10 To 108
        Page = .Range("B" & x)
        Cellule = .Range("C" & x)
        Selection = .Range("D" & x)
        Exclusion = .Range("E" & x)
    
        Call Afficher(Page, Cellule, SelectionSQL(Selection, Exclusion) & Annee & "'")
    Next x
End With

End Sub

Code:
Private Sub Afficher(Page As String, Cellule As String, SQL As String)
On Error Resume Next
    Dim rs As ADODB.Recordset
    
    Sheets(Page).Select
        
    Set rs = New ADODB.Recordset

    rs.Open SQL, sConnect, asOpenForwardOnly, adLockReadOnly
    
    'Range(Cellule).Select

    If rs.Fields(0).Value <> 0 Then
        Range(Cellule) = CDec(rs.Fields(0).Value)
    Else
        Range(Cellule) = 0
    End If

    If CBool(rs.State And adStateOpen) Then rs.Close
    Set rs = Nothing
    
End Sub

Code:
Private Function SelectionSQL(Selection As String, Exclusion As String) As String

Dim SQL As String
Dim Caractere As String
Dim chaine As String

Dim Increment As Integer

Increment = 1

SQL = "SELECT SUM(SLDE) FROM dbo.FIN_GLG WHERE (NO_CMPT like '"

'Calcul de la selection
Do While Increment <> Len(Selection) + 1

    Caractere = Mid(Selection, Increment, 1)

If Caractere = "*" Then
    chaine = chaine & "_"
ElseIf Caractere = "/" Then
    SQL = SQL & chaine & "' OR NO_CMPT like "
    chaine = "'"
Else
    chaine = chaine & Caractere
End If

    Increment = Increment + 1

Loop

SQL = SQL & chaine & "')"


 'Calcul de l'exclusion
If Exclusion <> "" Then

    chaine = ""
    Caractere = ""
    Increment = 1
    
    SQL = SQL & " AND NOT NO_CMPT like '"
 
    Do While Increment <> Len(Exclusion) + 1

        Caractere = Mid(Exclusion, Increment, 1)

        If Caractere = "*" Then
            chaine = chaine & "_"
        ElseIf Caractere = "/" Then
            SQL = SQL & chaine & "' AND NOT NO_CMPT like "
            chaine = "'"
        Else
            chaine = chaine & Caractere
        End If

        Increment = Increment + 1

    Loop

    SQL = SQL & chaine & "'"
    
End If

SQL = SQL & " AND EXER_FIN = '"

SelectionSQL = SQL

End Function

[Edited by admin]
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
PRécision

J'avais changer les informations par des lettres au assards. JE donnerais pas le login etle mot de passe comme sa,....
La questino est simple
Je veut optimizer le code pour qu'il soit plus rapide
Il est extremement lent.
 
Upvote 0
My French grammar is so bad as to be offensive, so I'll try in English. Juan is 10× better at this stuff than I (and that's a conservative estimate), but I would think that perhaps you could pull a recordset one time and then apply filters to that record set rather than reopening the recordset time after time. If your recordsets are huge that might not be an option. But that's what I would try first.

Oh, and it should not matter because adOpenForwardOnly = 0 but you have an error in the constant name (you have asOpenForwardOnly).
 
Upvote 0
Bonjour, XGuarden,
BIENVENU à Bord !!!

Les recordset etcetera n'est pas ma spécialité, mais peut-être deux remarques pourraitnet être utiles.

1. Il serait préférable d'éviter l'utilisation de variables nommées comme "selection" (et increment, ...), puisque c'est des mots qui ont déjà une signifacation spécifique dans "la syntaxe générale" de VBA. En plus il ne s'agit pas d'une selection proprement dite mais du contenu de cette selection, à moins que j'aie mal compris. Ceci une parenthèse qui n'influencera pas vraiment le fonctionnement, mais peut-être bien la facilité de rédiger le code dans le futur.
2. est-ce que ce "loop" ne pourrait pas être évité, au moins partiellement ?
Code:
Do While Increment <> Len(Selection) + 1 

    Caractere = Mid(Selection, Increment, 1) 

If Caractere = "*" Then 
    chaine = chaine & "_" 
ElseIf Caractere = "/" Then 
    SQL = SQL & chaine & "' OR NO_CMPT like " 
    chaine = "'" 
Else 
    chaine = chaine & Caractere 
End If 

    Increment = Increment + 1 

Loop
Il me semble aussi qu'un "For ... Next" serait plus rapide que le "Loop ... While"
Code:
For I = 1 To Len(Selection) + 1
Caractere = Mid(Selection, I, 1)
Il se peut que ma compréhension de que tu veux faire n'est pas complète, quelques examples de "selections" possibles seraient utiles, les commentaires suivantes sont donc à voir ...
la partie ci-dessous pourrait être effectué en une seule ligne
Code:
If Caractere = "*" Then 
    chaine = chaine & "_"
utilisant Application.Substitute
Code:
Const test = "abcd*fghi*kl"
MsgBox Application.Substitute(test, "*", "_")
pensons aussi à utiliser la function Instr afin d'éviter trop de "loop" non-nécessaires ...
exemple
Code:
Const test = "abcd*fghi*k/l"
MsgBox InStr(1, test, "/")  'position de la première apparance de "/"
en espérant que ceci puisse être utile ou au moins donner de nouvelles idées ...

bien à toi,
Erik
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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