Excel VBAã«ãããç¬èªã€ãã³ãã®èšèšãšå®è£ ãã¯ããã¯
ç§ã¯VBAã®æŽ»çšçµéšãéããŠåŸãç¥èãæŽçããå ±æããç®çã§èšäºãäœæããŠããããã°ã©ãã³ã°æŽ1幎åã«ãªããšã³ãžãã¢ã§ããååã¯ã芪ãã©ã«ããã¹ååŸã®å®è£ ãã¯ããã¯ã«ã€ããŠè©³ãã説æããŸãããä»åã¯ãExcel VBAã«ãããç¬èªã€ãã³ãïŒèªäœã®éç¥æ©èœïŒã®äœæãšæŽ»çšæ¹æ³ã«ã€ããŠè§£èª¬ããŸãã
- 第1å: Excel VBAã®åºç€ç¥èãšã»ãã¥ãªãã£èšå®
- 第2å: Excel VBAã®åºæ¬æäœãšãªããžã§ã¯ãã®çè§£
- 第3å: Excel VBAã«ããã倿°ãšå®æ°ã®åºæ¬
- 第4å: Excel VBAã«ãããã·ãŒãæäœã®åºæ¬ãšãšã©ãŒåŠç
- 第5å: Excel VBAã«ãããæ¡ä»¶åå²
- 第6å: Excel VBAã«ãããç¹°ãè¿ãåŠçã®åºæ¬
- 第7å: Excel VBAã«ãããé åãšFor Eachã®æŽ»çš
- 第8å: Excel VBAã«ãããFormulaãšValueã®äœ¿ãåããšãŠãŒã¶ãŒå ¥åã®ååŸ
- 第9å: Excel VBAã«ããããã¡ã€ã«æäœãšãã©ã«ã管çã®åºæ¬
- 第10å: Excel VBAã«ãããFileSystemObjectãæŽ»çšããé«åºŠãªãã¡ã€ã«æäœ
- 第11å: Excel VBAã«ãããFileSystemObjectãæŽ»çšããé«åºŠãªãã¡ã€ã«æäœ å¿çšç·š
- 第12å: Excel VBAã«ãããStrConv颿°ã®æŽ»çšãšå¿çšãã¯ããã¯
- 第13å: Excel VBAã«ãããã¯ãŒã¯ããã¯ã®å®å šãªæäœãšç®¡çãã¯ããã¯
- 第14å: Excel VBAã«ãããFunctionïŒé¢æ°ïŒã®äœæãšæŽ»çšãã¯ããã¯
- 第15å: Excel VBAã«ãããé åãè¿ã颿°ã®äœæãšæŽ»çšãã¯ããã¯
- 第16å: Excel VBAã«ãããã³ã¬ã¯ã·ã§ã³ã®æŽ»çšãšå¿çšãã¯ããã¯
- 第17å: Excel VBAã«ãããèŸæžåïŒDictionaryïŒã®æŽ»çšãšå¿çšãã¯ããã¯
- 第18å: Excel VBAã«ãããEnumåãæŽ»çšãã颿°èšèšãšå®è£ ãã¯ããã¯
- 第19å: Excel VBAã«ããããŠãŒãã£ãªãã£é¢æ°ã®äœæãšæŽ»çšãã¯ããã¯
- 第20å: Excel VBAã«ãããæ£èŠè¡šçŸã掻çšãããã¡ã€ã«åè§£æãã¯ããã¯
- 第21å: Excel VBAã§ç¯å²å ã®å³åœ¢ãå¹ççã«åé€ãããã¯ããã¯
- 第22å: Excel VBAã§ææ°ãã¡ã€ã«ãå¹ççã«æ€çŽ¢ãã颿°èšèšãã¯ããã¯
- 第23å: Excel VBAã§éžæããç¯å²ã«å¯ŸããŠãäžè¡ããã«ç©ºè¡ãæ¿å ¥ãããã¯ããã¯
- 第24å: Excel VBAã§å¯èŠã»ã«ã掻çšãããã£ã«ã¿ãŒæäœãã¯ããã¯
- 第25å: Excel VBAã§å¯èŠã»ã«ã®ã¿ãå¹ççã«ã³ããŒãããã¯ããã¯
- 第26å: Excel VBAã«ããããã¡ã€ã«ã»ãã©ã«ãç§»åã®ååž°åŠçãã¯ããã¯
- 第27å: Excel VBAã«ããã芪ãã©ã«ããã¹ååŸã®å®è£ ãã¯ããã¯
- 第28å: Excel VBAã«ãããç¬èªã€ãã³ãã®èšèšãšå®è£ ãã¯ããã¯ïŒæ¬èšäºïŒ
- 第29å: Excel VBAã«ãããEnumåãæŽ»çšããã¡ã³ããã³ã¹æ§åäžãã¯ããã¯
- 第30å: Excel VBAã«ãããåçªå·ããã¢ã«ãã¡ããã倿ã®å¹ççå®è£ ãã¯ããã¯
- 第31å: Excel VBAã«ãããéè€ã®ãªãã·ãŒãåçæã®å¹ççå®è£ ãã¯ããã¯
ç®æ¬¡
- ã¯ããã«
- ç¬èªã€ãã³ãã®åºæ¬æŠå¿µ
- ã€ãã³ãçºçã®ä»çµã¿ãšå©ç¹
- WithEventsã䜿ã£ãã€ãã³ãåä¿¡ã®åºæ¬
- å®è·µçãªç¬èªã€ãã³ãã®å¿çšäŸ
- ãšã©ãŒãã³ããªã³ã°ãšãã¹ããã©ã¯ãã£ã¹
- ãŸãšã
ã¯ããã«
Excel VBAã«ãããç¬èªã€ãã³ãã¯ãããã°ã©ã ã®èšèšã倧ããæ¹åãã匷åãªä»çµã¿ã§ããéåžžã®VBAã§ã¯ãããã«ã¯ãªãã¯ãå³ã¯ãªãã¯ãšãã£ãExcelãæäŸããæšæºã€ãã³ãã䜿ããŸãããç¬èªã€ãã³ããæŽ»çšããããšã§ãããæè»ã§ä¿å®ããããããã°ã©ã ãäœæã§ããŸãã
åŸæ¥ã®æç¶ãåããã°ã©ãã³ã°ïŒé 次å®è¡å
: äžããäžã«åŠçãå®è¡ããæ¹åŒïŒã§ã¯ãåŠçã®å®äºãç¶æ
å€åãç¥ãããéã«è€éãªä»çµã¿ãå¿
èŠã§ãããç¬èªã€ãã³ãã䜿çšããããšã§ãççµåãªèšèšïŒäœçµå
: åéšåã®äŸåé¢ä¿ãå°ãªãèšèšïŒãå®çŸããã¡ã³ããã³ã¹æ§ãšæ¡åŒµæ§ã«åªããã·ã¹ãã ãæ§ç¯ã§ããŸãã
ç¬èªã€ãã³ãã®åºæ¬æŠå¿µ
ã€ãã³ãé§åããã°ã©ãã³ã°ãšã¯
ã€ãã³ãé§åããã°ã©ãã³ã°ãšã¯ãããã°ã©ã ã®å®è¡ãç¹å®ã®ã€ãã³ãïŒãŠãŒã¶ãŒæäœãç¶æ å€åãªã©ïŒã®çºçã«ãã£ãŠå¶åŸ¡ãããèšèšææ³ã§ãã
ããã°ã©ãã³ã°ã¹ã¿ã€ã« | å®è¡æ¹åŒ | å ·äœäŸ |
---|---|---|
æç¶ãå | äžããäžã«é 次å®è¡ | èšç®åŠçãããŒã¿å€æ |
ã€ãã³ãé§åå | ã€ãã³ãçºçæã«å¯Ÿå¿åŠçãå®è¡ | ãã¿ã³ã¯ãªãã¯ãããŒã¿æŽæ°éç¥ |
ã€ãã³ãïŒEventïŒãšã¯
ã€ãã³ãã¯ãããã°ã©ã å®è¡äžã«çºçããç¹å®ã®åºæ¥äºãç¶æ å€åã衚ããŸããExcelã§ã¯ãã»ã«ã®å€å€æŽãã¯ãŒã¯ã·ãŒãã®éžæããã¡ã€ã«ã®ä¿åãªã©ãã€ãã³ããšããŠå®çŸ©ãããŠããŸããç¬èªã€ãã³ããäœæããããšã§ãèªåã®ã¯ã©ã¹åºæã®åºæ¥äºãå®çŸ©ããä»ã®éšåã«éç¥ã§ããŸãã
ç¬èªã€ãã³ãã解決ããåé¡
åŸæ¥ã®æç¶ãåããã°ã©ãã³ã°ã§ã¯ã以äžã®ãããªåé¡ãçºçããã¡ã§ããã
- çµå床ã®é«ã: äžã€ã®åŠçãä»ã®åŠçã«åŒ·ãäŸåããŠãã
- åå©çšæ§ã®äœã: ç¹å®ã®åŠçã«ã®ã¿å¯Ÿå¿ããã³ãŒãã«ãªããã¡
- æ¡åŒµæ§ã®å°é£: æ°ããåŠçã远å ããéã«æ¢åã³ãŒãã®å€§å¹ ãªä¿®æ£ãå¿ èŠ
ç¬èªã€ãã³ããæŽ»çšããããšã§ããããã®åé¡ã以äžã®ããã«æ¹åã§ããŸãã
- ççµåãªèšèš: ååŠçãç¬ç«ããŠåäœããã€ãã³ããéããŠé£æº
- é«ãåå©çšæ§: æ±çšçãªã¯ã©ã¹ãšããŠæ§ã ãªãããžã§ã¯ãã§æŽ»çšå¯èœ
- åªããæ¡åŒµæ§: æ°ããã€ãã³ããã³ãã©ãŒãåŸããç°¡åã«è¿œå å¯èœ
ã¯ã©ã¹ã®ç°¡åãªèª¬æ
VBAã«ãããã¯ã©ã¹ãšã¯
ç¬èªã€ãã³ããçè§£ããããã«ããŸãVBAã«ãããã¯ã©ã¹ã®åºæ¬ãçè§£ããŠããå¿ èŠããããŸããã¯ã©ã¹ãšã¯ãããŒã¿ãšåŠçãã²ãšãŸãšãã«ããèšèšå³ã®ãããªãã®ã§ãã
ã¯ã©ã¹ã䜿çšããããšã§ãé¢é£ãã倿°ãšåŠçãäžã€ã®åäœãšããŠç®¡çã§ããããã«ãªããŸãã
æšæºã¢ãžã¥ãŒã«ãšã®éã
é ç® | æšæºã¢ãžã¥ãŒã« | ã¯ã©ã¹ã¢ãžã¥ãŒã« |
---|---|---|
çšé | åçºã®åŠçã颿° | ããŒã¿ãšåŠçãã»ããã§ç®¡ç |
ã€ã³ã¹ã¿ã³ã¹ | äœæäžå¯ | è€æ°ã®ã€ã³ã¹ã¿ã³ã¹ãäœæå¯èœ |
ããŒã¿ä¿æ | ã¢ãžã¥ãŒã«å šäœã§å ±æ | ã€ã³ã¹ã¿ã³ã¹ããšã«ç¬ç« |
ã€ãã³ã | çºçã»åä¿¡äžå¯ | ç¬èªã€ãã³ããå®çŸ©ã»çºçå¯èœ |
ã€ã³ã¹ã¿ã³ã¹ãšã¯
ã€ã³ã¹ã¿ã³ã¹ãšã¯ãã¯ã©ã¹ãšããèšèšå³ããäœãããå®éã®è£œåã®ããšã§ããäŸãã°ããè»ããšããã¯ã©ã¹ãããã°ãããããäœããããèµ€ãè»ããéãè»ããããããç°ãªãã€ã³ã¹ã¿ã³ã¹ã«ãªããŸããNew
ããŒã¯ãŒãã䜿ã£ãŠã€ã³ã¹ã¿ã³ã¹ãäœæããŸãã
ã¯ã©ã¹ã䜿ãã¡ãªãã
ã¯ã©ã¹ã䜿çšããããšã§ã以äžã®ãããªå©ç¹ãåŸãããŸãã
1. ããŒã¿ã®ä¿è·
ã¯ã©ã¹å
ã®å€æ°ã¯å€éšããçŽæ¥ã¢ã¯ã»ã¹ã§ããªããããæå³ããªã倿Žãé²ããŸãã
2. ã³ãŒãã®æŽç
é¢é£ããåŠçããŸãšããŠç®¡çã§ãããããããã°ã©ã å
šäœã®æ§é ãåããããããªããŸãã
3. åå©çšæ§ã®åäž
äžåºŠäœæããã¯ã©ã¹ã¯ãä»ã®ãããžã§ã¯ãã§ãç°¡åã«å©çšã§ããŸãã
ç°¡åãªã¯ã©ã¹ã®äŸ
ç¬èªã€ãã³ãã®èª¬æã«å ¥ãåã«ãã·ã³ãã«ãªã¯ã©ã¹ã®äŸãèŠãŠã¿ãŸãããã
' ã¯ã©ã¹ã¢ãžã¥ãŒã«å: SimpleCalculator
' èšç®æ©ã¯ã©ã¹å
ã§äœ¿çšãã倿°ïŒå€éšããã¯çŽæ¥ã¢ã¯ã»ã¹äžå¯ïŒ
Private currentValue As Double ' çŸåšã®èšç®çµæãä¿æãã倿°
' çŸåšã®å€ãååŸããã¡ãœããïŒå€éšããã¢ã¯ã»ã¹å¯èœïŒ
Public Function GetCurrentValue() As Double
GetCurrentValue = currentValue ' ä¿æããŠããå€ãè¿ã
End Function
' å€ãå ç®ããã¡ãœãã
Public Sub AddValue(addNum As Double)
currentValue = currentValue + addNum ' çŸåšã®å€ã«åŒæ°ã®å€ãå ç®
End Sub
' èšç®æ©ããªã»ããããã¡ãœãã
Public Sub Reset()
currentValue = 0 ' çŸåšã®å€ã0ã«ãªã»ãã
End Sub
ãã®äŸã§ã¯ãcurrentValue
ãšãã倿°ãšããããæäœãã3ã€ã®ã¡ãœãããã¯ã©ã¹å
ã«ãŸãšããŠããŸãã
ã¯ã©ã¹ã®äœ¿çšäŸ
' æšæºã¢ãžã¥ãŒã«
' èšç®æ©ã¯ã©ã¹ã®äœ¿çšäŸãå®è¡ããããã·ãŒãžã£
Public Sub TestCalculator()
' èšç®æ©ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæ
Dim calc As SimpleCalculator
Set calc = New SimpleCalculator ' æ°ããèšç®æ©ãäœæ
' èšç®ã®å®è¡
calc.AddValue 10 ' 10ãå ç®
calc.AddValue 5 ' ããã«5ãå ç®
' çµæã®è¡šç€º
MsgBox "èšç®çµæ: " & calc.GetCurrentValue() ' çµæã¯15ã«ãªã
' èšç®æ©ã®ãªã»ãã
calc.Reset ' å€ã0ã«ãªã»ãã
MsgBox "ãªã»ããåŸ: " & calc.GetCurrentValue() ' çµæã¯0ã«ãªã
End Sub
ãã®ããã«ãã¯ã©ã¹ã䜿çšããããšã§ãããŒã¿ïŒcurrentValue
ïŒãšåŠçïŒåã¡ãœããïŒãäžã€ã®åäœãšããŠç®¡çã§ããŸãããããŠãç¬èªã€ãã³ãã¯ããã®ã¯ã©ã¹ã®ä»çµã¿ãåºç€ãšããŠåäœããŸãã
次ã®ç« ã§ã¯ããã®ã¯ã©ã¹ã®ä»çµã¿ãå©çšããŠãã©ã®ããã«ç¬èªã€ãã³ããå®çŸãããã詳ãã解説ããŠãããŸãã
ã€ãã³ãçºçã®ä»çµã¿ãšå©ç¹
ã€ãã³ãã·ã¹ãã ã®æ§æèŠçŽ
ç¬èªã€ãã³ãã·ã¹ãã ã¯ã以äžã®3ã€ã®äž»èŠã³ã³ããŒãã³ãã§æ§æãããŸãã
- ã€ãã³ãçºçæºïŒã€ãã³ããå®çŸ©ãçºçãããã¯ã©ã¹ïŒ
- ã€ãã³ãå®çŸ©ïŒçºçããåºæ¥äºã®ä»æ§ïŒ
- ã€ãã³ããã³ãã©ãŒïŒã€ãã³ãçºçæã«å®è¡ãããåŠçïŒ
[ã€ãã³ãçºçæº] â [ã€ãã³ãçºç] â [ã€ãã³ããã³ãã©ãŒå®è¡]
â â â
åŠçå®è¡ RaiseEvent 察å¿åŠçå®è¡
åºæ¬çãªã€ãã³ãå®çŸ©ã®äŸ
ãŸããã·ã³ãã«ãªç¬èªã€ãã³ãã®å®çŸ©æ¹æ³ãèŠãŠã¿ãŸãããã
' ã¯ã©ã¹ã¢ãžã¥ãŒã«å: BasicEventClass
' åŠçå®äºæã«çºçããç¬èªã€ãã³ããå®çŸ©
Public Event TaskCompleted(taskName As String)
' äœããã®äœæ¥ãå®è¡ããå®äºæã«ã€ãã³ããçºçãããã¡ãœãã
Public Sub ExecuteTask()
On Error GoTo ErrorHandler
' äœæ¥ã®å®è¡ãã·ãã¥ã¬ãŒãïŒå®éã®åŠçã§ã¯å
·äœçãªæ¥ååŠçãèšè¿°ïŒ
Application.Wait Now + TimeValue("00:00:02")
' äœæ¥å®äºãã€ãã³ãã§éç¥
RaiseEvent TaskCompleted("ããŒã¿åŠçã¿ã¹ã¯")
Exit Sub
ErrorHandler:
MsgBox "ã¿ã¹ã¯å®è¡äžã«ãšã©ãŒãçºçããŸãã: " & Err.Description, vbCritical
Err.Clear
End Sub
ã³ãŒã解説
1. ã€ãã³ãå®çŸ©éšå
Public Event TaskCompleted(taskName As String)
-
Public Event
ããŒã¯ãŒãã§ç¬èªã€ãã³ããå®£èš -
TaskCompleted
ã¯ä»»æã®ã€ãã³ãåïŒããããããååãä»ããããšãéèŠïŒ -
taskName As String
ã¯ãã€ãã³ãçºçæã«æž¡ãæ å ±
2. ã€ãã³ãçºçéšå
RaiseEvent TaskCompleted("ããŒã¿åŠçã¿ã¹ã¯")
-
RaiseEvent
ããŒã¯ãŒãã§ã€ãã³ããå®éã«çºçããã - å®çŸ©ããã€ãã³ãåãšå¿ èŠãªåŒæ°ãæå®
ã€ãã³ãäœ¿çšæã®å©ç¹
ç¬èªã€ãã³ãã䜿çšããããšã§ã以äžã®ãããªå©ç¹ãåŸãããŸãã
- åŠçã®åé¢: ã€ãã³ãçºçåŽã¯åä¿¡åŽã®è©³çްãç¥ãå¿ èŠããªã
- æè»ãªæ¡åŒµ: æ°ããã€ãã³ããã³ãã©ãŒãåŸãã远å å¯èœ
- ãããã°ã®å®¹æã: ã€ãã³ãã®æµãã远跡ãããã
ã€ãã³ãäœ¿çšæã®æ³šæç¹
ç¬èªã€ãã³ãã䜿çšããéã¯ã以äžã®ç¹ã«æ³šæããŠãã ããã
- ã€ãã³ããã³ãã©ãŒå ã§ãšã©ãŒãçºçãããšãã€ãã³ãçºçå ã«åœ±é¿ããå¯èœæ§ããããŸã
- 埪ç°çãªã€ãã³ãçºçïŒã€ãã³ãAãã€ãã³ãBãçºçãããã€ãã³ãBãã€ãã³ãAãçºçãããïŒãé¿ããŠãã ãã
- ã€ãã³ããã³ãã©ãŒã®åŠçæéãé·ããããšãå šäœã®ããã©ãŒãã³ã¹ã«åœ±é¿ããŸã
WithEventsã䜿ã£ãã€ãã³ãåä¿¡ã®åºæ¬
WithEventsããŒã¯ãŒãã®åœ¹å²
ç¬èªã€ãã³ããåä¿¡ããŠåŠçããããã«ã¯ãWithEventsããŒã¯ãŒãã䜿çšããŠãªããžã§ã¯ãã宣èšããå¿ èŠããããŸããWithEventsã¯ãæå®ãããªããžã§ã¯ããçºçããã€ãã³ããåãåãããšãã§ããããã«ããç¹å¥ãªå®£è𿹿³ã§ãã
åºæ¬çãªã€ãã³ãåä¿¡ã®å®è£
å
ã»ã©äœæããBasicEventClass
ã®ã€ãã³ããåä¿¡ããæšæºã¢ãžã¥ãŒã«ãäœæããŸãããã
' æšæºã¢ãžã¥ãŒã«
' WithEventsããŒã¯ãŒãã䜿çšããŠã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã宣èš
' ããã«ãããBasicEventClassã®ã€ãã³ããåä¿¡å¯èœã«ãªã
Dim WithEvents eventHandler As BasicEventClass
' ã€ãã³ãåä¿¡ã·ã¹ãã ã®åæååŠç
Public Sub InitializeEventSystem()
' BasicEventClassã®æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæ
Set eventHandler = New BasicEventClass
MsgBox "ã€ãã³ãã·ã¹ãã ãåæåãããŸãããã¿ã¹ã¯ãéå§ã§ããŸãã", vbInformation
End Sub
' ã¿ã¹ã¯å®è¡ãéå§ããããã·ãŒãžã£
Public Sub StartTask()
' åæåãããŠããªãå Žåã¯æ©æãªã¿ãŒã³
If eventHandler Is Nothing Then
MsgBox "å
ã«InitializeEventSystemãå®è¡ããŠãã ããã", vbExclamation
Exit Sub
End If
' BasicEventClassã®ã¿ã¹ã¯å®è¡ã¡ãœãããåŒã³åºã
eventHandler.ExecuteTask
End Sub
' TaskCompletedã€ãã³ãã®ãã³ãã©ãŒïŒã€ãã³ãçºçæã«èªåå®è¡ãããïŒ
Private Sub eventHandler_TaskCompleted(taskName As String)
' åä¿¡ããã€ãã³ãæ
å ±ã䜿çšããŠåŸç¶åŠçãå®è¡
MsgBox "ã¿ã¹ã¯ãå®äºããŸããïŒ" & vbCrLf & _
"ã¿ã¹ã¯å: " & taskName & vbCrLf & _
"次ã®åŠçã«é²ãããšãã§ããŸãã", vbInformation
' å®éã®æ¥åã§ã¯ãããã§ã¿ã¹ã¯å®äºåŸã®åŠçãå®è¡
' äŸ: ãã°èšé²ã次ã®ã¿ã¹ã¯ã®éå§ãçµæã®ä¿åãªã©
End Sub
WithEventsã®ä»çµã¿ãšå¶çŽ
1. 宣èšã®å¶çŽäºé
WithEventsã䜿çšãã倿°å®£èšã«ã¯ã以äžã®å¶çŽããããŸãã
å¶çŽé ç® | 説æ | çç± |
---|---|---|
宣èšå Žæ | ã¢ãžã¥ãŒã«ã¬ãã«ã§ã®ã¿å®£èšå¯èœ | VBAã®ã€ãã³ãåŠçæ©æ§ã®å¶é |
é åå®£èš | é åãšããŠå®£èšäžå¯ | åèŠçŽ ã®ã€ãã³ã管çãå°é£ |
New ãšã®äœµçš | å®£èšæã«NewããŒã¯ãŒã䜿çšäžå¯ | åŸããSetã¹ããŒãã¡ã³ãã§ä»£å ¥ |
2. ã€ãã³ããã³ãã©ãŒã®åœåèŠå
Private Sub eventHandler_TaskCompleted(taskName As String)
- åœåãã¿ãŒã³: ã倿°å_ã€ãã³ãåã
-
ã¢ã¯ã»ã¹ä¿®é£Ÿå:
Private
ã䜿çšïŒå€éšããçŽæ¥åŒã³åºãããªãïŒ - èªåèªè: VBAãšãã£ã¿ãèªåçã«ã€ãã³ããã³ãã©ãŒãšããŠèªè
ã€ãã³ããã³ãã©ãŒã®èªåçæ
VBAãšãã£ã¿ã§ã¯ãWithEventsã§å®£èšãã倿°ãéžæãããšããã®ãªããžã§ã¯ãã§å©çšå¯èœãªã€ãã³ãã®äžèЧã衚瀺ãããéžæããããšã§èªåçã«ã€ãã³ããã³ãã©ãŒã®ãã³ãã¬ãŒããçæãããŸããããã«ãããã¿ã€ããã¹ãé²ããå¹ççã«ã€ãã³ããã³ãã©ãŒãäœæã§ããŸãã
ç¬èªã€ãã³ãã®äŸ
ããŒã¿æ€èšŒã€ãã³ãã·ã¹ãã ã®èšèš
ããŒã¿æ€èšŒã®çµæãéç¥ããã€ãã³ãã·ã¹ãã ãäœæããŠã¿ãŸãããã
' ã¯ã©ã¹ã¢ãžã¥ãŒã«å: DataValidationClass
' æ€èšŒçµæã詳现ã«éç¥ããã€ãã³ã
' isValid: æ€èšŒã®æåã»å€±æãã©ã°
' fieldName: æ€èšŒå¯Ÿè±¡ãã£ãŒã«ãã®åç§°
' errorMessage: ãšã©ãŒçºçæã®è©³çްã¡ãã»ãŒãž
Public Event ValidationResult(isValid As Boolean, _
fieldName As String, errorMessage As String)
' æ°å€ç¯å²ã®æ€èšŒãå®è¡ããã¡ãœãã
Public Sub ValidateNumberRange(inputValue As String, minValue As Long, _
maxValue As Long, fieldDisplayName As String)
On Error GoTo ErrorHandler
' æ°å€å€æã詊è¡
Dim numericValue As Long
numericValue = CLng(inputValue)
' æå®ç¯å²å
ãã©ãããæ€èšŒ
If numericValue >= minValue And numericValue <= maxValue Then
' æ€èšŒæåãã€ãã³ãã§éç¥
RaiseEvent ValidationResult(True, fieldDisplayName, "")
Else
' ç¯å²å€ãšã©ãŒãã€ãã³ãã§éç¥
RaiseEvent ValidationResult(False, fieldDisplayName, _
"å€ã¯" & minValue & "ãã" & maxValue & "ã®ç¯å²ã§å
¥åããŠãã ããã")
End If
Exit Sub
ErrorHandler:
' æ°å€å€æãšã©ãŒãã€ãã³ãã§éç¥
RaiseEvent ValidationResult(False, fieldDisplayName, "æ°å€åœ¢åŒã§å
¥åããŠãã ããã")
Err.Clear
End Sub
' å¿
é å
¥åé
ç®ã®æ€èšŒãå®è¡ããã¡ãœãã
Public Sub ValidateRequired(inputValue As String, fieldDisplayName As String)
' 空æååã空çœã®ã¿ã®å
¥åããã§ãã¯
If Trim(inputValue) = "" Then
RaiseEvent ValidationResult(False, fieldDisplayName,_
"å¿
é é
ç®ã§ããå€ãå
¥åããŠãã ããã")
Else
RaiseEvent ValidationResult(True, fieldDisplayName, "")
End If
End Sub
æ€èšŒçµæåŠçã·ã¹ãã ã®å®è£
' æšæºã¢ãžã¥ãŒã«
' ããŒã¿æ€èšŒã€ãã³ããåä¿¡ãããã³ãã©ãŒ
Dim WithEvents validator As DataValidationClass
' æ€èšŒã·ã¹ãã ã®åæå
Public Sub InitializeValidator()
Set validator = New DataValidationClass
MsgBox "ããŒã¿æ€èšŒã·ã¹ãã ãæºåå®äºããŸããã", vbInformation
End Sub
' å®çšçãªæ€èšŒãã¹ãã®å®è¡
Public Sub RunValidationTest()
If validator Is Nothing Then
InitializeValidator
End If
' ãŠãŒã¶ãŒãã幎霢ã®å
¥åãååŸ
Dim ageInput As String
ageInput = InputBox("幎霢ãå
¥åããŠãã ããïŒ18-100æ³ïŒ", "幎霢æ€èšŒãã¹ã")
' å¿
é å
¥åãã§ãã¯
validator.ValidateRequired ageInput, "幎霢"
' å¿
é ãã§ãã¯ãéã£ãå Žåã®ã¿ç¯å²ãã§ãã¯ãå®è¡
If Trim(ageInput) <> "" Then
validator.ValidateNumberRange ageInput, 18, 100, "幎霢"
End If
End Sub
' æ€èšŒçµæã€ãã³ãã®ãã³ãã©ãŒ
Private Sub validator_ValidationResult(isValid As Boolean, _
fieldName As String, errorMessage As String)
If isValid Then
' æ€èšŒæåæã®åŠç
MsgBox fieldName & "ã®æ€èšŒãæåããŸããã", vbInformation
' å®éã®æ¥åã§ã¯ãæåæã®åŸç¶åŠçãå®è¡
' äŸ: 次ã®å
¥åé
ç®ãžã®ç§»åãããŒã¿ä¿åã®èš±å¯ãªã©
Else
' æ€èšŒå€±ææã®åŠç
MsgBox fieldName & "ã®æ€èšŒãšã©ãŒ" & vbCrLf & _
"詳现: " & errorMessage, vbExclamation
' å®éã®æ¥åã§ã¯ããšã©ãŒæã®å¯Ÿå¿åŠçãå®è¡
' äŸ: å
¥åæ¬ã®ãã€ã©ã€ãããšã©ãŒãã°ã®èšé²ãªã©
End If
End Sub
é²æè¡šç€ºã€ãã³ãã·ã¹ãã ã®å®è£
é·æéå®è¡ãããåŠçã®é²è¡ç¶æ³ããªã¢ã«ã¿ã€ã ã§éç¥ããã·ã¹ãã ã§ãã
' ã¯ã©ã¹ã¢ãžã¥ãŒã«å: ProgressReportClass
' é²æç¶æ³ãéç¥ããã€ãã³ã
Public Event ProgressUpdate(currentStep As Long, totalSteps As Long, stepDescription As String)
' åŠçå®äºãéç¥ããã€ãã³ã
Public Event ProcessComplete(executionTime As String, totalItems As Long)
' 段éçãªåŠçãå®è¡ããåæ®µéã§é²æãéç¥ããã¡ãœãã
Public Sub ExecuteMultiStepProcess()
On Error GoTo ErrorHandler
' åŠçéå§æå»ãèšé²
Dim startTime As Date
startTime = Now
' åŠç段éã®å®çŸ©
Dim totalSteps As Long
totalSteps = 4
' ã¹ããã1: åæååŠç
RaiseEvent ProgressUpdate(1, totalSteps, "ã·ã¹ãã ã®åæåãå®è¡äž...")
Call ProcessingDelay(1000) ' 1ç§éã®åŠçãã·ãã¥ã¬ãŒã
' ã¹ããã2: ããŒã¿èªã¿èŸŒã¿
RaiseEvent ProgressUpdate(2, totalSteps, "ããŒã¿ãã¡ã€ã«ãèªã¿èŸŒã¿äž...")
Call ProcessingDelay(2000) ' 2ç§éã®åŠçãã·ãã¥ã¬ãŒã
' ã¹ããã3: ããŒã¿åŠç
RaiseEvent ProgressUpdate(3, totalSteps, "ããŒã¿ã®å€æã»èšç®ãå®è¡äž...")
Call ProcessingDelay(3000) ' 3ç§éã®åŠçãã·ãã¥ã¬ãŒã
' ã¹ããã4: çµæåºå
RaiseEvent ProgressUpdate(4, totalSteps, "çµæãåºåäž...")
Call ProcessingDelay(1000) ' 1ç§éã®åŠçãã·ãã¥ã¬ãŒã
' åŠçå®äºã®éç¥
Dim executionTime As String
executionTime = Format(Now - startTime, "nnåssç§")
RaiseEvent ProcessComplete(executionTime, 1000)
Exit Sub
ErrorHandler:
MsgBox "åŠçå®è¡äžã«ãšã©ãŒãçºçããŸãã: " & Err.Description, vbCritical
Err.Clear
End Sub
' åŠçæéãã·ãã¥ã¬ãŒãããããã®ãã«ããŒã¡ãœãã
Private Sub ProcessingDelay(milliseconds As Long)
' æå®ãããããªç§æ°ã ãåŠçã忢
Dim endTime As Date
endTime = Now + TimeValue("00:00:0" & Format(milliseconds / 1000, "0"))
Application.Wait endTime
End Sub
ãšã©ãŒãã³ããªã³ã°ãšãã¹ããã©ã¯ãã£ã¹
ã€ãã³ãã·ã¹ãã ã§ã®é©åãªãšã©ãŒåŠç
ç¬èªã€ãã³ãã䜿çšããéã¯ããšã©ãŒåŠçã«ç¹å¥ãªæ³šæãå¿ èŠã§ããã€ãã³ããã³ãã©ãŒå ã§ãšã©ãŒãçºçãããšãã€ãã³ãçºçå ã«ã圱é¿ãäžããå¯èœæ§ããããŸãã
' ãšã©ãŒæ
å ±ãéç¥ããå°çšã€ãã³ããå®çŸ©
Public Event ErrorOccurred(errorSource As String, errorDescription As String, errorNumber As Long)
' å®å
šãªã€ãã³ãçºçã¡ãœãã
Public Sub SafeExecuteTask()
On Error GoTo ErrorHandler
' éåžžã®åŠçãå®è¡
Call ExecuteMainProcess
' æ£åžžå®äºãã€ãã³ãã§éç¥
RaiseEvent TaskCompleted("ã¡ã€ã³åŠç")
Exit Sub
ErrorHandler:
' ãšã©ãŒæ
å ±ãã€ãã³ãã§éç¥
RaiseEvent ErrorOccurred("SafeExecuteTask", Err.Description, Err.Number)
Err.Clear
End Sub
' ã¡ã€ã³åŠçã®ã·ãã¥ã¬ãŒã·ã§ã³
Private Sub ExecuteMainProcess()
' å®éã®æ¥ååŠçãããã«èšè¿°
' ãšã©ãŒãçºçããå¯èœæ§ã®ããåŠç
End Sub
ãã¹ããã©ã¯ãã£ã¹
ç¬èªã€ãã³ãã广çã«æŽ»çšããããã®æšå¥šäºé ããŸãšããŸãã
1. ã€ãã³ãèšèšã®åå
- åäžè²¬ä»»: äžã€ã®ã€ãã³ãã¯äžã€ã®åºæ¥äºã®ã¿ãéç¥ãã
- æç¢ºãªåœå: ã€ãã³ãåã¯çºçããåºæ¥äºãæç¢ºã«è¡šçŸãã
- é©åãªåŒæ°: å¿ èŠæå°éãã€ååãªæ å ±ãåŒæ°ãšããŠæž¡ã
2. ãšã©ãŒåŠçã®å®è£ æ¹é
- ãšã©ãŒå°çšã€ãã³ã: ãšã©ãŒæ å ±ãéç¥ããå°çšã€ãã³ããå®çŸ©
- äŸå€ã®äŒæé²æ¢: ã€ãã³ããã³ãã©ãŒå ã§ã®ãšã©ãŒãçºçå ã«åœ±é¿ããªãããé æ ®
- ãã°èšé²: ãšã©ãŒçºçæã¯è©³çŽ°ãªæ å ±ããã°ã«èšé²
3. ããã©ãŒãã³ã¹ã®èæ ®äºé
- ãã³ãã©ãŒã®è»œéå: ã€ãã³ããã³ãã©ãŒå ã®åŠçã¯å¯èœãªéã軜éã«ä¿ã€
- éåæåŠçã®æ€èš: éãåŠçã¯å¥ã®ã¿ã€ãã³ã°ã§å®è¡ããããšãæ€èš
- ã€ãã³ãé »åºŠã®å¶åŸ¡: é »ç¹ãããã€ãã³ãçºçãé¿ãã
ã»ãã¥ãªãã£äžã®æ³šæç¹
ç¬èªã€ãã³ãã䜿çšããéã¯ã以äžã®ã»ãã¥ãªãã£é¢ã§ã®æ³šæãå¿ èŠã§ãã
- ã€ãã³ãåŒæ°ã«æ©å¯æ å ±ãå«ããå Žåã¯ãé©åãªã¢ã¯ã»ã¹å¶åŸ¡ãå®è£ ããŠãã ãã
- ã€ãã³ããã³ãã©ãŒå ã§ãã¡ã€ã«æäœããããã¯ãŒã¯ã¢ã¯ã»ã¹ãè¡ãå Žåã¯ãååãªæ€èšŒãè¡ã£ãŠãã ãã
- å€éšããåãåã£ãããŒã¿ãã€ãã³ãåŒæ°ãšããŠäœ¿çšããå Žåã¯ãäºåã«æ€èšŒã»ãµãã¿ã€ãºã宿œããŠãã ãã
ãŸãšã
ä»å解説ããç¬èªã€ãã³ãã®èšèšãšå®è£ ãã¯ããã¯ã¯ããåŠçã®é²è¡ç¶æ³ããªã¢ã«ã¿ã€ã ã§è¡šç€ºããããŒã¿å€æŽãä»ã®ã¢ãžã¥ãŒã«ã«èªåéç¥ããšãã£ãæ©èœãå®å šèªååããå®çšçãªææ³ã§ãããã®ãã¯ããã¯ã掻çšããããšã§ããè€éãªåŠçã®å®äºãåŸ ã€éã®äžå®ãããšã©ãŒçºçç®æã®ç¹å®å°é£ããšããåŸæ¥ã®åé¡ããè§£æŸãããççµåã§ä¿å®ããããçã«æ¡åŒµæ§ã®é«ããã¯ãã·ã¹ãã ãå®çŸã§ããŸãã
ãã®ææ³ã®æ žå¿ãšãªãã®ã¯ãWithEventsããŒã¯ãŒãã«ããã€ãã³ãåä¿¡ã®ä»çµã¿ãRaiseEventã䜿ã£ãéç¥æ©èœã®å®è£ ããããŠã¯ã©ã¹ã¢ãžã¥ãŒã«ã掻çšããããŒã¿ãšåŠçã®äžå 管çã§ãããããã®æè¡èŠçŽ ãçµã¿åãããããšã§ãããŒã¿æ€èšŒã·ã¹ãã ãé²æè¡šç€ºæ©èœããšã©ãŒéç¥ã·ã¹ãã ãªã©ãæ§ã ãªå Žé¢ã§å¿çšå¯èœãªæ±çšçãªã€ãã³ãé§åã·ã¹ãã ãæ§ç¯ã§ããŸãã
å®è£ æã«ç¹ã«éèŠãªã®ã¯ãé©åãªãšã©ãŒãã³ããªã³ã°ãšã€ãã³ããã³ãã©ãŒã®è»œéåã®åŸ¹åºã§ããã€ãã³ããã³ãã©ãŒå ã§ã®ãšã©ãŒãçºçå ã«åœ±é¿ããªãããé æ ®ãããšã©ãŒå°çšã€ãã³ããå®çŸ©ããããšã§ãå ç¢ã§å®çšçãªã·ã¹ãã ãäœæã§ããŸãããŸãã颿°ãšããŠåé¢ããèšèšã«ããåå©çšæ§ãšä¿å®æ§ã確ä¿ããåäžè²¬ä»»ã®ååã«åºã¥ããæç¢ºãªã€ãã³ãèšèšãè¡ãããšã§ãä»ã®ãããžã§ã¯ãã§ã容æã«æŽ»çšã§ããæ±çšçãªã¯ã©ã¹ã©ã€ãã©ãªãšããŠçºå±ãããããšãå¯èœã§ãã
次åã¯ãExcel VBAã«ãããEnumåãæŽ»çšããã¡ã³ããã³ã¹æ§åäžãã¯ããã¯ã«ã€ããŠè©³ãã解説ããŸãïŒã·ãŒãã¬ã€ã¢ãŠãã®å€æŽãé »ç¹ã«çºçããæ¥åç°å¢ã§ãæ°å€çŽæ¥æå®ã«ãããå°çã®ãããªä¿®æ£äœæ¥ããããEnumåã®èªåè¿œåŸæ©èœã«ãã£ãŠã©ã®ããã«è§£æŸãããããå ·äœçã«ã玹ä»ããŸããè€æ°ã·ãŒãç°å¢ã§ã®èšèšæŠç¥ã宿°ç®¡çã®äžå åã«ããå¹çåããããŠãåçªå·ã®å€æŽã§ãã¯ããåããªããªãããšãã£ãéçšäžã®åé¡ãæ ¹æ¬ãã解決ããææ³ããäŒãããäºå®ã§ãããã²ãæåŸ ãã ããïŒ