做到问题直到循环

时间:2016-12-22 14:31:15

标签: excel vba excel-vba

我一直在看下面代码的输出,我看不出它有什么问题 - 过去几周我一直在看它,但仍然看不出它有什么问题!< / p>

代码如下:

Sub Macro()
Dim i As Long
Dim sString As String

Do Until i = 27
    If i = 0 Then
        sString = sString & "H"
    ElseIf i = 26 Then
        sString = sString & "!"
    ElseIf i = 25 Then
        sString = sString & "y"
    ElseIf i = 24 Then
        sString = sString & "d"
    ElseIf i = 23 Then
        sString = sString & "o"
    ElseIf i = 22 Then
        sString = sString & "b"
    ElseIf i = 21 Then
        sString = sString & "y"
    ElseIf i = 20 Then
        sString = sString & "r"
    ElseIf i = 19 Then
        sString = sString & "e"
    ElseIf i = 18 Then
        sString = sString & "v"
    ElseIf i = 17 Then
        sString = sString & "e"
    ElseIf i = 16 Then
        sString = sString & " "
    ElseIf i = 15 Then
        sString = sString & "s"
    ElseIf i = 14 Then
        sString = sString & "s"
    ElseIf i = 13 Then
        sString = sString & "a"
    ElseIf i = 12 Then
        sString = sString & "m"
    ElseIf i = 11 Then
        sString = sString & "t"
    ElseIf i = 10 Then
        sString = sString & "i"
    ElseIf i = 9 Then
        sString = sString & "r"
    ElseIf i = 8 Then
        sString = sString & "h"
    ElseIf i = 7 Then
        sString = sString & "C"
    ElseIf i = 6 Then
        sString = sString & " "
    ElseIf i = 5 Then
        sString = sString & "y"
    ElseIf i = 4 Then
        sString = sString & "p"
    ElseIf i = 3 Then
        sString = sString & "p"
    ElseIf i = 2 Then
        sString = sString & "a"
    End If
i = i + 1
Loop

MsgBox sString, vbCritical

End Sub

2 个答案:

答案 0 :(得分:5)

简单的拼写错误,请改用:

Sub Macro()
Dim i As Long
Dim sString As String

Do Until i = 27
    If i = 0 Then
        sString = sString & "H"
    ElseIf i = 26 Then
        sString = sString & "!"
    ElseIf i = 25 Then
        sString = sString & "y"
    ElseIf i = 24 Then
        sString = sString & "d"
    ElseIf i = 23 Then
        sString = sString & "o"
    ElseIf i = 22 Then
        sString = sString & "b"
    ElseIf i = 21 Then
        sString = sString & "y"
    ElseIf i = 20 Then
        sString = sString & "r"
    ElseIf i = 19 Then
        sString = sString & "e"
    ElseIf i = 18 Then
        sString = sString & "v"
    ElseIf i = 17 Then
        sString = sString & "e"
    ElseIf i = 16 Then
        sString = sString & " "
    ElseIf i = 15 Then
        sString = sString & "s"
    ElseIf i = 14 Then
        sString = sString & "a"
    ElseIf i = 13 Then
        sString = sString & "m"
    ElseIf i = 12 Then
        sString = sString & "t"
    ElseIf i = 11 Then
        sString = sString & "s"
    ElseIf i = 10 Then
        sString = sString & "i"
    ElseIf i = 9 Then
        sString = sString & "r"
    ElseIf i = 8 Then
        sString = sString & "h"
    ElseIf i = 7 Then
        sString = sString & "C"
    ElseIf i = 6 Then
        sString = sString & " "
    ElseIf i = 5 Then
        sString = sString & "y"
    ElseIf i = 4 Then
        sString = sString & "p"
    ElseIf i = 3 Then
        sString = sString & "p"
    ElseIf i = 2 Then
        sString = sString & "a"
    End If
i = i + 1
Loop

MsgBox sString, vbCritical

End Sub

答案 1 :(得分:3)

或者你可以将它浓缩一些......

Sub Macro()
Dim i As Long
Dim sString As String

i = 0
Do Until i = 27
  If i = 0 Then
    sString = sString & "H"
  ElseIf i = 26 Then
    sString = sString & "!"
  ElseIf i = 25 Or i = 21 Or i = 5 Then
    sString = sString & "y"
  ElseIf i = 24 Then
    sString = sString & "d"
  ElseIf i = 23 Then
    sString = sString & "o"
  ElseIf i = 22 Then
    sString = sString & "b"
  ElseIf i = 20 Or i = 9 Then
    sString = sString & "r"
  ElseIf i = 19 Or i = 17 Then
    sString = sString & "e"
  ElseIf i = 18 Then
    sString = sString & "v"
  ElseIf i = 16 Or i = 6 Then
    sString = sString & " "
  ElseIf i = 15 Or i = 11 Then
    sString = sString & "s"
  ElseIf i = 14 Or i = 2 Then
    sString = sString & "a"
  ElseIf i = 13 Then
    sString = sString & "m"
  ElseIf i = 12 Then
    sString = sString & "t"
  ElseIf i = 10 Then
    sString = sString & "i"
  ElseIf i = 8 Then
    sString = sString & "h"
  ElseIf i = 7 Then
    sString = sString & "C"
  ElseIf i = 4 Or i = 3 Then
    sString = sString & "p"
  End If
  i = i + 1
Loop

MsgBox sString, vbCritical

End Sub