Excel用户表单文本框事件

时间:2014-07-03 07:29:21

标签: excel vba textbox

我想编写一个比较两张纸中两列的程序。用户将输入他/她想要比较的工作表名称和范围。如果在两张纸中找到数据,则第一张纸将突出显示绿色的单元格,表示该值在另一张纸中可用。

当我运行代码时,它会给出运行时间'9'error: subscript is out of range

注意:当我通过在代码中输入工作表名称而不是使用文本框来运行代码时,它会成功运行程序。

 Private Sub FindBtn_Click()

 MsgBox (fromSheetTxtBox) ' it outputs the sheet name
'MsgBox (fromRangeFromTxtBox)
 'MsgBox (fromRangeToTxtBox)
'MsgBox (toSheetTxtBox)
'MsgBox (ToRangefromTxtBox)
'MsgBox (ToRangeToTxtBox)

'Dim toSheet As String
'Set toSheet = toSheetTxtBox.Value

Dim i As Integer
For i = 8 To 9331
    Set cell =       Worksheets(fromSheetTxtBox.Text).range("D8:D1427").Find(What:=Worksheets(toSheetTxtBox.Text).Cells(i, 2), lookat:=xlWhole)
If Not cell Is Nothing Then ' if jde cell value is found in tops then green jde cell
    Worksheets("toSheetTxtBox").Cells(i, 2).Interior.ColorIndex = 4

End If
Application.StatusBar = "Progress: " & i & " of 9331 " '& Format(i / 9331, "%")
Next i


End Sub

1 个答案:

答案 0 :(得分:0)

如果toSheetTxtBox TextBox ,请更改此行:

Worksheets("toSheetTxtBox").Cells(i, 2).Interior.ColorIndex = 4

到此:

Worksheets(toSheetTxtBox).Cells(i, 2).Interior.ColorIndex = 4