添加包含嵌套IF的公式到范围不起作用

时间:2017-09-18 14:39:15

标签: excel vba excel-vba

我在我的项目中需要将论坛添加到电子表格中的各个列(范围)中。第一个在单元格中获取值,并返回另一个值。这应该是整个列的相同值。

我尝试使用实际的公式,并且它与IF有问题。虽然这是在Excel中声明的方式并且工作正常,但当我尝试使用VBA添加它时,它会出现语句问题和=符号。

因此,我尝试了一种不同的方法,而不是整理出这个问题,因为它需要查找和比较的初始值是组合框的结果。在前面的步骤中,我将此值放在我从中获取初始值的列中。这是报告更新的月份。我想要实现的是上个月运行的价值。

例如,如果初始单元格的值是" 12月",则前一个月将是" 11月"这就是放在同一行内指定单元格中的结果。我也无法让这个工作。它没有错误,它只是通过它。

我愿意将下面的公式放在AN2到lRowB的范围内(这是另一个变量的最后一行) - 或 - 使用宏本身的变量(方法2)。无论什么是最有意义的,并且表现最好。这张表有超过20k条目。

公式为:

=IF([@[Current Update Month]]="January", "December", IF([@[Current Update Month]]="February", "January", IF([@[Current Update Month]]="March", "February", IF([@[Current Update Month]]="April", "March", IF([@[Current Update Month]]="May", "April", IF([@[Current Update Month]]="June", "May", IF([@[Current Update Month]]="July", "June", IF([@[Current Update Month]]="August", "July", IF([@[Current Update Month]]="September", "August", IF([@[Current Update Month]]="October", "September", IF([@[Current Update Month]]="November", "October", IF([@[Current Update Month]]="December", "November", 0))))))))))))

另一种方法是我上次在我的代码中使用的方法。

这是一个片段。

Dim lastMonthUpdate As String
Dim lastUpdateMonRng As Range

Set lastUpdateMonRng = sht.Range("AN2" & lRowB)
'LRowB is defined in a previous step. The value is this: lRowB = sht.Cells(Rows.Count, 1).End(xlUp).Row - it did not need restating

If ABCMatrixMonthSelect.ComboBox1.value = "January" Then
    lastMonthUpdate = "December"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "February" Then lastMonthUpdate = "January"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "March" Then lastMonthUpdate = "February"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "April" Then lastMonthUpdate = "March"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "May" Then lastMonthUpdate = "April"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "June" Then lastMonthUpdate = "May"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "July" Then lastMonthUpdate = "June"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "August" Then lastMonthUpdate = "July"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "September" Then lastMonthUpdate = "August"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "November" Then lastMonthUpdate = "September"
    ElseIf ABCMatrixMonthSelect.ComboBox1.value = "December" Then lastMonthUpdate = "November"

End If
With lastUpdateMonRng = lastMonthUpdate
End With

6 个答案:

答案 0 :(得分:1)

使用月份名称调用以下函数,它将返回前几个月的名称

Function GetPreviousMonth(ByVal sMonthName As String) As String
    GetPreviousMonth = MonthName(Month(DateAdd("m", -1, CDate("01/" & sMonthName & "/2000"))))
End Function

我已将其设置如下:
enter image description here

答案 1 :(得分:1)

=DATEVALUE("1-" & [@[Current Update Month]])
返回当前月份的名称(或名为当前更新月的列中保存的值)。

=DATEVALUE("1-January")
获取当前月份的名称并将其转换为实际日期 - 当前月份的第一个月份。这与编写公式=EOMONTH(DATEVALUE("1-" & [@[Current Update Month]]),-1)

相同

=EOMONTH("1/01/2017",-1)
返回上个月的最后一天。这与撰写=TEXT(EOMONTH(DATEVALUE("1-" & [@[Current Update Month]]),-1),"mmmm")相同。

最终公式:
=TEXT("31/12/2017","mmmm")
返回上个月的名称。这与撰写pyspark

相同

