我是我的項目中的一個點,我需要將FORUMula添加到電子表格中的各個列(範圍)。第一個在單元格中獲取一個值,並返回另一個值。這應該是整個列的相同值。將包含嵌套IF的公式添加到範圍不起作用
我嘗試使用實際的公式,它與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
我是在總結你的問題這樣解決:我需要一個公式,將取代一個月的名稱與前一個月? –
這不是替代。它查看包含更新月份的單元格中的值,該值也是ABCMatrixSelect.ComboBox1.value的結果,並將前一個月的名稱放在AN列中。它不會替換原始內容,它會查看一個單元格值並確定值。或者..它可以使用ABCMatrixSelect.Combox1.value本身。這兩種方法都會得到相同的結果。 – SharePoint0508
對不起,有點不清楚。我的意思正是你說的。但是你需要這個在VBA宏中?像查找/索引+匹配這樣的感覺可以做到這一點,而不必去VBA。 –