运行宏时出现运行时错误' 9':下标超出范围

时间:2018-03-06 07:38:55

标签: vba excel-vba excel

我是VBA的新手,我正在尝试为练习创建一个简单的销售数据集。我一直遇到运行时错误,这是代码:

Private Sub CommandButton1_Click()

Dim myWorksheet As Worksheet
Dim myFirstBlackRow As Long

Set myWorksheet = Worksheets("Sheet1")

With myWorksheet

myFirstBlackRow = .Cells.Find(What:="*", LookIn:=x1Formulas, LookAt:=x1Part, SearchOrder:=x1ByRows, SearchDirection:=x1Previous).Row + 1

With .Cells(myFirstBlackRow, 1)
    Select Case True
        Case OptionButton1.Value
            .Value = "Iphone"
        Case OptionButton2.Value
            .Value = "Samsung"
        Case OptionButton4.Value
            .Value = "Oppo"
        Case OptionButton3.Value
            .Value = "Huawei"
    End Select
End With

.Cells(myFirstBlackRow, 2).Value = Me.TextBox1.Value

End With

End Sub

提前感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

更改所有" 1" (1)到" l" (" L" S)

    myFirstBlackRow = .Cells.Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

这是将Option Explicit放在每个模块顶部的众多(非常多)理由之一