2014-01-17 30 views
0

我在工作簿中有多个工作表。我正在尝试总计D-G列,并且他们应用一个变量if..then声明结果。If ... Then语句在新位置显示结果

使用下面的代码提供了一个总数,但我无法弄清楚如何让“if”正常工作并显示结果。

Dim colm As Long, StartRow As Long 
Dim EndCell As Range 
Dim ws As Worksheet 
StartRow = 3 
For Each ws In Worksheets 
    Set EndCell = ws.Cells(Rows.Count, "c").End(xlUp).Offset(1, 1) 
    If EndCell.Row > StartRow Then 
    EndCell.Resize(, 4).Formula = "=SUM(R" & StartRow & "C:R[-1]C)" 
    End If 
    If EndCell.Row >= 0 Then J3 = "6 Free Bottles" 
    If EndCell.Row >= 1000 Then 
    J2 = Formula = ((EndCell.Row) * (0.05)) 
    J3 = "5% Discount" 
    ElseIf EndCell.Row >= 3000 Then 
    Range(J2) = Formula = ((EndCell.Row) * (0.1)) 
    Range(J3) = "10% Discount" 
    End If 
Next ws 
+0

这里有很多的语法问题..首先修复。例如。 'J3'应该是'Range(“J3”)'或简单的'[J3]'。另外,你是在'J2'和'J3'还是'Formula'上分配一个值? – L42

回答

0

如果要更改单元格的值,则无法使用J3 = "6 Free Bottles"。 Excel认为J3是一个变量。使用Cells(3, "J").Value = "6 Free Bottles"。您也可以使用Range("J2").Value =Range(J2) =会尝试将Range对象设置为等于某个东西,这不会做你想要的。

将导致问题的单独注释:对于您的If语句之一,您还缺少End If

+0

啊,我明白了。我仍然在学习,但这是一个很大的帮助。谢谢!会让你知道我的工作。 – Skyjack