Application.Evaluate with multiplication and range

时间:2018-01-17 17:07:23

标签: excel vba excel-vba

我在使用Application.Evaluate编写一行代码时遇到问题。我有一个表格,其数据如下所示:

00:01  Mark  1  0  3  5 
00:02  Mark  2  8  7  1 
00:05  Mark  8  1  4  3 
00:08  Mark  9  5  5  5 
00:10  Mark  7  8  9  7 

基本上我要做的是将第一列(00:01到00:10)与第3列相乘,然后将结果放在另一张纸上,然后与第4列相同,依此类推。这个想法是为了excel做这样的事情:

((00:01 * 1)+(00:02 * 2)+(00:05 * 8)+(00:08 * 9)+(00:10 * 7))

在下面的代码中,OCT是带有表的工作表,Month是应该放置结果的工作表,MonthSkipRow和MonthRow用于在Month中查找正确的行:

Dim OCTStartRow as Range
Dim OCTEndRow as Range
Dim MonthSkipRow as Range
Dim MonthRow as Range
dim i as Long, j as Long, x as Long, y as Long, w as Long

i = 3
j = 2

Do Until i > 33

If Not OCT.Columns(2).Find("Mark", lookat:=xlWhole) Is Nothing Then

    Set OCTStartRow = OCT.Columns(2).Find("Mark", lookat:=xlWhole)
    x = OCTStartRow.Row

    Set OCTEndRow = OCT.Columns(2).Find("Mark", lookat:=xlWhole, SearchDirection:=xlPrevious)
    y = OCTEndRow.Row

    Set MonthSkipRow = Month.Columns(1).Find("Total Duration Outbound (hrs)", lookat:=xlWhole)
    z = MonthSkipRow.Row

    Set MonthRow = Month.Columns(1).Find("Mark", lookat:=xlWhole, After:=Range("A" & z))
    w = MonthRow.Row

    Month.Cells(w, j).Value = Application.Evaluate( _
        (OCT.Range(Cells(x, i), Cells(y, i)) & "*" & _
        (OCT.Range("A" & x) & ("A" & y))))
    End if
    j = j + 1
    i = i + 1

Loop 

我的问题是最后一行代码,我不断收到错误消息“运行时错误”13“:类型不匹配”。我的猜测是这条线写得不好。我希望有人知道如何正确地写它。我希望这一切都有道理。

0 个答案:

没有答案