Make only PART of a textbox read only?

swimmerhair

New Member
Joined
Apr 15, 2015
Messages
24
Hey everyone,

I've used this site a lot and am looking for a particular problem that I can't seem to find an answer to. I have a textbox that I have in my userform and am looking to autopopulate the first line with the current date (Have no trouble doing this). I am also looking to make that string of text read-only/un-editable. Any suggestions?

Thanks in Advance
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I've used this site a lot and am looking for a particular problem that I can't seem to find an answer to. I have a textbox that I have in my userform and am looking to autopopulate the first line with the current date (Have no trouble doing this). I am also looking to make that string of text read-only/un-editable. Any suggestions?
At the moment, I am locked out from being able to place a TextBox on a user form (I keep getting an "Invalid argument" error when I try... I am researching the fix for the problem); however, this how it would be done for an ActiveX TextBox on a worksheet, the code should be similar if not exact...
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If TextBox1.SelStart < InStr(TextBox1.Text, vbLf) - 1 Then KeyCode = 0
End Sub
 
Upvote 0
At the moment, I am locked out from being able to place a TextBox on a user form (I keep getting an "Invalid argument" error when I try... I am researching the fix for the problem); however, this how it would be done for an ActiveX TextBox on a worksheet, the code should be similar if not exact...
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If TextBox1.SelStart < InStr(TextBox1.Text, vbLf) - 1 Then KeyCode = 0
End Sub
I finally got on a working computer... the above code is correct, however, it needs to be modified to let a person be able to arrow down off of the first line in case they accidentally arrow up to it (they can always click on a lower row to get off of it). Given that, use this event code for the TextBox (named TextBox1 for this example) and the user will not be able to type anything on the first line of text...

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If TextBox1.SelStart < InStr(TextBox1.Text, vbNewLine) And KeyCode <> 40 Then KeyCode = 0
End Sub
 
Upvote 0
Thank you Rick! Your code helped a lot, but on further trial and error (I had a specific amount of characters that could NOT be deleted) I came up with this solution:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'sub to make first line of text todays date as read-only
If (Textbox1.SelStart <= 10 And KeyCode = 8) = True Then
KeyCode = 0
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top