如何在TextBox中检查数据

时间:2015-10-07 12:56:27

标签: excel vba excel-vba

我有一个名为"输入"的工作表。使用Button(ActiveX)和TextBox(ActiveX)。当用户单击按钮时,我使用VBA检查TextBox的值,但代码无法找到文本框。

我的按钮代码:

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

1 个答案:

答案 0 :(得分:1)

如果值仍在textBox中,则必须引用该对象或控件。你可以这样做:

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

这样做会将textBox表单控件的值显示到单元格 A1。我知道这不是你想要的,但这是向你展示如何最简单的方法引用userform对象或控件。您可以在窗口中找到textBox控件的名称,如下所示:

Properties window of a TextBox in VBA Excel

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