How To Disable Editing In Textbox To Prevent User Inputting In Excel?


Powerful capabilities for data administration, analysis, and visualisation are provided by Microsoft Excel. For the sake of maintaining data integrity and preventing unintentional changes, you might occasionally want to limit user input in particular cells or textboxes. In this lesson, we'll go through how to prevent users from altering textboxes so they can only see the content without making any changes. You can protect your data and produce more secure Excel worksheets by using this strategy. So, let's get started and discover how to stop users from entering data into Excel textboxes!

Disable Editing In Textbox To Prevent User Inputting

Here we will first insert an activex controls checkbox, then add the VBA code to it to complete the task. So let us see a simple process to know how you can disable editing in text boxes to prevent user input in Excel.

Step 1

Consider an Excel sheet where you have textboxes similar to the below image.

First, click on "Developer" and insert the "ActiveX Control" checkbox. To do so, click on insert, then click on the checkbox and draw.

Developer > Insert > Checkbox > Draw.

Step 2

Then right−click on the check box and select "View Code" to open the VBA application. Then replace the existing code with the below code−

Right click > View code > Replace code.

Code

Private Sub CheckBox1_Click()
   Dim xTextBox As OLEObject
   Dim xFlag As Boolean
   Dim I As Long
   Dim xArr
   xArr = Array("TextBox1", "TextBox2", "TextBox3")
   xFlag = True
   If Me.CheckBox1 Then xFlag = False
   For Each xTextBox In ActiveSheet.OLEObjects
      If TypeName(xTextBox.Object) = "TextBox" Then
         For I = 0 To UBound(xArr)
            If xTextBox.Name = xArr(I) Then
               xTextBox.Enabled = xFlag
            End If
         Next
      End If
   Next
End Sub

In the code, CheckBox1 is the name of your inserted checkbox. And TextBox1, TextBox2, and TextBox3 are the textboxes you will disable editing in the worksheet. Please change them as you need.

Step 3

Then save the code and close the VBA application. From now on, when the checkbox is checked, the editing will be disabled.

Alt + Q > Check box.

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can disable editing in a textbox to prevent user input in Excel to highlight a particular set of data.

Updated on: 11-Jul-2023

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements