OVH Cloud OVH Cloud

fonction FIND ?

3 réponses
Avatar
catherine.soranzo
Bonjour à tous,

Voilà mon problème :
Dans un fichier excel(97), j'ai une colonne (F) "PAYS" et une colonne
(A) "RESEAU".
En VBA, je voudrais savoir comment faire pour qu'à chaque mot
"VIETNAM" ou "HONG-KONG" trouvé dans la colonne "PAYS", le code "01"
soit mis automatiquement dans la colonne "RESEAU".

Quelqu'un pourrait-il m'aider ???

Merci d'avance à ceux qui répondront.
A+
Catherine

3 réponses

Avatar
François Picalausa
Bonjour/soir,

tu peux essayer ceci:
Dim xlWkSt As Excel.WorkSheet

Set xlWkSt = ActiveWorkSheets(1) 'Feuille active

Dim LastCell As Excel.Range
Set LastCell = xlWkSt.Cells.SpecialCells(xlCellTypeLastCell)

Dim i As Long

For i = 1 To LastCell.Row
If Instr(1, uCase$(xlWkSt.Cells(i, 6).Text), "VIETNAM") Then
xlWkSt.Cells(i, 1).FormulaR1C1="1"
End If
Next i


--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"Medinlu" a écrit dans le message de
news:
Bonjour à tous,

Voilà mon problème :
Dans un fichier excel(97), j'ai une colonne (F) "PAYS" et une colonne
(A) "RESEAU".
En VBA, je voudrais savoir comment faire pour qu'à chaque mot
"VIETNAM" ou "HONG-KONG" trouvé dans la colonne "PAYS", le code "01"
soit mis automatiquement dans la colonne "RESEAU".

Quelqu'un pourrait-il m'aider ???

Merci d'avance à ceux qui répondront.
A+
Catherine


Avatar
ng
Bjour,
If Instr(1, uCase$(xlWkSt.Cells(i, 6).Text), "VIETNAM") Then


Un "If uCase$(xlWkSt.Cells(i, 6).Text like "*VIETNAM*" Then" ne serait-il
pas plus rapide ?

Nicolas.

"François Picalausa" a écrit dans le message de news:

Bonjour/soir,

tu peux essayer ceci:
Dim xlWkSt As Excel.WorkSheet

Set xlWkSt = ActiveWorkSheets(1) 'Feuille active

Dim LastCell As Excel.Range
Set LastCell = xlWkSt.Cells.SpecialCells(xlCellTypeLastCell)

Dim i As Long

For i = 1 To LastCell.Row
If Instr(1, uCase$(xlWkSt.Cells(i, 6).Text), "VIETNAM") Then
xlWkSt.Cells(i, 1).FormulaR1C1="1"
End If
Next i


--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"Medinlu" a écrit dans le message de
news:
> Bonjour à tous,
>
> Voilà mon problème :
> Dans un fichier excel(97), j'ai une colonne (F) "PAYS" et une colonne
> (A) "RESEAU".
> En VBA, je voudrais savoir comment faire pour qu'à chaque mot
> "VIETNAM" ou "HONG-KONG" trouvé dans la colonne "PAYS", le code "01"
> soit mis automatiquement dans la colonne "RESEAU".
>
> Quelqu'un pourrait-il m'aider ???
>
> Merci d'avance à ceux qui répondront.
> A+
> Catherine




Avatar
François Picalausa
Bonjour/soir,

Avec le code suivant (sous VBA Excel):

'-----------------------------------------------------------------
'CStopWatch: http://www.mvps.org/vb/tips/benchmarks.htm
Option Explicit

Sub Main()
Const LOOPS As Long = 5000
Const SomeText1 As String = "vIeTNaM"
Const SomeText2 As String = "/2ù$^6dsdvVIETNAMqscqcsd$^$ù$6dc"
Const SomeText3 As String = " VINAM "

Dim tloop As Long
Dim i As Long
Dim t1 As Long
Dim t2 As Long
Dim t3 As Long
Dim t4 As Long
Dim Stopwatch As CStopWatch

Set Stopwatch = New CStopWatch

Stopwatch.Reset
For i = 1 To LOOPS
Next i
tloop = Stopwatch.Elapsed

Stopwatch.Reset
For i = 1 To LOOPS
CompareLike SomeText1
CompareLike SomeText2
CompareLike SomeText3
Next i
t1 = Stopwatch.Elapsed

Stopwatch.Reset
For i = 1 To LOOPS
CompareInstrBin SomeText1
CompareInstrBin SomeText2
CompareInstrBin SomeText3
Next i
t2 = Stopwatch.Elapsed

Stopwatch.Reset
For i = 1 To LOOPS
CompareInstrTxt SomeText1
CompareInstrTxt SomeText2
CompareInstrTxt SomeText3
Next i
t3 = Stopwatch.Elapsed

Stopwatch.Reset
For i = 1 To LOOPS
CompareLike2 SomeText1
CompareLike2 SomeText2
CompareLike2 SomeText3
Next i
t4 = Stopwatch.Elapsed

Debug.Print "Loop : " & tloop
Debug.Print "Like : " & t1
Debug.Print "InstrBin : " & t2
Debug.Print "InstrText : " & t3
Debug.Print "Like2 : " & t4
End Sub

Function CompareLike(Text As String) As Boolean
CompareLike = UCase$(Text) Like "*VIETNAM*"
End Function

Function CompareLike2(Text As String) As Boolean
CompareLike2 = Text Like "*[Vv][Ii][Ee][Tt][Nn][Aa][Mm]*"
End Function

Function CompareInstrBin(Text As String) As Boolean
CompareInstrBin = (InStr(1, UCase$(Text), "VIETNAM", vbBinaryCompare) >
0)
End Function

Function CompareInstrTxt(Text As String) As Boolean
CompareInstrTxt = (InStr(1, Text, "VIETNAM", vbTextCompare) > 0)
End Function

'-----------------------------------------------------------------

J'obtiens:

Loop : 0
Like : 25
InstrBin : 23
InstrText : 50
Like2 : 37

Donc, surtout, ne pas utiliser Instr en mode de comparaison vbTextCompare
Sinon, Inst en comparaison binarie est légèrement plus rapide (ça varie
entre 1 et 2 millisecondes sur tous mes test)

et sur 500000 boucles, j'obtiens:
Loop : 5
Like : 2617
InstrBin : 2512
InstrText : 5130
Like2 : 3200

Donc, il faut comment à faire de l'utilisation très très très intensive pour
voir la différence.
(sous VB, en mode debug, les résultats sont les mêmes à peu de choses près)

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"ng" a écrit dans le message de
news:
Bjour,
If Instr(1, uCase$(xlWkSt.Cells(i, 6).Text), "VIETNAM") Then


Un "If uCase$(xlWkSt.Cells(i, 6).Text like "*VIETNAM*" Then" ne
serait-il pas plus rapide ?

Nicolas.