アメリカの大学生活---日記

ミネソタ州立大学ムーアヘッド校 会計学部を5月に卒業して帰国し、税務の仕事してます。

練習問題4(Ifの練習)

 

excel-ubara.com

 

 

Sub 練習問題4 ()
Dim i As Long
   For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
   Cells(i, 4) = Int(Cells(i, 2) / Cells(i, 3))
   Cells(i, 5) = Cells(i, 2) - (Cells(i, 4) * Cells(i, 3))
   If Cells(i, 4) = 0 Then
   Cells(i, 4) = "×"
End If
Next
End Sub

 

*メモ(よく忘れやすい所)

- 各々の値の関係を理解する。

- If Cells( , ) = 数字 Then

     Cells( , ) = "ox"

  End If

 

excel-ubara.com

 

Sub 練習問題5()
Dim i As Long
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
Cells(i, 4) = Cells(i, 3) / Cells(i, 2)
If Cells(i, 4) >= 1.05 Then
   Cells(i, 5) = "S"
ElseIf Cells(i, 4) >= 1 Then
    Cells(i, 5) = "A"
ElseIf Cells(i, 4) >= 0.95 Then
    Cells(i, 5) = "B"
ElseIf Cells(i, 4) >= 0.9 Then
    Cells(i, 5) = "C"
Else
    Cells(i, 5) = "D"
End If
 Next
End Sub

 

*肢選択においては、Select Caseの方が可読性が良く, なるべく、Select Caseを使用する。 ただし、どちらでも書けるように練習しておく。

 

Sub 練習問題5_2()
Dim i As Long
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
Cells(i, 4) = Cells(i, 3) / Cells(i, 2)
 Select Case Cells(i, 4)
            Case Is >= 1.05
                    Cells(i, 5) = "S"
            Case Is >= 1
                    Cells(i, 5) = "A"
            Case Is >= 0.95
                    Cells(i, 5) = "B"
            Case Is >= 0.9
                    Cells(i, 5) = "C"
   Case Else
                    Cells(i, 5) = "D"
 End Select
 Next
End Sub