If cu multiple conditii in VBA

tyro_excel_vba
Mesaje: 27
Membru din: Joi Oct 18, 2018 3:07 pm

If cu multiple conditii in VBA

Mesaj de tyro_excel_vba » Mar Sep 24, 2019 2:11 pm

Buna,

Am scris un cod care se uita pe o coloana si daca regaseste o valoare sau mai multe valori imi intoarce o informatie.
Problema mea, acolo unde pe coloana ar trebui sa gaseasca mai multe variante (2 sau mai multe) imi ia in considerare ultima conditie (ex daca in fisier pe coloana M am filtru pe ADRIATIC, imi intoarce ca Iso "East", daca pun comment conditia care intoarce rezultatul Iso =East, imi intoarce Iso "SE", daca si conditia care intoarce Iso = SE o trec comment, imi intoarce Iso= DK, iar daca consitia asta o trec comment, imi intoarce rezultatul corect.

Sunt convinsa ca am scris ceva gresit in cod, dar nu imi dau seama ce, am incercat mai multe variante,dar nu o scot la capat...

Sub sendmultiple()

Dim CountryIso As Range
Dim Iso As String
Dim xCell As Range

Set CountryIso = Range("M2:M1000")
If CountryIso Is Nothing Then Exit Sub

ActiveSheet.Range("$A$2:$AE$316").AutoFilter Field:=14

For Each xCell In CountryIso
If xCell.Value = "ADRIATIC" And xCell.EntireRow.Hidden = False Then
Iso = "Adriatic"
End If

If xCell.Value = "Algeria" And xCell.EntireRow.Hidden = False Then
Iso = "DZ"
End If

If xCell.Value = "Argentina" And xCell.EntireRow.Hidden = False Then
Iso = "AR"
End If

If xCell.Value = "Australia" And xCell.EntireRow.Hidden = False Then
Iso = "AU"
End If

If xCell.Value = "Brazil" And xCell.EntireRow.Hidden = False Then
Iso = "BR"
End If

If xCell.Value = "Bulgaria" And xCell.EntireRow.Hidden = False Then
Iso = "BG"
End If


If xCell.Value = "China (DRAC)" And xCell.EntireRow.Hidden = False Then
Iso = "CN"
End If

If xCell.Value = "Colombia" And xCell.EntireRow.Hidden = False Then
Iso = "CO"
End If

If xCell.Value = "GCC" And xCell.EntireRow.Hidden = False Then
Iso = "GCC"
End If

If xCell.Value = "Germania" And xCell.EntireRow.Hidden = False Then
Iso = "DE"
End If

If xCell.Value = "India" And xCell.EntireRow.Hidden = False Then
Iso = "IN"
End If

If xCell.Value = "Israel" And xCell.EntireRow.Hidden = False Then
Iso = "IL"
End If

If xCell.Value = "Italy" And xCell.EntireRow.Hidden = False Then
Iso = "IT"
End If

If xCell.Value = "Korea" And xCell.EntireRow.Hidden = False Then
Iso = "KR"
End If

If xCell.Value = "Mexique" And xCell.EntireRow.Hidden = False Then
Iso = "MX"
End If

If xCell.Value = "Morocco" And xCell.EntireRow.Hidden = False Then
Iso = "MA"
End If

If xCell.Value = "Netherlands" And xCell.EntireRow.Hidden = False Then
Iso = "NL"
End If

If xCell.Value = "Romania" And xCell.EntireRow.Hidden = False Then
Iso = "RO"
End If

If xCell.Value = "Russia" And xCell.EntireRow.Hidden = False Then
Iso = "RU"
End If

If xCell.Value = "UK" And xCell.EntireRow.Hidden = False Then
Iso = "UK"
End If

If xCell.Value = "Ukraine" And xCell.EntireRow.Hidden = False Then
Iso = "UA"
End If

If xCell.Value = "Denmark ext" Or xCell.Value = "NORDICS" And xCell.EntireRow.Hidden = False Then
Iso = "DK"
End If

If xCell.Value = "Sweden ext" Or xCell.Value = "NORDICS" And xCell.EntireRow.Hidden = False Then
Iso = "SE"
End If

If xCell.Value = "Czech republic" Or xCell.Value = "Hungary" Or xCell.Value = "Poland" Or xCell.Value = "MidCE" And xCell.EntireRow.Hidden = False Then
Iso = "East"
End If

Next

End sub

IPP
Moderator
Moderator
Mesaje: 4196
Membru din: Mie Iul 29, 2009 7:26 am
Localitate: Cluj-Napoca

Re: If cu multiple conditii in VBA

Mesaj de IPP » Mar Sep 24, 2019 5:09 pm

Buna ziua

Cand e vorba de macro eu unul si in principiu astept si un fisier exemplu.

Altfel si ca idee (daca am inteles bine ce se doreste in final) eu as incerca o solutie bazata pe replace la nivel de celule vizibile.

Daca doriti sa ramaneti la codul dvs. atunci ati putea rescrie partea de if-uri:

If xCell.EntireRow.Hidden = False Then
If xCell.Value = "ADRIATIC" Then Iso = "Adriatic"
'restul de situatii...
'.......
If xCell.Value = "Czech republic" Then Iso = "East"
If xCell.Value = "Hungary" Then Iso = "East"
If xCell.Value = "Poland" Then Iso = "East"
If xCell.Value = "MidCE" Then Iso = "East"
End If

IP

Scrie răspuns

Înapoi la “Visual Basic for Application (VBA) cu Excel - Intrebari tehnice”