如何通过比较两个工作表与一个“in”式的查询来创build一个新工作表?

我面临一个问题,我有两个来自SalesForce CRM的Excel表格,我需要使用它来过滤掉某个类别中的文章。 它们最初是从SF知识平台下载的,与SalesForce中由数据加载器转储的文章types和类别对象(表格)相对应。 这里是每个样品:

分类

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变体伪代码中,我会这样做:

 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中做类似的事情?

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

 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