比方说,我有如下表:
https://i.imgur.com/qa2kiFv.png
在一个不同的表,我有适用的扣数的逗号分隔的值的列:(例如,1,4,6)。
我怎样才能VLOOKUP所有相应的扣除数点值,总结起来?
例如,如果单元格表示7,1,和图2a,它会综上所述-15,-5和-2总共-22。
例子:https://i.imgur.com/CjhNkZU.png
(注:我没有足够的信誉发表图片,所以如果有人将仁者作为编辑的帖子,并添加图片,我会很感激的。)
对于这个工作转换你必须到Excel结构表中的数据(备份您的工作簿,选择数据和按“Ctrl” +“T”,指定一个名称表) 检查本作进一步参考: https://support.office.com/en-us/article/overview-of-excel-tables-7ab0bb7d-3a9e-4b56-a3c9-6c94334e492c
从本质上说,你应该有两个表,因为这: - 表保存的扣点(我叫它TableDeduction) - 表中包含典型的扣除数(我叫它TableTypical)
复制此代码粘贴到一个模块和自定义文本下面每一行>>>>自定义:
Sub GetPoints()
' Declare objects variables
Dim typicalTable As ListObject
Dim deductionTable As ListObject
Dim typicalCell As Range
' Declare other variables
Dim sheetName As String
Dim typicalTableName As String
Dim deductionTableName As String
Dim typicalValues As Variant ' Array
Dim deductionValue As Integer ' Change for long if sum is gonna be greater than 32.000
' Generic variables
Dim counter As Integer
' >>>> Customize to fit your needs
sheetName = "Sheet1"
typicalTableName = "TableTypical"
deductionTableName = "TableDeduction"
' <<<<
' Initiate table objects
Set typicalTable = ThisWorkbook.Worksheets(sheetName).ListObjects(typicalTableName)
Set deductionTable = ThisWorkbook.Worksheets(sheetName).ListObjects(deductionTableName)
' Loop through the typical table cells
For Each typicalCell In typicalTable.DataBodyRange.Columns(1).Cells
' Validate that it's valid
If typicalCell.Value <> "None" Then
' Reinitiate the sum
deductionValue = 0
' Split to cell values by commas
typicalValues = Split(typicalCell.Value, ",")
' For each value look it's corresponding deduction points
For counter = 0 To UBound(typicalValues)
' >>>> Customize the columns number
If IsError(Application.Match(CStr(typicalValues(counter)), deductionTable.DataBodyRange.Columns(1), 0)) Then
' >>>> Customize the columns number
' Lookup the table for numbers
If IsNumeric(Application.Index(deductionTable.DataBodyRange.Columns(2), Application.Match(CLng(typicalValues(counter)), deductionTable.DataBodyRange.Columns(1), 0))) Then
' >>>> Customize the columns number
deductionValue = deductionValue + Application.Index(deductionTable.DataBodyRange.Columns(2), Application.Match(CLng(typicalValues(counter)), deductionTable.DataBodyRange.Columns(1), 0))
End If
Else
' >>>> Customize the columns number
' Lookup the table for string
If IsNumeric(Application.Index(deductionTable.DataBodyRange.Columns(2), Application.Match(CStr(typicalValues(counter)), deductionTable.DataBodyRange.Columns(1), 0))) Then
' >>>> Customize the columns number
deductionValue = deductionValue + Application.Index(deductionTable.DataBodyRange.Columns(2), Application.Match(CStr(typicalValues(counter)), deductionTable.DataBodyRange.Columns(1), 0))
End If
End If
' Output the value in the next cell
typicalCell.Offset(0, 1).Value = deductionValue
Next counter
End If
Next typicalCell
End Sub