anonymous No title
No License VBA
2020年11月11日
Copy Clone
Sub VBA100本ノック_22_B()
    Cells.ClearContents
    Dim i As Long
    Dim clm As Long
    Dim talk As Variant
    Dim ans As Variant

    For i = 1 To 30
        Select Case True
            Case i Mod 15 = 0
                ans = "FizzBuzz": clm = 4
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case i Mod 5 = 0
                ans = "Buzz": clm = 3
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case i Mod 3 = 0
                ans = "Fizz": clm = 2
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case Else
                ans = i: clm = 1
                Cells(i, clm) = IIf(talk = "", ans, talk)
        End Select
        Cells(i, clm).Select
        If talk <> "" Then
            If talk <> ans Then
                Cells(i + 1, 1) = "はいダメーー"
                Exit Sub
            Else
                talk = ""
            End If
        End If
        If Int((10 - 1 + 1) * Rnd + 1) = 1 Then
           talk = RandRet(i + 1)
        End If
        Application.Wait [Now() + TimeValue("00:00:00.5")]
    Next
End Sub

Function RandRet(ByVal n As Long) As Variant
    Dim arr As Variant
    arr = Array(n, "Fizz", "Buzz", "FizzBuzz")
    RandRet = arr(WorksheetFunction.RandBetween(0, 3))
End Function
Sub VBA100本ノック_22_B()
    Cells.ClearContents
    Dim i As Long
    Dim clm As Long
    Dim talk As Variant
    Dim ans As Variant

    For i = 1 To 30
        Select Case True
            Case i Mod 15 = 0
                ans = "FizzBuzz": clm = 4
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case i Mod 5 = 0
                ans = "Buzz": clm = 3
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case i Mod 3 = 0
                ans = "Fizz": clm = 2
                Cells(i, clm) = IIf(talk = "", ans, talk)
            Case Else
                ans = i: clm = 1
                Cells(i, clm) = IIf(talk = "", ans, talk)
        End Select
        Cells(i, clm).Select
        If talk <> "" Then
            If talk <> ans Then
                Cells(i + 1, 1) = "はいダメーー"
                Exit Sub
            Else
                talk = ""
            End If
        End If
        If Int((10 - 1 + 1) * Rnd + 1) = 1 Then
           talk = RandRet(i + 1)
        End If
        Application.Wait [Now() + TimeValue("00:00:00.5")]
    Next
End Sub

Function RandRet(ByVal n As Long) As Variant
    Dim arr As Variant
    arr = Array(n, "Fizz", "Buzz", "FizzBuzz")
    RandRet = arr(WorksheetFunction.RandBetween(0, 3))
End Function
No one still commented. Please first comment.