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

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

我尝试使用实际公式,并且与IF有关的问题。 虽然这是如何在Excel中声明和工作正常,当我尝试使用VBA添加它,它有一个问题与该语句和=标志。

所以,我不是在整理这个问题,而是尝试了一种不同的方法,因为它需要查找和比较的初始值是combobox的结果。 在之前的一个步骤中,我已经把这个值放在了我从中获取初始值的列中。 这是报告正在更新的月份。 我试图达到的是上个月运行的价值。

例如,如果初始单元格的值是“十二月”,则前一个月将是“十一月”,这是将被放置到同一行内的指定单元格中的结果。 我也无法得到这个工作。 它没有错误,它只是通过它。

我打算把下面的公式放在AN2到lRowB的范围内(这是另一个variables的最后一行) – 或者 – 在macros本身使用一个variables(方法2)。 无论什么最有意义,performance最好。 这张表有超过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 

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

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

我已经把它设置如下:
在这里输入图像说明

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

=DATEVALUE("1-" & [@[Current Update Month]])
取当前月份的名称,并把它变成一个真正的date – 本月的第一个月份。 这与编写公式=DATEVALUE("1-January")

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

最终公式:
=TEXT(EOMONTH(DATEVALUE("1-" & [@[Current Update Month]]),-1),"mmmm")
返回上个月的名称。 这与writing =TEXT("31/12/2017","mmmm")

只要阅读VBA部分,在这种情况下,我会使用@Zacs的答案,并直接在代码中使用计算,如果你不希望(在我看来)更简单的方法把所有的一个小函数。 虽然我不会把这一年添加到CDATE(这一年的默认设置保持不变)。

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

在VBA中,每个If语句都需要以End If语句End If 。 而不是创build多个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 

我会find一种方法,不得不将combobox的月份名称作为“标签”和1到12之间的数字作为“值”。 那么你可以得到“上个月”,像下面这样微不足道的东西:

 =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 

然后,你可以用一个简单的查找replace那个巨大的嵌套的If公式。 尽pipe“月份指数减一”的方法让我感觉更有效率。

这里有几件事要解决:

1)您正在使用表格引用[@ [当前更新月份]],所以列表“AN2”必须位于表格的旁边以供参考。 如果你使用这种types的引用,它将工作在任何地方= if(表1 [当前更新月份])。
2)你也在比较文本值,把它放到VBA中,你将需要使用CHR(34)来input双引号。 这里有一小段代码可以将一个公式添加到引用一个表格的一系列单元格中

 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) & ")))" 

我更喜欢一个无macros而易于使用的解决scheme:
1)添加一个公式的列,从文本到数字,例如“1月”到1:
=MONTH(DATEVALUE(C4&"1"))

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

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

就这样。 对于20万行不应该是一个大问题。 一个月后面的例子

编辑
步骤1-3可以转换成单线公式:
=TEXT(DATE(2010,IF(MONTH(DATEVALUE(C4&"1"))=1,12,MONTH(DATEVALUE(C4&"1"))-1),1),"mmmm")