PDA

View Full Version : default text in



danovkos
04-28-2009, 05:38 AM
i try this question on other forum, but nobody there helps me.
http://www.excelforum.com/excel-programming/673435-default-text.html

can anyone help me with this here?

I want to have in my textbox1 always a default text (Text here). After clicking to this textbox it will clear it and it is possible to written and seach names. Ater clicking in other cell or clicking out out this textbox, the default text will appears in textbox again. How can i do it?

here is code:

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Dim i As Long
Dim cell As Range
Dim FirstCell As String
Range("J2").ClearContents
Rem Application.Wait waitTime

If TextBox1.Value = "" Then Exit Sub
If Len(TextBox1.Text) < 4 Then Exit Sub

With Worksheets("2008")
With .Range(.Range("e1"), .Range("e65536").End(xlUp))
Set cell = .Find(What:=TextBox1.Value, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not cell Is Nothing Then
FirstCell = cell.Address
Do
Range("J2").End(xlUp).Offset(1, 0).Value = cell.Value
Set cell = .FindNext(cell)
Loop While Not cell Is Nothing And cell.Address <> FirstCell
End If
End With
End With

With Worksheets("2007")
With .Range(.Range("a1"), .Range("a65536").End(xlUp))
Set cell = .Find(What:=TextBox1.Value, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not cell Is Nothing Then
FirstCell = cell.Address
Do
Range("J2").End(xlUp).Offset(1, 0).Value = cell.Value
Set cell = .FindNext(cell)
Loop While Not cell Is Nothing And cell.Address <> FirstCell
End If
End With
End With
With Worksheets("vývoj")
With .Range(.Range("f1"), .Range("f65536").End(xlUp))
Set cell = .Find(What:=TextBox1.Value, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not cell Is Nothing Then
FirstCell = cell.Address
Do
Range("J2").End(xlUp).Offset(1, 0).Value = cell.Value
Set cell = .FindNext(cell)
Loop While Not cell Is Nothing And cell.Address <> FirstCell
End If
End With
End With
With Worksheets("KK")
With .Range(.Range("f1"), .Range("f65536").End(xlUp))
Set cell = .Find(What:=TextBox1.Value, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not cell Is Nothing Then
FirstCell = cell.Address
Do
Range("J2").End(xlUp).Offset(1, 0).Value = cell.Value
Set cell = .FindNext(cell)
Loop While Not cell Is Nothing And cell.Address <> FirstCell
End If
End With
End With

End Sub

danovkos
04-28-2009, 05:39 AM
.

MaximS
04-28-2009, 06:24 AM
I want to have in my textbox1 always a default text (Text here).

Set in TextBox1 Properties >> Value as Text Here



After clicking to this textbox it will clear it and it is possible to written

you have 2 options (add that code to your text box):

A) Double Click on the text box

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.Text = ""
End Sub

B) Move over the text box


Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
TextBox1.Text = ""
End Sub

Cosmo
04-28-2009, 06:44 AM
Is this what you are looking for?:
Private enteredText As String
Private updateTextBox As Boolean
Private Const DEFAULT_TEXT As String = "Enter Text Here:"
Private Sub UserForm_Initialize()
Me.TextBox1.Value = DEFAULT_TEXT
updateTextBox = True
End Sub
Private Sub TextBox1_Change()
If updateTextBox Then
enteredText = Me.TextBox1.Value
End If
End Sub
Private Sub TextBox1_Enter()
Me.TextBox1.Value = enteredText
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
updateTextBox = False
Me.TextBox1.Value = DEFAULT_TEXT
updateTextBox = True
End Sub

MaximS
04-28-2009, 06:53 AM
then for searching i would add command button with code:


Private Sub CommandButton1_Click()
Dim x As String
Dim y As Long
Dim rng As Range
Dim Erro As String
Set rng = ActiveSheet.UsedRange
x = TextBox1.Text
If x <> "" Then
On Error Resume Next
y = WorksheetFunction.Match(x, rng, 0)
On Error GoTo Err:
Rows(y).Select
Exit Sub
End If
Err:
Erro = MsgBox("Match not found", vbOKOnly, _
"Given string does not exist in used range")
End Sub


and that to put "Text here" back (insert into worksheet code):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
TextBox1.Text = "Text Here"
End Sub

danovkos
04-28-2009, 11:24 AM
thanks a lot to all
i will try it tomorow

danovkos
04-28-2009, 11:13 PM
where should i insert this your code?
i tried it to insert up to my textbox code, which i have in microsoft object - sheet1, but it do nothing. I tried it also insert in module which i have in this workbook for others codes but without efect.


Is this what you are looking for?:
Private enteredText As String
Private updateTextBox As Boolean
Private Const DEFAULT_TEXT As String = "Enter Text Here:"
Private Sub UserForm_Initialize()
Me.TextBox1.Value = DEFAULT_TEXT
updateTextBox = True
End Sub
Private Sub TextBox1_Change()
If updateTextBox Then
enteredText = Me.TextBox1.Value
End If
End Sub
Private Sub TextBox1_Enter()
Me.TextBox1.Value = enteredText
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
updateTextBox = False
Me.TextBox1.Value = DEFAULT_TEXT
updateTextBox = True
End Sub

danovkos
04-28-2009, 11:24 PM
Thanks for advise, but i dont want to use button to start searching. I spent a lot of time for looking for code, which works as "live searching" (as i write character, it search). And a people from this and other forum spent with this a lot of time too. And i like it (maybe it searching slowly and it take a long time, but it works).

then for searching i would add command button with code:


Private Sub CommandButton1_Click()
Dim x As String
Dim y As Long
Dim rng As Range
Dim Erro As String
Set rng = ActiveSheet.UsedRange
x = TextBox1.Text
If x <> "" Then
On Error Resume Next
y = WorksheetFunction.Match(x, rng, 0)
On Error GoTo Err:
Rows(y).Select
Exit Sub
End If
Err:
Erro = MsgBox("Match not found", vbOKOnly, _
"Given string does not exist in used range")
End Sub


and that to put "Text here" back (insert into worksheet code):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
TextBox1.Text = "Text Here"
End Sub

danovkos
05-05-2009, 02:04 AM
Please, only one thing. What i need to add to my code, that if i have there default text, f.e. "text here" and i want, that after click on textbox it will select the dafault text.
I want after clicking in textbox direct writen name for searching. I don want to delet the default text.
Thx