Private Sub CommandButton1_Click()
Dim Year As Long
Dim i As Long
Year = 2008
For Year = 2008 To ComboBox2.Value
For i = 2 To 200 Step 12
If ComboBox1.Value = "Januari" Then
Range("G15").Value = Sheets(3).Cells(i, 1).Value
ElseIf ComboBox1.Value = "Februari" Then
Range("G15").Value = Sheets(3).Cells(i + 1, 1).Value
ElseIf ComboBox1.Value = "Maret" Then
Range("G15").Value = Sheets(3).Cells(i + 2, 1).Value
ElseIf ComboBox1.Value = "April" Then
Range("G15").Value = Sheets(3).Cells(i + 3, 1).Value
ElseIf ComboBox1.Value = "Mei" Then
Range("G15").Value = Sheets(3).Cells(i + 4, 1).Value
ElseIf ComboBox1.Value = "Juni" Then
Range("G15").Value = Sheets(3).Cells(i + 5, 1).Value
ElseIf ComboBox1.Value = "Juli" Then
Range("G15").Value = Sheets(3).Cells(i + 6, 1).Value
ElseIf ComboBox1.Value = "Agustus" Then
Range("G15").Value = Sheets(3).Cells(i + 7, 1).Value
ElseIf ComboBox1.Value = "September" Then
Range("G15").Value = Sheets(3).Cells(i + 8, 1).Value
ElseIf ComboBox1.Value = "Oktober" Then
Range("G15").Value = Sheets(3).Cells(i + 9, 1).Value
ElseIf ComboBox1.Value = "November" Then
Range("G15").Value = Sheets(3).Cells(i + 10, 1).Value
ElseIf ComboBox1.Value = "Desember" Then
Range("G15").Value = Sheets(3).Cells(i + 12, 1).Value
End If
Next i
Next Year
Sheets(2).Range("I5").Value = ("CONTRACT SPOT")
End Sub
我想要它當我選擇januari 2009組合它顯示1,我選擇januari 2010組合它顯示13等等。請幫忙當語句符合條件時如何停止循環? -vba-excel
到目前爲止它只顯示循環結束時的最後一個數字,例如,當我選擇januari 2009時顯示193 – Yomi
combobox2.value範圍是2008至2050 – Yomi
如果您想要退出For'循環時條件得到滿足,然後放入'If'語句的'Exit For'中。如果你解釋得更好,也許有一種方法來優化你的代碼 –