user5397924
user5397924

Reputation: 21

How to check for data in TextBox

I have a worksheet named "Input" with a Button (ActiveX) and a TextBox (ActiveX). I used VBA to check value of the TextBox when a user clicks the button, but the code cant find the textbox.

My code for the 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

Upvotes: 2

Views: 140

Answers (1)

synthaxe
synthaxe

Reputation: 95

If the values are still inside the textBox then you have to reference the object or the control. you can do it like this:

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

What this will do is display the value of textBox form control to cell A1. I know this is not what you are looking for but this is the simplest way of showing you how to reference userform objects or controls. You can find the name of a textBox control on a window like this:

Properties window of a TextBox in VBA Excel

You need to use the (Name) when referencing textboxes

Upvotes: 1

Related Questions