Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Correction d'une SELECT CASE

12 réponses
Avatar
Apitos
Bonjour =E0 tous,

Dans le code suivant, j'aimerais corriger une SELECT CASE qu'elle contient =
des t=E2ches qui se r=E9p=E8tent.

Ainsi, j'aimerais d=E9finir les variables suivantes, parce que j'ai essay=
=E9 de trouver quel type elles appartiennent sans succ=E8s (Alors j'ai comm=
ent=E9 Option Explicit) :

- (Reponse) et (Canal)

'---------------------------------------------------------
'Option Explicit

Sub OuvreFich()
Dim B$(), BB$(), Arr()
'Dim Reponse As Boolean, Canal As String
Dim Item
Dim fName As String, A$
Dim i As Byte, j As Byte, LastLg As Long, X As Long

Application.ScreenUpdating =3D False
Application.EnableEvents =3D False

'On Error Resume Next
Reponse =3D Application.GetOpenFilename _
("All Files (*.*),*.*")

If Reponse =3D False Then Exit Sub
Canal =3D FreeFile
Open Reponse For Input As #Canal

'Tableau des XAC
Arr =3D Array("12", "15", "70", "33", "62")
fName =3D "TPC " ' Nom du fichier sous la forme : "TPC*.xls"
Do While Not EOF(Canal)
Line Input #Canal, A$

If Len(Trim(A$)) > 0 Then '-- Si la ligne est non vide

If InStr(1, A$, "XAC") > 0 Then
Line Input #Canal, A$
X =3D 0
Do While InStr(1, A$, "END") =3D 0
i =3D 0: j =3D 0
If Arr(X) =3D Mid(Trim(A$), 1, 2) Then
B$ =3D Split(Trim(A$), " ")
'-- Eliminer les vides du tableau B$
For Each Item In B$
If Len(Item) > 0 Then
ReDim Preserve BB$(j)
BB$(j) =3D B$(i)
j =3D j + 1
End If
i =3D i + 1
Next Item
Select Case Arr(X)
Case "12"
'ouvre PA
Workbooks.Open fName & "PA.xls"
'*-- 12 --
[E22] =3D [F22]: [F22] =3D BB$(2): [G22] =3D [H=
22]: [H22] =3D BB$(3)
Case "15"
'ouvre PA
Workbooks.Open fName & "PA.xls"
'-- 15 --
[E22] =3D [F22]: [F22] =3D BB$(2): [G22] =3D [H=
22]: [H22] =3D BB$(3)
Case "70"
'ouvre JCW
Workbooks.Open fName & "JCB.xls"
'-- 70 --
[E22] =3D [F22]: [F22] =3D BB$(2): [G22] =3D [H=
22]: [H22] =3D BB$(3)

Case "33"
'ouvre MSK
Workbooks.Open fName & "MSK.xls"
'*-- 33 --
[E22] =3D [F22]: [F22] =3D BB$(2): [G22] =3D [H=
22]: [H22] =3D BB$(3)

Case "62"
'ouvre MSK
Workbooks.Open fName & "MSK.xls"
'-- 62 --
[E22] =3D [F22]: [F22] =3D BB$(2): [G22] =3D [H=
22]: [H22] =3D BB$(3)

End Select
End If
Line Input #Canal, A$
X =3D X + 1
Loop
End If
End If
Loop
On Error GoTo 0
End Sub
'------------------------------------------

Merci d'avance.

2 réponses

1 2
Avatar
h2so4
Le mardi 5 juin 2012 02:01:45 UTC+2, Apitos a écrit :
Bonsoir h2so4, Denis

> Même si tu t'arrivais à écrire la même chose en 2 lignes de cod e de moins,
> tu n'y gagnerais pas grand-chose en efficacité.

Ca n'empêche pas de faire une nouvelle tentative ;)

Qu'en dites-vous de ce code, il est optimisé ?


'---------------------------------------------------------
Option Explicit
Dim BB$()
Dim r As Boolean
Dim fName As String
Sub OuvreFich()
Dim B$(), Arr()
Dim Reponse As Variant, Canal As Variant
Dim Item
Dim A$
Dim i As Byte, j As Byte, k As Byte, LastLg As Long, X As Long
Dim wkPA As Workbook, wkJCW As Workbook, wkMSK As Workbook

'Tableau des XAC
Arr = Array("12", "15", "70", "33", "62")
fName = "TPC " ' Nom du fichier sous la forme : "TPC*.xls"

Application.ScreenUpdating = False
Application.EnableEvents = False

'On Error Resume Next
Reponse = Application.GetOpenFilename _
("All Files (*.*),*.*")

If Reponse = False Then Exit Sub
Canal = FreeFile
Open Reponse For Input As #Canal

ReDim BB$(4, 2) ' 5 lignes et 3 colonnes

Do While Not EOF(Canal)
Line Input #Canal, A$

If Len(Trim(A$)) > 0 Then '-- Si la ligne est non vide

If InStr(1, A$, " OAC") > 0 Then
Line Input #Canal, A$
Line Input #Canal, A$
Line Input #Canal, A$
X = 0
Do While InStr(1, A$, "END") = 0
i = 0: j = 0
If Len(Mid(Trim(A$), 1, 2)) > 0 Then
If Not IsError(Application.Match(Mid(Trim(A$), 1, 2), Arr, 0)) Then
B$ = Split(Trim(A$), " ")
'-- Eliminer les vides du tableau B$
For Each Item In B$
If Len(Item) > 0 And Item <> "S" And j < = 2 Then
BB$(X, j) = B$(i)
j = j + 1
End If
i = i + 1
Next Item
X = X + 1
End If
End If
Line Input #Canal, A$
Loop
End If
End If
Loop

For k = LBound(BB$) To UBound(BB$)
Select Case BB$(k, 0)
Case "12"
Call OpenWriteF("PA", 0)
Case "70"
Call OpenWriteF("JCW", 2, False)
Case "33"
Call OpenWriteF("MSK", 3)
End Select
Next k
'On Error GoTo 0
End Sub
'------------------------------------------
Sub OpenWriteF(f As String, L As Byte, Optional r As Boolean = True)
Workbooks.Open fName & f & ".xls"
With Sheets("feuil2")
.[E22] = .[F22]: .[F22] = BB$(L, 1): .[G22] = .[H22]: .[H22 ] = BB$(L, 2)
If r Then
.[E25] = .[F25]: .[F25] = BB$(L + 1, 1): .[G25] = .[H25 ]: .[H25] = BB$(L + 1, 2):
End If
End With
End Sub



bonjour,

pas sûr que ton optimisation fasse la même chose que ton code initial. par ex, pas de traitement pour "15" ou "62".
Avatar
Apitos
Bonsoir Denis, h2so4

Désolé, je ne fais pas ce que tu demandes à moins que tu aies une
question très précise à demander.



J'ai seulement demandé si j'ai bien fait ou pas ...

pas sûr que ton optimisation fasse la même chose que ton code initial . par ex, pas de traitement pour "15" ou "62".



J'ai essayé et ca fait ce que j'attendais.

Pour le "15" et le "62", ils sont traités avec le "12" et le "33" puisque c'est le même fichier à ouvrir et y écrire.

Seul pour le "70" qu'il a une seule ligne (22) pour écrire.

C'est pour ça que j'ai ajouté le teste Vrai/Faux

Si vrai on écrit aussi dans la ligne (25).

Pour le "70" il est faux parce qu’on n’a pas besoin d'écrire dans la ligne (25).
1 2