문제 설명
SpinButton SpinUp 또는 SpinDown 새로 고침 (Refreshing SpinButton SpinUp or SpinDown)
내 Excel 스프레드시트에 스핀 버튼을 추가하여 스핀 버튼을 위로 클릭하면 셀 값이 0.01 증가하고 스핀 버튼을 아래로 클릭하면 셀 값이 .01 감소하도록 스핀 버튼을 추가하려고 합니다. 다음 코드가 있습니다.
Private Sub SpinButton1_SpinUp()
Dim i As Integer
i = Sheet1.Range("E8").Value
Sheet1.Range("E8") = i + 0.01
End Sub
이 코드의 기능을 테스트할 때 E8 셀은 0.01 위로 올라가지만 한 번만 올라갑니다. 버튼을 다시 클릭해도 아무 일도 일어나지 않습니다. 이것을 어떻게 고칠 수 있습니까?
고마워요!
참조 솔루션
방법 1:
An integer will not store the decimal value. So if you have 8 in cell E8 you are getting 8 in the int and then writing 8 + .01 to cell E8 so you get 8.01.
The next time you read 8.01 into the int, VB is rounding off the decimals so you are getting 8 in the int again and adding the same value back in.
You need to change the variable to decimal variable.
Taking out the variable declaration will just make VB guess what to use and that is not a good practice. You should use "Option Explicit" at the top of your code to prevent this.
Option Explicit
Private Sub SpinButton1_SpinUp()
Dim i As Double
i = Sheet1.Range("E8").Value
Sheet1.Range("E8") = i + 0.01
End Sub
You could also use
Dim i As Single
depending on the size of the decimal that it could get to.
방법 2:
You don't need to store the value in a variable. Try this.
Private Sub SpinButton1_SpinUp()
Sheet1.Range("E8").Value = Sheet1.Range("E8").Value + 0.01
End Sub
Private Sub SpinButton2_SpinDown()
Sheet1.Range("E8").Value = Sheet1.Range("E8").Value ‑ 0.01
End Sub
방법 3:
change Integer to Double
Private Sub SpinButton1_SpinUp()
Dim i As Double
i = Sheet1.Range("E8").Value
Sheet1.Range("E8") = i + 0.01
End Sub
(by Chris2015、MatthewD、Linga、Ebrahim Poursadeqi)