我需要在 VBA 中撰寫如下代碼描述

查看圖片或表格,首先我要檢查單元格(Ax)中包含的值,其中 x 是下一個空行(在我的示例中 x=8)。如果此單元格(A8)的值等于“M”,我想在包含最大值的列(B)中找到最大值,并在單元格(B8)中寫下以下下一個值:在這種情況下為“8 ”。然后在 Column(C) 中寫入值“M8”。相反,如果等于“F”,則單元格(B8)中的值將是“10”,而在單元格(C8)中我想得到“F10)。
PS 新行中的值“M”或“F”來自文本框,如以下代碼所示。
這是我到目前為止寫的代碼:
Private Sub CommandButton2_Click()
Dim Lastrow As Long
Lastrow = Sheets("Database finale").Range("A1").End(xlDown).Row 1
Sheets("Database finale").Select
Cells(Lastrow, 1).Value = TextBox1.Value
If Cells(Lastrow, 1).Value = "M" Then
Cells(UltimaRiga, 2).Value = WorksheetFunction.Max(Range("B1:B"))
Else
Cells(UltimaRiga, 2).Value = WorksheetFunction.Max(Range("B1:B"))
End If
End Sub
這是我到目前為止寫的代碼,但我不知道如何修復雙重條件(等于 M 或等于 F)并將之前的最大值加 1 并在 Cells(Lastrow, 2) 中報告。
你能幫我寫代碼嗎?
uj5u.com熱心網友回復:
您可以使用該maxifs函式來檢查M或的最大值F:
Dim Lastrow As Long
Lastrow = Sheets("Database finale").Range("A1").End(xlDown).Row 1
Sheets("Database finale").Select
Cells(Lastrow, 1).Value = Worksheets("Database finale").TextBox1.Value
If Cells(Lastrow, 1).Value = "M" Then
'Setting the maximum number for M using MaxIfs
Cells(Lastrow, 2).Value = WorksheetFunction.MaxIfs(Range(Cells(1, 2), Cells(Lastrow - 1, 2)), Range(Cells(1, 1), Cells(Lastrow - 1, 1)), "M") 1
Else
'Setting the maximum number for F using MaxIfs
Cells(Lastrow, 2).Value = WorksheetFunction.MaxIfs(Range(Cells(1, 2), Cells(Lastrow - 1, 2)), Range(Cells(1, 1), Cells(Lastrow - 1, 1)), "F") 1
End If
'Setting the concatenated string in the third column
Cells(Lastrow, 3).Value = Cells(Lastrow, 1).Value & Cells(Lastrow, 2).Value
請注意,我在作業表內創建了一個 TextBox。您可能需要根據您的使用方式對其進行修改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/405389.html
標籤:
上一篇:在兩列的基礎上找到最大連續重復值
下一篇:運行n行的計算函式