只需阅读VBA部分,在这种情况下我会使用@Zacs答案并直接在代码中使用计算,如果您不希望(在我看来)更简单的方法将其全部放入一个小功能。虽然我不会将年份添加到CDATE(将该年份默认为今年)。

答案 2 :(得分:0)

我会使用宏观方法。宏在处理多个条件语句方面要好得多。您可以通过事件调用宏,您可以使用宏在Excel中制作自定义公式,也可以在需要时手动调用宏。下面是如何在VBA中使用多个条件的主要部分。

在VBA中,每个If语句都需要以End If语句结束。不要创建多个If语句,而是使用Else If将多个条件放入同一个If语句中

    If ABCMatrixMonthSelect.ComboBox1.value = "January" Then 
        lastMonthUpdate = "December"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "February" Then lastMonthUpdate = "January"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "March" Then lastMonthUpdate = "February"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "April" Then lastMonthUpdate = "March"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "May" Then lastMonthUpdate = "April"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "June" Then lastMonthUpdate = "May"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "July" Then lastMonthUpdate = "June"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "August" Then lastMonthUpdate = "July"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "September" Then lastMonthUpdate = "August"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "November" Then lastMonthUpdate = "September"
        ElseIF ABCMatrixMonthSelect.ComboBox1.value = "December" Then lastMonthUpdate = "November"

    End If

答案 3 :(得分:0)

我找到一种必须使用组合框的方法,将月份名称作为"标签"和1到12之间的数字作为"值"。然后你可以得到上个月的#34;有点像琐事:

=If(@[Current Update Month]=1, 12, @[Current Update Month]-1)

虽然这给你1到12之间的数字;你可以有一个小的UDF暴露VBA.Strings.MonthName

Public Function MonthName(ByVal Index As Long) As String
    MonthName = VBA.Strings.MonthName(Index)
End Function

然后你可以将公式修改为:

=MonthName(If(@[Current Update Month]=1, 12, @[Current Update Month]-1))

或者,您可以使用查找表:

Current   |  Previous
==========|==========
January   |  December
February  |  January
March     |  February
April     |  March
May       |  April
June      |  May
July      |  June
August    |  July
September |  August
October   |  September
November  |  October
December  |  November

然后您可以使用简单的查找替换那个巨大的嵌套If公式。 "月指数减1"虽然方法让我觉得更有效率。

答案 4 :(得分:0)

要解决的一些问题:

1)您正在使用表引用[@ [Current Update Month]],因此列“AN2”必须在表的旁边才能使引用起作用。如果您使用此类型的参考,它将在任何地方工作 = if(表1 [当前更新月]。
2)您也在比较文本值,要将其放入VBA,您需要使用CHR(34)输入双引号。下面是一小段代码,用于将公式添加到引用表

的一系列单元格中
Let sht.Range("AN2" & lRowB).Formula = "=if([@[Current Update Month]]=" & Chr(34) & "January" & Chr(34) & "," & Chr(34) & "December" & Chr(34) & ", IF([@[Current Update Month]]=" & Chr(34) & "March" & Chr(34) & "," & Chr(34) & "February" & Chr(34) & ",IF([@[Current Update Month]]=" & Chr(34) & "July" & Chr(34) & "," & Chr(34) & "June" & Chr(34) & ")))"

答案 5 :(得分:0)

我更喜欢一种无宏的解决方案,易于理解:
1)添加一个包含公式的列,该公式将月份从文本转换为数字,例如“1月”到1:
=MONTH(DATEVALUE(C4&"1"))

2)添加另一个减去一个月的栏目:
=IF(D4=1,12,D4-1)

3)添加最后一列,将数字从数字翻译成文本,例如,2到“二月”: =TEXT(DATE(2010,E4,1);"mmmm")

这就是全部。对于20k行应该不是什么大问题。 One month back example

<强> EDITED
步骤1-3可以转换为sinlge线公式:
=TEXT(DATE(2010,IF(MONTH(DATEVALUE(C4&"1"))=1,12,MONTH(DATEVALUE(C4&"1"))-1),1),"mmmm")

相关问题