简体   繁体   中英

VBA - Match range to see if cell value is in array

ISOmatch contains all the values that are in the arrays sISO and sISOnew. I am looking to copy values from approximately 200 different workbooks int one file. ISOmnatch is a column of all the ISO codes in the arrays.

I am trying to have it cycle through each book, copy a value, find the ISO match and paste that value, then move to the next book, find the iso match, etc.

Here is the code I have worked up so far. The main issue I am hacing is how to detect if there is a match or not such as "If cell.value = sISO() or sISOnew() Then". I am not too sure how to implement the embedded excel match function or if that would even be easier.

    Sub NTSDM_Econ()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim isoWF, isoNWF As String
Dim sISO As Variant
Dim sISOnew As Variant

isoWF = "J:\Washington\Groups\CIG\GRS\Workfiles\"
isoNWF = "J:\Washington\Groups\CIG\GRS\Workfiles\New Countries\"

sISO = Array( _
"AFG", "AGO", "ALB", "ARE", "ARG", "ARM", "AUS", "AUT", "AZE", "BDI", "BEL", "BFA", "BGD", _
"BGR", "BHR", "BIH", "BLR", "BOL", "BRA", "BTN", "BWA", "CAN", "CHE", "CHL", "CHN", _
"CIV", "CMR", "COG", "COL", "CRI", "CUB", "CYP", "CZE", "DEU", "DNK", "DOM", "DRC", "DZA", _
"ECU", "EGY", "ESP", "EST", "FIN", "FRA", "GAB", "GBR", "GEO", "GHA", "GIN", "GNQ", "GRC", _
"GTM", "HKG", "HND", "HRV", "HUN", "IDN", "IND", "IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", _
"JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KOR", "KOS", "KWT", "LAO", "LBN", "LBR", "LBY", _
"LKA", "LSO", "LTU", "LVA", "MAR", "MDA", "MDG", "MEX", "MKD", "MLI", "MMR", "MNE", "MNG", _
"MOZ", "MRT", "MUS", "MWI", "MYS", "NAM", "NCL", "NER", "NGA", "NIC", "NLD", "NOR", "NPL", _
"NZL", "OMN", "PAK", "PAN", "PER", "PHL", "PNG", "POL", "PRI", "PRT", "PRY", "QAT", "ROM", _
"RUS", "SAU", "SDN", "SEN", "SGP", "SLE", "SLV", "SRB", "SSD", "SVK", "SVN", _
"SWE", "SWZ", "SYR", "TGO", "THA", "TJK", "TKM", "TLS", "TTO", "TUN", "TUR", "TWN", "TZA", _
"UGA", "UKR", "URY", "USA", "UZB", "VEN", "VNM", "YEM", "ZAF", "ZMB", "ZWE")

sISOnew = Array( _
"ABW", "AIA", "AND", "ASM", "ATG", "BEN", "BHS", "BLZ", "BMU", "BRB", "BRN", "CAF", "COM", _
"CPV", "CUW", "CYM", "DJI", "DMA", "ERI", "ETH", "FJI", "FSM", "GMB", "GNB", "GRD", "GUF", _
"GUM", "GUY", "HTI", "JAM", "KIR", "KNA", "LCA", "LIE", "LUX", "MAC", "MCO", "MDV", "MHL", _
"MLT", "MTQ", "NRU", "PLW", "PRK", "PSE", "REU", "RWA", "SLB", "SMR", "SOM", "STP", "SUR", _
"SXM", "SYC", "SYC", "TCD", "TON", "TUV", "VCT", "VIR", "VUT", "WSM")

' smaller array used to test
sISO = Array("AFG", "AGO")

For Each ctry In sISO
    For Each Cell In Range("B4:B214")
        If UBound(Filter(sISO, Cell.Value)) > -1 Then

            '' do some stuff

            ActiveWorkbook.Close
        End If
    Next
Next ctry

End Sub

You can test if an item is in an array with Filter(). Instead of If cell.value = sISO() or sISONew() something like if UBound(Filter(sISO, cell.value)) > -1 or UBound(Filter(sISONew, cell.value)) > -1 then ... should work.

You can use the function InStr() to see if the values match, but you then need to have all your values in a string. Here is the part of the code that could do that. Just make sure you fill the array before you enter the loops

Dim ISOlist As String
Dim i As Long
Dim myVal As String

'Fill the two arrays into one long string with comma-separated values.
'The string need to make sure there is a comma both before and after each value
For i = LBound(sISO) To UBound(sISO)
    ISOlist = ISOlist & "," & sISO(i)
Next
For i = LBound(sISOnew) To UBound(sISOnew)
    ISOlist = ISOlist & "," & sISOnew(i)
Next
ISOlist = ISOlist & ","        'the ending comma is important

        'Get the value that I should test, adding a comma before and after
        myVal = "," & cell.value & ","

        'If cell.Value = sISO() Or sISOnew() Then
        If InStr(myVal, ISOlist) > 0 Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM