
関数エラーがあったらA列に列アルファベットをいれるよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、Excelのシート上で関数エラーを検出し、そのエラーがある行のA列に対応する列のアルファベットを記録するものです。
プロシージャの説明
画面更新の停止
`Application.ScreenUpdating = False` により、処理が終わるまで画面の更新を停止します。これにより処理が速くなります。
変数の宣言
`FoundCell`, `FirstCell`, `Target` は `Range` 型の変数で、セルの範囲を示します。
`K1` はエラー値を格納する配列で、エラータイプが7つ入っています。
`i` はループカウンタ、`N` は列アルファベットを保持するための変数です。
A列の挿入と初期設定
新しい列をA列として挿入し、1行目に「関数フラグ」と設定します。
`K1` 配列にExcelの関数エラーを7種類格納します。
エラーの検索と記録
`For i = LBound(K1) To UBound(K1)` のループで、`K1` に格納されたエラーを順番に検索します。
`Cells.Find` でエラーを含むセルを探し、見つかったらその列アルファベットをA列に記録します。
`Do Loop` でエラーを含む他のセルを `FindNext` を使って順次検索し、同様に記録します。
フィルタの再設定
最後の列にフィルタを設定し直します。
メッセージの表示と画面更新の再開
エラーチェックが完了したことをユーザーに知らせるメッセージボックスを表示し、`Application.ScreenUpdating = True` で画面更新を再開します。
具体的な処理例
例えば、C列に `#N/A` エラーがあった場合、A列の対応する行に「_C」という値が追加されます。次にD列に `#NAME?` エラーがあった場合、同様にA列に「_D」と追加されます。
Sub 関数エラーがあったらA列に列アルファベットをいれるよ()
Application.ScreenUpdating = False
Dim FoundCell As Range, FirstCell As Range, Target As Range
Dim K1(6) As String, i As Long, N As String
Columns("A").Insert
Cells(1, 1) = "関数フラグ"
K1(0) = "#N/A"
K1(1) = "#NAME?"
K1(2) = "#NULL!"
K1(3) = "#NUM!"
K1(4) = "#REF!"
K1(5) = "#VALUE!"
K1(6) = "#DIV/0!"
For i = LBound(K1) To UBound(K1)
Set FoundCell = Cells.Find(What:="*" & K1(i) & "*")
If FoundCell Is Nothing Then
GoTo L1
' MsgBox "見つかりません"
' Exit Sub
Else
Set FirstCell = FoundCell
End If
N = Split(FoundCell.Address(True, False), "$")(0)
Cells(FoundCell.Row, 1) = Cells(FoundCell.Row, 1).Value & "_" & N
Do
Set FoundCell = Cells.FindNext(FoundCell)
If FoundCell.Address = FirstCell.Address Then
Exit Do
ElseIf FoundCell.Column > 0 Then
N = Split(FoundCell.Address(True, False), "$")(0)
Cells(FoundCell.Row, 1) = Cells(FoundCell.Row, 1).Value & "_" & N
ElseIf FoundCell.Column = "" Then
Exit Do
End If
Loop
L1:
Next i
' フィルタかけなおし
Dim C As Long
C = Cells(1, Columns.Count).End(xlToLeft).Column
If ActiveSheet.AutoFilterMode = True Then
Cells(1, C).AutoFilter
Cells(1, C).AutoFilter
Else
Cells(1, C).AutoFilter
End If
MsgBox "関数フラグ完了!"
Application.ScreenUpdating = True
End Sub
Excel VBA リファレンス | Microsoft Learn
この記事のYouTube動画はこちら
キーワード
#excel #できること #vba #関数エラー #エラー検出 #列アルファベット #セルの検索 #フィルタ設定 #A列 #関数フラグ #Findメソッド #エクセル自動化 #プログラミング初心者 #配列の利用 #エクセルVBA #画面更新停止 #エラーチェック #DoLoop #Rangeオブジェクト #自動化ツール
Function Error Check and Record in Column A
This explanation is created using ChatGPT.
This VBA procedure identifies function errors in an Excel sheet and records the column alphabet of the error in column A of the corresponding row.
Procedure Explanation
Stop Screen Updating
`Application.ScreenUpdating = False` stops screen updating until the process is complete, improving speed.
Declare Variables
`FoundCell`, `FirstCell`, `Target` are `Range` type variables representing cell ranges.
`K1` is an array holding error types with seven entries.
`i` is a loop counter, and `N` holds the column alphabet.
Insert Column A and Initialize
A new column is inserted as column A, with "Function Flag" set in the first row.
The `K1` array stores seven types of Excel function errors.
Search and Record Errors
The `For i = LBound(K1) To UBound(K1)` loop searches for errors in `K1` sequentially.
`Cells.Find` searches for cells containing errors and records the column alphabet in column A.
The `Do Loop` uses `FindNext` to search for other cells with errors, recording them similarly.
Reset Filter
The filter is reset on the last column.
Display Message and Resume Screen Updating
A message box informs the user that the error check is complete, and `Application.ScreenUpdating = True` resumes screen updating.
Example Process
For instance, if column C has a `#N/A` error, "_C" is added to the corresponding row in column A. If column D has a `#NAME?` error, "_D" is similarly added.
Sub InsertColumnAlphabetIfErrorInFunctions()
Application.ScreenUpdating = False
Dim FoundCell As Range, FirstCell As Range, Target As Range
Dim K1(6) As String, i As Long, N As String
Columns("A").Insert
Cells(1, 1) = "Function Flag"
K1(0) = "#N/A"
K1(1) = "#NAME?"
K1(2) = "#NULL!"
K1(3) = "#NUM!"
K1(4) = "#REF!"
K1(5) = "#VALUE!"
K1(6) = "#DIV/0!"
For i = LBound(K1) To UBound(K1)
Set FoundCell = Cells.Find(What:="*" & K1(i) & "*")
If FoundCell Is Nothing Then
GoTo L1
' MsgBox "Not Found"
' Exit Sub
Else
Set FirstCell = FoundCell
End If
N = Split(FoundCell.Address(True, False), "$")(0)
Cells(FoundCell.Row, 1) = Cells(FoundCell.Row, 1).Value & "_" & N
Do
Set FoundCell = Cells.FindNext(FoundCell)
If FoundCell.Address = FirstCell.Address Then
Exit Do
ElseIf FoundCell.Column > 0 Then
N = Split(FoundCell.Address(True, False), "$")(0)
Cells(FoundCell.Row, 1) = Cells(FoundCell.Row, 1).Value & "_" & N
ElseIf FoundCell.Column = "" Then
Exit Do
End If
Loop
L1:
Next i
' Reapply Filter
Dim C As Long
C = Cells(1, Columns.Count).End(xlToLeft).Column
If ActiveSheet.AutoFilterMode = True Then
Cells(1, C).AutoFilter
Cells(1, C).AutoFilter
Else
Cells(1, C).AutoFilter
End If
MsgBox "Function Flag Completed!"
Application.ScreenUpdating = True
End Sub
Excel VBA Reference | Microsoft Learn
YouTube Video of This Article
Keywords