我在尝试从Excel电子表格中循环两列以上的信息时遇到了一些麻烦,以便通过计算数据来创建我想要的输出。
我的任务是查找"每个销售人员上个月的客户联系人总数。"
我试图从12月份(第1列)中提取数据,以查找为每个销售人员分配了多少客户ID(第2列)(第3列)。文本框中的输出应显示销售人员" =" 总客户数
我在Visual Studio Community 2017中进行了编码,并使用EPPlus扩展来提取我的Excel电子表格。
到目前为止,这是我的代码:
Dim dateToCheck As DateTime
dateToCheck = New Date(2017, 12, 1) 'year month day
Dim diDateCustId As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))
Dim excelDate As DateTime
Dim excelCustId As String
Dim excelSalesPerson As String
For startRow = 2 To lastRow
excelDate = Convert.ToDateTime(worksheetHS.GetValue(startRow, 1).ToString())
excelCustId = worksheetHS.GetValue(startRow, 2).ToString()
excelSalesPerson = worksheetHS.GetValue(startRow, 3).ToString()
If dateToCheck.Month = excelDate.Month Then
If
If diDateCustId.ContainsKey(excelSalesPerson) Then
Dim liSt As List(Of String) = diDateCustId(excelSalesPerson)
If liSt.Contains(excelCustId) Then
' do nothing
Else
liSt.Add(excelCustId)
diDateCustId(excelSalesPerson) = liSt
End If
Else
Dim liSt As List(Of String) = New List(Of String)
liSt.Add(excelCustId)
diDateCustId.Add(excelSalesPerson, liSt)
End If
End If
Next startRow
' outputting value to Textbox
For Each keyValPair In diDateCustId
TextBox1.Text += keyValPair.Value.ToString() + " > " + keyValPair.Value.Count.ToString() + Environment.NewLine
Next
通过勾选一个单选按钮,然后单击“计算”按钮来计算该功能。这个代码示例来自我的第二个单选按钮条件,但我更改了代码中引用" excelData" 的所有部分为" excelSalesPerson" 但现在代码无法正常工作并产生错误。我不确定如何正确编码以循环所有三列以找到我想要的输出。
这是我之前计算的代码,实际上有效,供参考。任务是找到"上个月每天的客户联系总数。"
Dim dateToCheck As DateTime
dateToCheck = New Date(2017, 12, 1) 'year month day
Dim diDateCustId As Dictionary(Of Date, List(Of String)) = New Dictionary(Of Date, List(Of String))
Dim excelDate As DateTime
Dim excelCustId As String
For startRow = 2 To lastRow
excelDate = Convert.ToDateTime(worksheetHS.GetValue(startRow, 1).ToString())
excelCustId = worksheetHS.GetValue(startRow, 2).ToString()
If dateToCheck.Month = excelDate.Month Then
If diDateCustId.ContainsKey(excelDate) Then
Dim liSt As List(Of String) = diDateCustId(excelDate)
If liSt.Contains(excelCustId) Then
' do nothing
Else
liSt.Add(excelCustId)
diDateCustId(excelDate) = liSt
End If
Else
Dim liSt As List(Of String) = New List(Of String)
liSt.Add(excelCustId)
diDateCustId.Add(excelDate, liSt)
End If
End If
Next startRow
' outputting value to Textbox
For Each keyValPair In diDateCustId
TextBox1.Text += Convert.ToDateTime(keyValPair.Key).ToShortDateString() + " > " + keyValPair.Value.Count.ToString() + Environment.NewLine
Next
答案 0 :(得分:0)
给出一些像这样的数据:
┌────────┬──────────────┬──────────────┐
│ Month │ Customer IDs │ Sales Person │
├────────┼──────────────┼──────────────┤
│ Oct-17 │ 1 │ John Smith │
│ Oct-17 │ 2 │ Jane Doe │
│ Dec-17 │ 1 │ John Smith │
│ Dec-17 │ 2 │ John Smith │
│ Dec-17 │ 2 │ John Smith │
│ Dec-17 │ 1 │ Jane Doe │
│ Dec-17 │ 2 │ Jane Doe │
└────────┴──────────────┴──────────────┘
您可以选择表格并插入数据透视表。
Month
字段拖到下面的过滤器区域。Customer IDs
拖到值区域。Customer IDs
旁边的下拉菜单,然后选择Value Field Settings...
。从列表中选择Count
,设置自定义名称,然后点击OK
。Sales Person
拖到行区域。将行标签替换为"销售人员"。查看结果。
┌────────────────┬────────────────┐
│ Month │ Dec-17 │
├────────────────┼────────────────┤
│ Sales Person │ # of Customers │
├────────────────┼────────────────┤
│ Jane Doe │ 2 │
│ John Smith │ 3 │
├────────────────┼────────────────┤
│ Grand Total │ 5 │
└────────────────┴────────────────┘