SpinButton SpinUp 또는 SpinDown 새로 고침 (Refreshing SpinButton SpinUp or SpinDown)


문제 설명

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 Chris2015MatthewDLingaEbrahim Poursadeqi)

참조 문서

  1. Refreshing SpinButton SpinUp or SpinDown (CC BY‑SA 2.5/3.0/4.0)

#vba #excel






관련 질문

열에 문자열이 포함된 경우 열 번호 나열 (List column numbers if columns contain string)

SpinButton SpinUp 또는 SpinDown 새로 고침 (Refreshing SpinButton SpinUp or SpinDown)

변수를 사용하여 Excel VBA에서 3차원 배열 요소에 액세스 (Use variables to access 3 dimensional array elements in Excel VBA)

TODAY() 함수와 월 1일을 기준으로 범위 이동 (Moving a range based on TODAY() function and first of the month)

ALV 레이아웃의 표시된 모든 열을 제거하는 방법 (How to remove all displayed columns of ALV layout)

셀에 입력한 msoShapeOval은 MsoShapeType으로 선언된 변수의 값이 될 수 없습니다. (msoShapeOval entered in cell not allowed to be value for variable declared as MsoShapeType)

Excel VBA의 세로 열에 여러 json을 생성하는 방법은 무엇입니까? (How to generate multiple json in vertical column in Excel VBA?)

공개 함수의 VBA 런타임 오류 -2147319767(80028029) (VBA Run-time error -2147319767 (80028029) in Public Function)

알 수 없는 수의 문자를 패턴 일치시키는 방법 (How to Pattern Match an Unknown Number of Characters)

Excel VBA 파일이 열려 있는지 확인 기능 (Excel VBA Check if File is open function)

ActiveCell..Offset(-1,0)=을 사용하는 If/And/Then/Else 문의 VBA 문제= (VBA Issue with If/And/Then/Else statement using ActiveCell..Offset(-1,0)=)

VBA 런타임 오류 '1004': 응용 프로그램 정의 또는 개체 정의 오류" 설정 셀 내용 (VBA Runtime Error '1004': Application-defined or Object-defined error" setting cell contents)







코멘트