Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA初学者-运行时错误“9”下标超出范围_Vba_Excel - Fatal编程技术网

Excel VBA初学者-运行时错误“9”下标超出范围

Excel VBA初学者-运行时错误“9”下标超出范围,vba,excel,Vba,Excel,我正在尝试编写一个简单的宏,将日期格式mm/dd/yy更改为月份全名。这就是我在VBA中使用了几天的经验。我错过了这里的错误 Sub ChangeDate() Dim Last As Integer Last = Cells(Rows.Count, 1).End(xlUp).Row Dim Counter As Integer For Counter = 2 To Last Dim Month As String Month =

我正在尝试编写一个简单的宏,将日期格式mm/dd/yy更改为月份全名。这就是我在VBA中使用了几天的经验。我错过了这里的错误

Sub ChangeDate()

    Dim Last As Integer
    Last = Cells(Rows.Count, 1).End(xlUp).Row
    Dim Counter As Integer

    For Counter = 2 To Last
        Dim Month As String
        Month = Left(Worksheets("Sheet1").Cells(Counter, 2), 1)

        If Month = "7" Then
            Worksheets("Sheet1").Cells(Counter, 2).Value = "July"

        ElseIf Month = "8" Then
            Worksheets("Sheet1").Cells(Counter, 2).Value = "August"

        ElseIf Month = "9" Then
            Worksheets("Sheet1").Cells(Counter, 2).Value = "September"
        End If

    Next Counter

End Sub
尝试使用以下代码:

Sub ChangeDate()
    Dim Counter As Long
    Dim Last As Integer

    Worksheets("Sheet1").Select
    Last = Cells(Rows.Count, 1).End(xlUp).Row

    For Counter = 2 To Last
        Dim Month As String
        Month = Left(Worksheets("Sheet1").Cells(Counter, 1), 1)

        If Month = "7" Then
        Worksheets("Sheet1").Cells(Counter, 2).Value = "July"

        ElseIf Month = "8" Then
        Worksheets("Sheet1").Cells(Counter, 2).Value = "August"

        ElseIf Month = "9" Then
        Worksheets("Sheet1").Cells(Counter, 2).Value = "September"
        End If
    Next Counter
End Sub
此代码读取第1列中的日期,并将月份放入第2列。
建议使用用例语句而不是ElseIf。

将计数器声明为Long.worksheetsheets1.cellscenter,2.Value=formatworksheetsheets1.cellscenter,2.Value2,mmmm。。。或者只需更改工作表sheets1.rangeB2:B&last.numberformat=mmmm为什么需要使用代码?您可以使用MMM将格式单元格更改为自定义,以供将来参考,如果。。。如果。。。如果这是一个很好的选择案例语句的地方,则结束。