2

我遇到了一个问题,我有两张来自 SalesForce CRM 的 Excel 表,我需要用它来过滤掉某个类别的文章。这些最初是从 SF 知识平台下载的,对应于数据加载器转储的 SalesForce 中的文章类型和类别对象(表)。以下是每个示例:

类别

ID                  PARENTID            GROUPNAME       DATACATEGORYNAME
02oC00000007RoyIAE  ka2C00000004RqwIAE  All_Products    Product1
02oC00000007TAiIAM  ka2C00000004IXuIAM  All_Products    Product1
02oC00000007TB2IAM  ka2C00000004RpFIAU  All_Products    Product2
02oC00000007TPYIA2  ka2C00000004IckIAE  All_Products    Product2

文章

ID                  TITLE
ka2C00000004RqwIAE  How to do this
ka2C00000004RqmIAE  How to do that
ka2C00000004RpFIAU  My product exploded
ka2C00000004RpFXYZ  Some title
ka2C00000004RFbIAM  How does group licensing work?

我面临的问题是我只想要Categories.DATACATEGORYNAME 是“Product2”的文章中的文章。在 C-Variant 伪代码中,我会执行以下操作:

List<CustomObject> final = new List<CustomObject>(); //Note that customObject would have fields for each of my final desired values
for (row c in categories)
{   
    for (row a in article)
    {
        if (c.PARENTID == a.ID)
        {
            final.add(new CustomObject { ID = a.ID, TITLE = a.TITLE });
        }
    }
}

然后,我会将此列表打印到 CSV 文件或类似文件中。

使用像 SQL 这样的替代技术,我会做类似“in”查询的事情。我想知道的是:有没有办法在 Excel 中做类似的事情?

4

1 回答 1

1

这是一个基本的解决方案。我会在一秒钟内添加一些解释性评论。

Sub test()


Dim i As Integer, j As Integer, k As Integer 'Define a few integer variables for counting.
Dim bookReport As Workbook, Art As Worksheet, Cat As Worksheet 'Define worksheet and workbook variables.
Dim newSheet As Worksheet 'Define variable for new worksheet (for output).

Set bookReport = Excel.ActiveWorkbook 'Set book variable to active workbook.
Set Art = bookReport.Worksheets("Article") 'Set Art to worksheet named "Article"
Set Cat = bookReport.Worksheets("Categories") 'Set Cat to worksheet named "Categories"
Set newSheet = bookReport.Worksheets.Add 'Create a new worksheet and assign it to the newSheet variable.

k = 1 'Set a starting point for "k". This variable will be used to keep track of our location on the output sheet.
For i = 1 To Cat.UsedRange.Rows.Count 'We'll use the i variable to loop through each row in the used range of the "Categories" sheet...
    For j = 1 To Art.UsedRange.Rows.Count '...and use the j variable to do the same for the "Articles" sheet.
        If Art.Cells(j, 1).Value = Cat.Cells(i, 2).Value Then 'If the first column of the [j] row in the "Articles" sheet equals the second column of the [i] row in the "Categories" sheet, then...
            newSheet.Cells(k, 1).Value = Art.Cells(j, 1).Value 'Insert the value of the first column of the [j] row in the "Articles" column into the first column of the [k] row in our new worksheet.
            newSheet.Cells(k, 2).Value = Art.Cells(j, 2).Value 'Do the same with the second column.
            k = k + 1 'Increment our tracking variable.
        End If 'Exit the conditional.
    Next j 'Increment j and repeat
Next i 'Increment i and repeat



End Sub
于 2013-11-12T19:59:52.260 回答