如何检查文本框中的数据

我有一个名为“input”与一个button(ActiveX)和一个文本框(ActiveX)的工作表。 我用VBA来检查文本框的值,当用户点击button,但代码不能find文本框。

我的button的代码:

Sub Toevoegen() Dim invoerenws As Worksheet Dim overzichtws As Worksheet Dim nextRow As Long Dim oCol As Long Dim myRng As Range Dim myCopy As String Dim myCell As Range 'cells to copy from Input sheet - some contain formulas myCopy = "TextBox1" Set invoerenws = Worksheets("invoeren") Set overzichtws = Worksheets("overzicht") With overzichtws nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row End With With invoerenws Set myRng = .Range(myCopy) If Application.CountA(myRng) <> myRng.Cells.Count Then MsgBox "Please fill in all the cells!" Exit Sub End If End With With overzichtws With .Cells(nextRow, "A") .Value = Now .NumberFormat = "mm/dd/yyyy hh:mm:ss" End With .Cells(nextRow, "B").Value = Application.UserName oCol = 3 For Each myCell In myRng.Cells overzichtws.Cells(nextRow, oCol).Value = myCell.Value oCol = oCol + 1 Next myCell End With 'clear input cells that contain constants With invoerenws On Error Resume Next With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants) .ClearContents Application.GoTo .Cells(1) ', Scroll:=True End With On Error GoTo 0 End With End Sub 

如果值仍然在文本框内,则必须引用对象或控件。 你可以这样做:

 textBox.value = worksheet.cells(1,1) 

这将做的是显示textBox窗体控件的值到单元格A1。 我知道这不是你正在寻找,但这是告诉你如何引用用户窗体对象或控件的最简单的方法。 你可以在这样的窗口中find一个文本框控件的名字:

VBA Excel中TextBox的属性窗口

引用文本框时,您需要使用(名称)