我如何从大数据中获取国家/地区名称?

时间:2018-05-02 05:05:40

标签: excel excel-vba excel-formula vba

如何从数据中提取国家/地区名称?

https://i.stack.imgur.com/Xhe97.jpg

4 个答案:

答案 0 :(得分:1)

  1. 假设您有从J1J5的国家/地区列表,如下面的屏幕截图所示。

    enter image description here

  2. 在模块中复制并粘贴以下功能。见截图。

    Option Explicit
    
    Public Function getCountry(ByVal srtText As String) As String
    Dim CountryArray As Variant
    Dim i As Long
    
        Application.Volatile
        CountryArray = Application.Transpose(Range("J1:J5").Value2)
        For i = LBound(CountryArray) To UBound(CountryArray)
            If (InStr(1, srtText, CountryArray(i), 1)) > 0 Then
                getCountry = CountryArray(i)
                Exit Function
            End If
        Next i
    End Function
    

    enter image description here

    1. 然后在B1单元格中使用以下功能。

      =getCountry(A1)
      
    2. enter image description here

答案 1 :(得分:1)

首先列出一个国家/地区列表 - 最好是 - 另一张表格。下面的屏幕截图是 Sheet2

上的示例列表

country list

假设您的数据位于下方,请将以下公式写入 Sheet1 B1,然后按 Ctrl + Shift + 输入以激活数组公式。

=INDEX(Sheet2!$A$1:$A$5,SUMPRODUCT(ISNUMBER(FIND(Sheet2!$A$1:$A$5,A1))*ROW(Sheet2!$1:$5)))

data & formula

  • 如果您的国家/地区列表包含的项目超过5项,那么您应该更改公式Sheet2!$A$1:$A$5& ROW(Sheet2!$1:$5)相应的部分。

答案 2 :(得分:0)

    'As far as I know this task can be achieve if we have list of all country.
'here CountyText.text file contain list of all country.
'for demo this list collect from internet you can add your own list in this file.
'This function can be use where compitational time doesn't matter.
'Before use this function copy paste this code in new module.

Function FindCountry(cellstring As String) As String
Dim DataLine As String
Dim File As String
File = "D:\CountyText.txt" 'Path of county text file
'(for demo this list collect from internet) you can add your own list in this file
FindCountry = "" 'initial value of function

On Error Resume Next 'this is for error handling
Close #1
Open File For Input As #1
On Error GoTo 0

While Not EOF(1) 'while end of file
    Line Input #1, DataLine ' read in data 1 line at a time
If InStr(cellstring, DataLine) Then
FindCountry = DataLine
End If
Wend

Close #1
End Function

编写countryText.txt文件格式见截图

countryText.txt

OutPut

添加" USA"在countryText.txt文件中,它可以识别

答案 3 :(得分:0)

您可以在单元格B2中使用公式,如下所示:

=LOOKUP(2^15,SEARCH(CountryList,A2,1),CountryList)

  

其中CountryList是指包含您要查找的国家/地区名称的范围!