0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初級者】インデントのイメージ

Last updated at Posted at 2025-02-17

初めに

このサンプルは初めての人用、にインデントのイメージです

注意

あくまでもこちらのインデントの考え方以外にもいろいろあるので、そのうちの一つです。

インデントタイミング 

関数の始まりと終わり

関数の宣言後の内容についてインデントします
(これは定義はインデントしない流派もあります)

function fncProg() as String
  'インデント 
   
    '定義
     dim hoge as string

    '本文
    hoge = "テスト表示"
    msgbox(hoge)

    fncProg = ""
end function

判定式の内容

判定分の場合の実行方法もインデントします

Select Case Cells(i, 4)
Case "投手"
    cnt1 = cnt1 + 1  '←インデントする
Case Else
    cnt4 = cnt4 + 1  '←インデントする
End Select

If cnt4 > 1 Then
    msgbox("")  '←インデントする
End If

繰り返し文の中身

繰り返し処理での実行内容もインデントします

For i = 3 To 最終行
    i= i +1  '←インデントする
Next

二重入れ子

上記のインデント条件が重なる場合は再度、再度インデントをします

For i = 3 To 最終行
    i= i +1  '←インデントする
    
    'For文の中のFor文なのでインデントする
    For cnt = 1 To 最終行
        cnt= cnt +1  '←インデントする
    Next
Next

サンプル

入れ子サンプル

'--------------------------------
'関数名 :選手カウント
'内容  :それぞれの守備登録人数を数え、E列に表示する       
'引数    :なし
'戻り値  :なし
'--------------------------------
Sub カウント()
    Dim i As Long
    Dim j As Long
    Dim cnt1 As Long
    Dim cnt2 As Long
    Dim cnt3 As Long
    Dim cnt4 As Long

    Dim 最終行 As Long

    最終行 = Cells(Rows.Count, "C").End(xlUp).Row

    For i = 3 To 最終行

        For k = 2 To 最終行

            Select Case Cells(i, 4)
            Case "投手"
                cnt1 = cnt1 + 1
            Case Else
                cnt4 = cnt4 + 1
            End Select

            If cnt4 > 1 Then
                msgbox("")
            End If

            If Cells(i, 4) = "投手" Then
                cnt1 = cnt1 + 1
            ElseIf Cells(i, 4) = "捕手" Then
                cnt2 = cnt2 + 1
            ElseIf Cells(i, 4) = "内野手" Then
                cnt3 = cnt3 + 1
            Else
                cnt4 = cnt4 + 1
            End If


        Next

        If cnt4 > 1 Then
            msgbox("")
        End If

    Next

    Dim setFlg As Boolean

    For i = 3 To 最終行

        setFlg = False

        If Cells(i, 4) = "投手" Then
            cnt1 = cnt1 + 1
            setFlg = True

        End If

        If Cells(i, 4) = "捕手" Then
            cnt2 = cnt2 + 1
            setFlg = True
        End If

        If Cells(i, 4) = "内野手" Then
            cnt3 = cnt3 + 1
            setFlg = True
        End If

        If setFlg = False Then
            cnt4 = cnt4 + 1
        End If
    Next

    Cells(4, 7) = cnt1
    Cells(5, 7) = cnt2
    Cells(6, 7) = cnt3
    Cells(7, 7) = cnt4


End Sub


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?