What I am trying to do is have a dynamic list of companies (I use data validation) that once chosen an impact number would be reported.
My data set consists of a list of companies and the associated country codes in which they operate (Company Table pic). The second data set has the country codes associated with the country name and the number of citizens in this country (Country Table pic).
A countries citizens are only impacted if 2 or more companies chosen are operating in that country.
Say I chose company 4 and company 6 in my drop down list (Company Selection pic). Since both these companies operate in "AZ" and "BJ", I'm trying to get the output cell to populate with 1,079.
I want this to be possible with up to 20 company selections.
I wanted to do this with an index match but I couldnt understand how I could get it to return an array. My thinking is that for each company selection I would start to build up a unique array which would be populated with each companies country of operation. Then after that array has been built from all chosen company locations I would then assess that array and see if any country code occurs 2 or more times. If it does then I could return those country codes and use them to sumifs on the second database.
Attempt Code:
Function Impact(CompanySelection As Range, CompanyTable As Range, CountryTable As Range)
Dim CountryCodes As Object
Dim LookupCountries As Object
Dim Results As Object
Dim CImpact As Long
Dim cell As Variable
For Each cell In CompanySelection.Range
If cell.Value = "" Then
Exit For
CountryCodes.Add Application.WorksheetFunction.Index(CompanyTable, Application.WorksheetFunction.Match(cell, CompanyTable, 0), 2)
CountryCodes.Add Application.WorksheetFunction.Index(CompanyTable, Application.WorksheetFunction.Match(cell, CompanyTable, 0), 3)
CountryCodes.Add Application.WorksheetFunction.Index(CompanyTable, Application.WorksheetFunction.Match(cell, CompanyTable, 0), 4)
CountryCodes.Add Application.WorksheetFunction.Index(CompanyTable, Application.WorksheetFunction.Match(cell, CompanyTable, 0), 5)
Next
For each cell in CountryCodes
count # of occurances of each unique country code
If code in CountryCodes occurs >=2 Then
LookupCountries.Add Value
For Each cell In LookupCountries
Result.Add Application.WorksheetFunction.Index(CountryTable,
Application.WorksheetFunction.Match(cell, CountryTable, 2))
Next
For Each cell In Result
CImpact = CImpact + cell.Value
Next
Impact = CImpact
End Function
Company table
| Company | Country | Country | Country |
| ------------ | ------------ | ------------ | ------------ |
| Company 1 | AO | BZ | BS |
| Company 2 | BW | AQ | AO |
| Company 3 | BA | BI | |
| Company 4 | BR | AZ | BJ |
| Company 5 | AI | | |
| Company 6 | AZ | BJ | BS |
Country Table
| Country | Citizens |
| ------------ | ------------ |
| AO | 582 |
| AI | 536 |
| AQ | 350 |
| AZ | 732 |
| BA | 408 |
| BI | 826 |
| BJ | 347 |
| BR | 767 |
| BS | 336 |
| BW | 604 |
| BW | 601 |
Company Selection
| Company Selection |
| ------------ |
| Company 4 |
| Company 6 |
| ... |
| ... |
| ... |
| ... |
Output Cell
| Impacted Citizens = |
| ------------ |
3条答案
按热度按时间alen0pnh1#
使用字典计数的解决方案
8cdiaqws2#
我认为现在它是正确的。a)Concat每个公司的国家,B)concat选定公司的国家,c)检查每个国家是否存在twise在(b),d)设置公民在主表只有当国家存在twise。但VBA更灵活!
vsmadaxz3#
我使用了wrbp的解决方案,并做了一些修改以适应我的用例。