我遇到的问题是,我有两张来自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中做类似的事情?
答案 0 :(得分: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