Excel VBAã§éžæããç¯å²ã«å¯ŸããŠãäžè¡ããã«ç©ºè¡ãæ¿å ¥ãããã¯ããã¯
ç§ã¯VBAã®æŽ»çšçµéšãéããŠåŸãç¥èãæŽçããå ±æããç®çã§èšäºãäœæããŠããããã°ã©ãã³ã°æŽ1幎åã«ãªããšã³ãžãã¢ã§ããååã¯ãæå®ããããã©ã«ãå ããææ°ã®Excelãã¡ã€ã«ãèªåçã«æ€çŽ¢ã»ååŸããå®è·µçãªé¢æ°ã«ã€ããŠè©³ãã説æããŸãããä»åã¯ãéžæããç¯å²ã«å¯ŸããŠãäžè¡ããã«ç©ºè¡ãæ¿å ¥ããå®çšçãªãã¯ãã«ã€ããŠè§£èª¬ããŸãã
- 第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ã«ãããéè€ã®ãªãã·ãŒãåçæã®å¹ççå®è£ ãã¯ããã¯
ç®æ¬¡
- ã¯ããã«
- ç©ºè¡æ¿å ¥ã®èª²é¡ãšè§£æ±ºæ¹æ³
- åºæ¬ã³ãŒãã®è§£èª¬
- éé åŠçã®éèŠæ§
- æ¹è¯çã®å®è£
- é¡äŒŒãã¯ããã¯ã®ç޹ä»
- å®è£ æã®æ³šæç¹ãšãã¹ããã©ã¯ãã£ã¹
- ãŸãšã
ã¯ããã«
Excelã§ããŒã¿ãæŽçã»è¿œå ããéãæ¢åã®ããŒã¿éã«è¿œå ã®æ å ±ãæ¿å ¥ããã¹ããŒã¹ãå¿ èŠã«ãªãããšããããŸãããŸããé 眮ãããããŒã¿ãèŠãããããããã«è¡éã空ãããå Žåããå°å·æã®èªã¿ããããåäžãããããã«äœçœã確ä¿ãããå Žåãªã©ãäžè¡ããã«ç©ºè¡ãæ¿å ¥ããäœæ¥ã¯æå€ãšé »ç¹ã«çºçããŸãã
ããããæåã§äžã€ãã€è¡ãæ¿å ¥ããŠããäœæ¥ã¯æéããããã倧éã®ããŒã¿ã«å¯ŸããŠã¯çŸå®çã§ã¯ãããŸããããŸããæ¿å ¥äœçœ®ãééãããªã©ãã¥ãŒãã³ãšã©ãŒã«ããå·¥æ°ã®å¢å ãšããåé¡ããããŸãã
ä»å玹ä»ããInsertEmptyRowsBetween颿°ã¯ããã®ãããªèª²é¡ã解決ããããã«èšèšããŸãããéžæããç¯å²ã«å¯ŸããŠäžè¡ããã«ç©ºè¡ãèªåæ¿å ¥ããå®çšçãªãã¯ãã§ãæåã§ã®ç ©éãªäœæ¥ããè§£æŸãããå¹ççã«ã·ãŒãã®ã¬ã€ã¢ãŠãã調æŽã§ããŸãã
ç©ºè¡æ¿å ¥ã®èª²é¡ãšè§£æ±ºæ¹æ³
æåã®èª²é¡
æåã§ç©ºè¡ãæ¿å ¥ããéã®åé¡ç¹ãæŽçããŠã¿ãŸãããã
- 倧éã®ããŒã¿ã«å¯ŸããŠäžè¡ãã€æåã§æ¿å ¥ããã®ã¯æéãããã
- æ¿å ¥äœçœ®ãééãããªã©ãã¥ãŒãã³ãšã©ãŒã«ããå·¥æ°ã®å¢å
- äœæ¥ã®äžè²«æ§ãä¿ã€ã®ãå°é£
è§£æ±ºæ¹æ³ã®ã¢ãããŒã
ãããã®èª²é¡ã解決ããããã«ãä»åã®ãã¯ãã§ã¯ä»¥äžã®ææ³ãæ¡çšããŠããŸãã
- éžæç¯å²ã®èªåååŸ: ãŠãŒã¶ãŒãéžæããç¯å²ãèªåçã«èªè
- éé åŠçã«ããå®å šãªæ¿å ¥: æäžè¡ããäžã«åãã£ãŠåŠçããããšã§ãè¡çªå·ã®ããã鲿¢
- ã·ã³ãã«ãªã«ãŒãæ§é : çè§£ãããããä¿å®æ§ã®é«ãã³ãŒãèšèš
åºæ¬ã³ãŒãã®è§£èª¬
Sub InsertEmptyRowsBetween()
' éžæç¯å²ãæ ŒçŽãã倿°
Dim mg As Range
' çŸåšéžæãããŠããç¯å²ãmg倿°ã«ä»£å
¥
Set mg = Selection
' éžæç¯å²ã®è¡æ°ãã1ãŸã§éé ã«ã«ãŒããå®è¡
Dim i As Long
For i = mg.Rows.count To 1 Step -1
' çŸåšã®è¡ã®æ¬¡ã®äœçœ®ã«æ°ããè¡ãæ¿å
¥ïŒäžæ¹åã«ã·ããïŒ
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
End Sub
ã«ãŒãåŠçã®æ§é
For i = mg.Rows.count To 1 Step -1
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
ãã®ã«ãŒãã¯éžæç¯å²ã®æäžè¡ããæäžè¡ã«åãã£ãŠéé ã«åŠçãè¡ããŸããStep -1 ã«ãã£ãŠãã«ãŠã³ã¿ãŒã1ãã€æžå°ããªããã«ãŒããå®è¡ãããŸãã
éé åŠçã®éèŠæ§
ãªãéé åŠçãå¿ èŠãªã®ã
è¡ã®æ¿å ¥åŠçã«ãããŠéé åŠçãéèŠãªçç±ã詳ãã説æããŸãã
é æ¹ååŠçã®åé¡ç¹
äžããäžã«åãã£ãŠåŠçãè¡ã£ãå Žåãè¡ãæ¿å ¥ãããã³ã«æ¢åã®è¡çªå·ããããŠããŸããŸãã
' åé¡ã®ããã³ãŒãäŸïŒé æ¹ååŠçïŒ
For i = 1 To mg.Rows.count
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
- æåã®ã«ãŒãïŒi=1ïŒã§2è¡ç®ã«ç©ºè¡ãæ¿å ¥
- å ã®2è¡ç®ã3è¡ç®ã«ããã
- 次ã®ã«ãŒãïŒi=2ïŒã§3è¡ç®ã«ç©ºè¡ãæ¿å ¥ãããšãå ã®2è¡ç®ïŒçŸåšã®3è¡ç®ïŒã®çŽåŸã«æ¿å ¥ãããŠããŸã
éé åŠçã®å©ç¹
éé åŠçã§ã¯ãæ¿å ¥äœçœ®ããäžã®è¡çªå·ã¯å€åããªããããæ³å®éãã«åŠçãç¶è¡ã§ããŸãã
' æ£ããã³ãŒãïŒéé åŠçïŒ
For i = mg.Rows.count To 1 Step -1
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
ãã®åŠçã§ã¯ãæäžè¡ããé çªã«ç©ºè¡ãæ¿å ¥ããããããŸã åŠçããŠããªãäžåŽã®è¡ã®çªå·ã«åœ±é¿ãäžããŸããã
æ¹è¯çã®å®è£
åºæ¬ã³ãŒãã¯åäœããŸãããå®çšæ§ãé«ããããã«ããã€ãã®æ¹è¯ãå ããŠãããã§ãããã
ãšã©ãŒãã³ããªã³ã°ä»ãã®æ¹è¯ç
Sub InsertEmptyRowsBetweenImproved()
' 倿°å®£èš
Dim mg As Range
Dim i As Long
Dim isCompleted As Boolean
' åæå
isCompleted = False
' ãšã©ãŒãçºçããå Žåã®åŠçå
ãæå®
On Error GoTo Cleanup
' éžæç¯å²ãåäžã»ã«ã®å Žåã¯åŠçãçµäº
If Selection.Rows.Count = 1 Then
' ã¡ãã»ãŒãžããã¯ã¹ã§èŠåã衚瀺
MsgBox "è€æ°è¡ãéžæããŠãããã¯ããå®è¡ããŠãã ããã", vbExclamation
' CleanupåŠçãžç§»å
GoTo Cleanup
End If
' çŸåšã®éžæç¯å²ãmg倿°ã«ä»£å
¥
Set mg = Selection
' åŠçéå§ãç¥ãããã¡ãã»ãŒãžã衚瀺
Application.StatusBar = "ç©ºè¡æ¿å
¥åŠçãå®è¡äž..."
' ç»é¢æŽæ°ãäžæåæ¢ïŒåŠçé床åäžã®ããïŒ
Application.ScreenUpdating = False
' éžæç¯å²ã®æäžè¡ããæäžè¡ã«åãã£ãŠéé ã«ãŒã
For i = mg.Rows.Count To 1 Step -1
' çŸåšã®è¡ã®æ¬¡ã®äœçœ®ã«ç©ºè¡ãæ¿å
¥
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
' åŠçãæ£åžžã«å®äºããããšãèšé²
isCompleted = True
Cleanup:
' ç»é¢æŽæ°ãå¿
ãåéïŒæ£åžžçµäºã»ãšã©ãŒçµäºåããïŒ
Application.ScreenUpdating = True
' ã¹ããŒã¿ã¹ããŒãã¯ãªã¢
Application.StatusBar = False
' åŠççµæã«å¿ããŠã¡ãã»ãŒãžã衚瀺
If Err.Number <> 0 Then
' ãšã©ãŒãçºçããå Žå
MsgBox "ãšã©ãŒãçºçããŸãã: " & Err.Description, vbCritical
ElseIf isCompleted Then
' æ£åžžã«åŠçãå®äºããå Žå
MsgBox "空è¡ã®æ¿å
¥ãå®äºããŸããã", vbInformation
End If
' ãšã©ãŒæ
å ±ãã¯ãªã¢
Err.Clear
End Sub
æ¹è¯ç¹ã®è©³çŽ°èª¬æ
1. GoTo Cleanup ãã¿ãŒã³ã®å°å ¥
On Error GoTo Cleanup
' ... åŠçæ¬äœ ...
GoTo Cleanup ' æ£åžžçµäºæãåãCleanupåŠçãå®è¡
Cleanup:
' å¿
èŠãªã¯ãªãŒã³ã¢ããåŠç
Application.ScreenUpdating = True
Application.StatusBar = False
ãã®æ§é ã«ãããæ£åžžçµäºãšãšã©ãŒçµäºã®ã©ã¡ãã®å Žåã§ãã確å®ã«ã¯ãªãŒã³ã¢ããåŠçãå®è¡ãããŸãã
2. ç¶æ 管çã«ããé©åãªã¡ãã»ãŒãžè¡šç€º
Dim isCompleted As Boolean
isCompleted = False
' åŠçå®äºæ
isCompleted = True
' CleanupåŠçå
ã§ã®æ¡ä»¶åå²
If Err.Number <> 0 Then
' ãšã©ãŒã¡ãã»ãŒãž
ElseIf isCompleted Then
' å®äºã¡ãã»ãŒãž
End If
isCompleted
ãã©ã°ã䜿çšããããšã§ãåŠçã®å®äºç¶æ
ãæ£ç¢ºã«ææ¡ã§ããŸããããã«ããã以äžã®3ã€ã®ç¶æ
ãé©åã«å€å¥ã§ããŸãã
-
ãšã©ãŒçºç:
Err.Number <> 0
ã®å Žå -
æ£åžžå®äº:
isCompleted = True
ãã€ãšã©ãŒãªãã®å Žå - æ©æçµäº: å ¥å倿€èšŒã§åŠçãäžæãããå ŽåïŒã¡ãã»ãŒãžè¡šç€ºãªãïŒ
3. å ¥å倿€èšŒã®æ¹è¯
If Selection.Rows.Count = 1 Then
MsgBox "è€æ°è¡ãéžæããŠãããã¯ããå®è¡ããŠãã ããã", vbExclamation
GoTo Cleanup
End If
GoTo Cleanup
ã«ããå
¥å倿€èšŒã§åŠçãäžæãããå Žåã§ããå¿
èŠãªã¯ãªãŒã³ã¢ããåŠçãå®è¡ãããŸãããã®å ŽåãisCompleted
ã¯False
ã®ãŸãŸãªã®ã§ãå®äºã¡ãã»ãŒãžã¯è¡šç€ºããããèŠåã¡ãã»ãŒãžã®ã¿ã衚瀺ãããŸãã
4. ãšã©ãŒæ å ±ã®é©åãªç®¡ç
' ãšã©ãŒæ
å ±ãã¯ãªã¢
Err.Clear
CleanupåŠçã®æåŸã§Err.Clear
ãåŒã³åºãããšã§ããšã©ãŒæ
å ±ã確å®ã«ã¯ãªã¢ããŸããããã«ãããåŸç¶ã®åŠçã«åœ±é¿ãäžããããšãé²ããŸãã
5. ã³ãŒãã®ä¿å®æ§åäž
ãã®æ¹è¯çã§ã¯ã以äžã®ä¿å®æ§ã®åäžãå³ãããŠããŸãã
- åäžè²¬ä»»ã®åå: CleanupåŠçãäžç®æã«éçŽããã
- DRYïŒDon't Repeat YourselfïŒ: ã³ãŒãã®éè€ãå®å šã«æé€ããã
- å¯èªæ§ã®åäž: åŠçã®æµããæç¢ºã«ãªã
- æ¡åŒµæ§: æ°ããã¯ãªãŒã³ã¢ããåŠçã远å ããéããäžç®æã®ä¿®æ£ã§å¯Ÿå¿å¯èœ
é¡äŒŒãã¯ããã¯ã®ç޹ä»
1. æå®ééã§ã®ç©ºè¡æ¿å ¥
äžè¡ããã§ã¯ãªããæå®ããééã§ç©ºè¡ãæ¿å ¥ããå¿çšçã§ãã
Sub InsertRowsWithInterval()
Dim i As Long
Dim mg As Rang
Set mg = Selection
' æ¿å
¥ééã3è¡ã«èšå®
Dim interval As Long
interval = 3
For i = mg.Rows.count To interval Step -interval
' æå®äœçœ®ã«ç©ºè¡ãæ¿å
¥
mg.Rows(i + 1).Insert Shift:=xlDown
Next i
End Sub
2. è€æ°è¡åææ¿å ¥ç
äžåºŠã«è€æ°è¡ãæ¿å ¥ããããŒãžã§ã³ã§ãã
Sub InsertMultipleRows()
Dim i As Long
Dim mg As Rang
Set mg = Selection
' äžåºŠã«æ¿å
¥ããè¡æ°ã2è¡ã«èšå®
Dim insertCount As Long
insertCount = 2
For i = mg.Rows.count To 1 Step -1
' æå®ããè¡æ°åã®ç©ºè¡ãäžåºŠã«æ¿å
¥
mg.Rows(i + 1).Resize(insertCount).Insert Shift:=xlDown
Next i
End Sub
3. æ¡ä»¶ä»ãç©ºè¡æ¿å ¥
ç¹å®ã®æ¡ä»¶ãæºããè¡ã®åŸã«ã®ã¿ç©ºè¡ãæ¿å ¥ããããŒãžã§ã³ã§ãã
Sub InsertRowsConditionally()
Dim i As Long
Dim mg As Rang
Set mg = Selection
For i = mg.Rows.count To 1 Step -1
' çŸåšã®è¡ã®æåã®ã»ã«ã®å€ãååŸ
Dim cellValue As String
cellValue = mg.Cells(i, 1).Value
' ã»ã«ã®å€ã空çœã§ãªãå Žåã®ã¿ç©ºè¡ãæ¿å
¥
If cellValue <> "" Then
' çŸåšã®è¡ã®æ¬¡ã®äœçœ®ã«ç©ºè¡ãæ¿å
¥
mg.Rows(i + 1).Insert Shift:=xlDown
End If
Next i
End Sub
å®è£ æã®æ³šæç¹ãšãã¹ããã©ã¯ãã£ã¹
1. ã¡ã¢ãªå¹çã®èæ ®
倧éã®ããŒã¿ãåŠçããéã¯ãã¡ã¢ãªäœ¿çšéã«æ³šæãå¿ èŠã§ãã
' å¹ççãªãªããžã§ã¯ãåç
§ã®ç®¡ç
Set mg = Nothing ' åŠçå®äºåŸã«ãªããžã§ã¯ãåç
§ãã¯ãªã¢
2. ãŠãŒã¶ããªãã£ã®åäž
' åŠçåã«ç¢ºèªãã€ã¢ãã°ã衚瀺
Dim result As VbMsgBoxResult
result = MsgBox("éžæç¯å²ã«ç©ºè¡ãæ¿å
¥ããŸããïŒ", vbYesNo + vbQuestion)
If result = vbNo Then
Exit Sub
End If
3. ã¢ã³ãã¥ïŒå ã«æ»ãïŒãžã®é æ ®
VBAã§å®è¡ããæäœã¯ãéåžžã®Ctrl+Zã§ã¯å ã«æ»ããªãå ŽåããããŸããéèŠãªããŒã¿ãæ±ãéã¯ãäºåã«ããã¯ã¢ãããäœæããããšãæšå¥šããŸãã
' ã¯ãŒã¯ããã¯ã®ä¿åç¶æ
ã確èª
If Not ActiveWorkbook.Saved Then
Dim saveResult As VbMsgBoxResult
saveResult = MsgBox("äœæ¥å
容ãä¿åããŠããå®è¡ããããšããå§ãããŸããç¶è¡ããŸããïŒ", _
vbYesNo + vbExclamation)
If saveResult = vbNo Then
Exit Sub
End If
End If
ãŸãšã
ä»å解説ããInsertEmptyRowsBetween颿°ã¯ãã·ã³ãã«ãªãããå®çšæ§ã®é«ãè¡æ¿å ¥ãã¯ãã§ããéé åŠçãšããéèŠãªæŠå¿µãçè§£ããããšã§ãè¡ãåã®æ¿å ¥ã»åé€ã䌎ãåŠçãå®å šã«å®è£ ã§ããããã«ãªããŸãã
ãã®é¢æ°ã®æ žå¿ãšãªãã®ã¯ãRangeãªããžã§ã¯ãã®æäœãšéé ã«ãŒãåŠçã®çµã¿åããã§ããSelection ããããã£ã«ãããŠãŒã¶ãŒéžæç¯å²ã®ååŸãRows.Insert ã¡ãœããã«ããè¡æ¿å ¥ããã㊠Step -1 ã«ããéé åŠçã«ãããå¹ççã§å®å šãªç©ºè¡æ¿å ¥ãå®çŸããŠããŸãã
å®éã®æ¥åã§æŽ»çšããéã«ã¯ããšã©ãŒãã³ããªã³ã°ãããã©ãŒãã³ã¹æé©åããŠãŒã¶ããªãã£ã®åäžãªã©ã®æ¹è¯ãå ããããšã§ãããå ç¢ãªã·ã¹ãã ãæ§ç¯ã§ããŸãããŸãã玹ä»ããé¡äŒŒãã¯ããã¯ãå¿çšããããšã§ãæ§ã ãªè¡æ¿å ¥ããŒãºã«å¯Ÿå¿ã§ããã§ãããã
ãã®ãããªåºæ¬çãªè¡æäœãã¯ãããã¹ã¿ãŒããããšã§ãExcelã§ã®ããŒã¿æŽçäœæ¥ãå€§å¹ ã«å¹çåã§ããããè€éãªãã¯ãéçºã®åºç€ã¹ãã«ã身ã«ã€ããããšãã§ããŸãããã²ãèªåã®æ¥åç°å¢ã«åãããŠã«ã¹ã¿ãã€ãºããæŽ»çšããŠã¿ãŠãã ããã
次åã¯ãExcel VBAã§å¯èŠã»ã«ïŒãã£ã«ã¿ãŒé©çšåŸã«è¡šç€ºãããŠããã»ã«ïŒã掻çšããé«åºŠãªãã£ã«ã¿ãŒæäœãã¯ããã¯ã«ã€ããŠè§£èª¬ããŸãããã£ã«ã¿ãŒçµæãå¥ã®æ¡ä»¶ãšããŠåå©çšããå®è·µçãªãã¯ãã§ãããã²ãæåŸ ãã ããïŒ