gpt4 book ai didi

excel - 如何在 VBA 中使用多个 case 语句

转载 作者:行者123 更新时间:2023-12-04 21:50:49 25 4
gpt4 key购买 nike

我有两列,可以说 A 和 B。在 A 列中,我有 Apple、banana、Brinjal 等值,在 B 列中有成熟和未成熟的值。在 C 列中,我想检查它是水果还是蔬菜,然后成熟或未成熟。我想要下面的结果。

如何使用多个 Case 语句?

Please see the below table

Private Sub CommandButton1_Click()
Dim category As String, result As String
For i = 2 To 1000
category = Range("A" & i).Value
Select Case category
Case "Apple"
result = "Fruit"
Case "Brinjal"
result = "Vegetable"
End Select
Range("C" & i).Value = result
Next
End Sub

最佳答案

您可以使用逗号指定值列表:

Private Sub CommandButton1_Click()
Dim category As String, result As String
For i = 2 To 1000
category = Range("A" & i).Value
Select Case category
Case "Apple", "Banana", "Orange"
result = "Fruit"
Case "Brinjal"
result = "Vegetable"
Case else
result = vbnullstring
End Select
Range("C" & i).Value = Range("B" & i).Value & " " & result
Next i
End Sub

关于excel - 如何在 VBA 中使用多个 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54385626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com