大家在工作中,可能会遇到这样的情况,就是需要将文字依照给定的标准换算为数值并求和。数据源如下:
我们需要依据表一的标准,把表二相应文字对照换算为数值并求和。如下图:
其实我们这些小白可以新建个工作表,再用VLOOKUP函数和sum函数即可实现。当然,这里说说如果用VBA的话,我们该怎么做呢?代码如下所示:Sub 替换求和()Dim A, D, E As Integer, F(1 To 7) As Single, H As ByteWith Sheets("表一")A = .Range("A1").CurrentRegionEnd WithWith Sheets("表二")D = .Range("A1").CurrentRegionFor E = 2 To UBound(D)For H = 2 To 8If D(E, H) "" ThenIf Not IsError(Application.VLookup(D(E, H), A, 1, 0)) ThenF(H - 1) = Application.VLookup(D(E, H), A, 2, 0)End IfEnd IfNextD(E, 9) = Application.Sum(F): Erase FNext.Range("A1").Resize(UBound(D), UBound(D, 2)) = DEnd WithEnd Sub |