반응형
Excel VBA의 동적 함수 호출
기능을 동적으로 호출하는 방법이 없을까 해서요.예를들면.
Sub foo1()
Debug.Print "in foo1"
End Sub
Sub foo2()
Debug.Print "in foo2"
End Sub
다음과 같은 작업을 수행할 수 있는 방법이 있습니까?
Sub callSomeFoo(i as Integer)
Call foo&i
End Sub
아니면 이와 같은 것이 필요한가요?
Sub callSomeFoo(i as Integer)
Select Case i
Case 1
Call foo1
Case Else
Call foo2
End Select
End Sub
시급한 문제가 아니라...단지 호기심이 많은기능적으로 현명하게 할 수 있는 다른 창의적인 것들도 환영합니다.
감사합니다!
edit1: 제가 가지고 있는 코드와 아래에 나열된 오류는 다음과 같습니다.
Sub foo1()
Debug.Print "in foo1"
End Sub
Sub foo2()
Debug.Print "in foo2"
End Sub
Sub callSomeFoo()
Dim i%
'using a cell on the worksheet to determine the function. Happens to be "1"
i = Sheets("Sheet1").Range("A1").Value
'Line below works
Call foo1
'Line below gives me an error
Application.Run "foo"&i
End Sub
오류:
런타임 오류 '1004' 매크로 'foo1'을(를) 실행할 수 없습니다.이 워크북에서 매크로를 사용할 수 없거나 일부 매크로를 사용할 수 없습니다.
실행 방법을 원하시는군요!
Sub callSomeFoo(i as Integer)
Application.Run "foo" & i
End Sub
근데 그게 안 돼요. VBA는 이름을 안 좋아해요.foo1
그래서 안 될 겁니다
FOO1도 셀 레퍼런스가 될 수 있기 때문입니다.Application의 첫 번째 arg입니다.실행은 Range 객체가 될 수 있으므로 FOO1을 평가하고, 그것이 셀이라고 생각하며, 그 셀이 비어 있으므로 무엇을 해야 할지 모르겠습니다. – Dick Kusleika
이는 더 좋은 메소드 이름을 선택하면 쉽게 해결할 수 있습니다.
테스트된 작업 예시
Option Explicit
Public Sub TestDynamic1()
Debug.Print "TestDynamic 1"
End Sub
Sub TestDynamic2()
Debug.Print "TestDynamic 2"
End Sub
Private Sub TestDynamic3()
Debug.Print "TestDynamic 3"
End Sub
Sub callTestDynamic(i As Integer)
On Error GoTo DynamicCallError
Application.Run "TestDynamic" & i
Exit Sub
DynamicCallError:
Debug.Print "Failed dynamic call: " & Err.Description
End Sub
Public Sub TestMe()
callTestDynamic 1
callTestDynamic 2
callTestDynamic 3
callTestDynamic 4
End Sub
언급URL : https://stackoverflow.com/questions/19845944/dynamic-function-calls-in-excel-vba
반응형
'programing' 카테고리의 다른 글
iTemes Security Pro Plugin 사용 시 Wordpress API에서 정보를 가져오는 방법 (0) | 2023.10.01 |
---|---|
AWS KMS 플러그인을 통한 정지 상태의 MariaDB Encryption - Key Rotation이 작동하지 않습니까? (0) | 2023.10.01 |
e에서 2조 자리까지 계산하는 가장 빠른 방법은 무엇입니까? (0) | 2023.10.01 |
jQuery를 사용하여 "삭제" 키 누르기 (0) | 2023.10.01 |
도커 이미지 컨텐츠를 보는 방법 (0) | 2023.10.01 |