根据下面的代码,我希望这段代码能够检测出单词" Oranges"," Oranges1"," Oranges 2"等等,但我现在能做的只是检测橘子。当我想要检测Oranges1&这个词时,对我来说变得更加复杂。 Oranges2在不同的excel表中。我试着改变
If Cells(i,1).Value =" Oranges"然后到 If Cells(i,1).Value =" Oranges& Oranges1"然后
但这不起作用。一旦此代码可以检测到单元名称,我想执行计算。
这种情况对我有什么解决方案吗?
我的代码
Private Sub CommandButton1_Click()
' Get the last row with text
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Integer, Total As Double
Total = 0
' Use LastRow in loop
For i = 1 To LastRow
' Check if cell has text "Oranges"
If Cells(i, 1).Value = "Oranges " Then
Dim Val1 As Double
Dim Val2 As Double
Val1 = Worksheets("Sheet1").Cells(i, 2).Value
Val2 = Worksheets("Sheet2").Cells(i, 2).Value
' Add value in column B to total
Total = Val1 + Val2
Dim sum As Double
sum = Val1 + Val2
Worksheets("Interface").Cells(i, 3) = "Total"
Worksheets("Interface").Cells(i, 4).Value = sum
End If
Exit For
Next i
答案 0 :(得分:0)
您可以将If
替换为Select Case
,以便将来添加更多方案。
Select Case Cells(i, 1).Value
Case "Oranges", "Oranges1", "Oranges 2"
' the rest of your coding goes here
End Select