![为什么 Select Case 在该代码 VBA 中不起作用?](https://linux22.com/image/1595253/%E4%B8%BA%E4%BB%80%E4%B9%88%20Select%20Case%20%E5%9C%A8%E8%AF%A5%E4%BB%A3%E7%A0%81%20VBA%20%E4%B8%AD%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%9F.png)
我必须通过 Select Case 计算“Costo”(成本),但当我运行宏时,它只会将 0 放入 Cost 中,而不是我放入宏中的数字。缺少了一些东西,但我不知道是什么。成本(P5-P50)Pase 类型(O5-O50)
Excel 截图:
Sub Ejercicio1()
Dim Estacionamiento As String
Dim Casillero As String
Dim TipoDePase As String
Dim Costo As Double
Dim contador As Double
contador = 5
Range("P5").Select
Do While contador <= 50
Select Case TipoDePase
Case "Normal":
Costo = 95200
Case "Lounge":
Costo = 280000
Case "Lounge Premium":
Costo = 392000
End Select
ActiveCell.Offset(1, 0).Select
contador = contador + 1
Loop
End Sub
答案1
它不起作用,因为您的代码没有与工作簿交互。它没有从工作簿中获取任何变量,也没有向其返回任何内容。这应该可以工作,但我还没有测试过
Sub Ejercicio1()
Dim Estacionamiento As String
Dim Casillero As String
Dim TipoDePase As String
Dim Costo As Double
Dim contador As Double
contador = 5
Range("P5").Select
Do While contador <= 50
TipoDePase = ActiveCell.Offset(0, -1) 'Get value from column P
Select Case TipoDePase
Case "Normal":
ActiveCell = 95200
Case "Lounge":
ActiveCell = 280000
Case "Lounge Premium":
ActiveCell = 392000
End Select
ActiveCell.Offset(1, 0).Select
contador = contador + 1
Loop
End Sub
答案2
改变这个:
Do While contador <= 50
Select Case TipoDePase
更改为:
Do While contador <= 50
TipoDePase = Range("P" & contador).Value
Select Case TipoDePase
您也可以删除这两行,它们在您的代码中没有任何作用:
Range("P5").Select
ActiveCell.Offset(1, 0).Select