Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

using words in array when lock another textbox on form

0

Hi 

here is  the  orginal code

Private Sub TextBox1_Change()

  If InStr(1, TextBox1.Value, "SALES INVOICE NO", vbTextCompare) > 0 Then
    TextBox2.Locked = False
    TextBox3.Locked = True
    TextBox2.SetFocus
  ElseIf InStr(1, TextBox1.Value, "CASH PAID", vbTextCompare) > 0 Then
    TextBox2.Locked = True
    TextBox3.Locked = False
    TextBox2.SetFocus
  Else
    'And if there is no "expenses" or "revenue", the textboxes are locked
    'if you want unlocked, change to False
    TextBox2.Locked = True
    TextBox3.Locked = True

  End If
End Sub

my  goal  puttin multiple  words in  one  line into array to  avoid writing  many  lines  for  each word , then the  code  will become big in writing many  lines 

I try make  short  code like  this as in bold lines

Private Sub TextBox1_Change()
Dim ARR As Variant
ARR = Array("SALES INVOICE NO", "CASH PAID")

  If InStr(1, TextBox1.Value, ARR, vbTextCompare) > 0 Then
    TextBox2.Locked = False
    TextBox3.Locked = True
    TextBox2.SetFocus
  Else
    'And if there is no "expenses" or "revenue", the textboxes are locked
    'if you want unlocked, change to False
    TextBox2.Locked = True
    TextBox3.Locked = True

  End If
End Sub

but  gives  mismatch error in this line 

  If InStr(1, TextBox1.Value, ARR, vbTextCompare) > 0 Then

the  code will lock textbox3 based on words are filled in textbox1.

by  the  way  this  is  the  part  of  my  project , so  there is  no  commandbutton to  copy  to  sheet , before I  copy to  sheet .I  have  to  solve  this part !

any  help  to  chieve this ,please?

Answer
Discuss

Answers

0
Selected Answer

Hi Abdo

You can't use that line:

  If InStr(1, TextBox1.Value, ARR, vbTextCompare) > 0 Then

since the third argument of Instr must be a String (not a Variant or string array like your ARR).

You could either step through the array performing Instr in each string or use the If/ Else If approach in your first version of the code.

Revision 24 July 2023

In the attached revised file, I've replaced TextBox 1 with ComboBox1- this allows your array of words to be shown in a dropdown list.

The revised code relies on making ARR as a Public variable (so once initialised, it's visible to other procedures) and haveing Option Base 1 (so the first element of arrays ihas index 1 not the default of 0), for ease of understanding. These first bits are in bold and MUST precede the procedures.

The ComboBox (with ARR) makes it easy to choose what you want). The Select Case/ End Select is where you say what happens for each match. Please note that I've used the textbox property Enabled to "lock" the unwanted cells (rather than Locked, as you did)

The code below is commented for your benefit.

Option Base 1
Public ARR As Variant

Private Sub UserForm_Initialize()

    ' set up array
    ARR = Array("SALES INVOICE NO", "CASH PAID")
    ' populate combobbox drop down
    ComboBox1.List = ARR
    ComboBox1.SetFocus
End Sub


Private Sub ComboBox1_Change()

    Dim Mtch As String, n As Long

    Mtch = ""

    ' loop through array, looking for any matches
    For n = LBound(ARR) To UBound(ARR)
    ' if matched, set Mtch to True
        If InStr(1, ComboBox1.Value, ARR(n), vbTextCompare) > 0 Then
            ' stop on first match
            Mtch = ARR(n)
            Exit For
        End If
    Next n

    ' do actions for whatever was matched
    Select Case Mtch

      Case ARR(1) ' for SALES INVOICE
      TextBox2.Enabled = True
      TextBox2.SetFocus
      TextBox3.Enabled = False
      
      Case ARR(2) ' For CASH PAID
      TextBox2.Enabled = False
      TextBox3.Enabled = True
      TextBox3.SetFocus
        
       ' add other cases HERE
       
      Case Else ' if no match
      TextBox2.Enabled = False
      TextBox3.Enabled = False
    End Select

End Sub

You can add other elements to the array ARR (within the _Initialize routine) but then need to add another Case <<value>>  section to the Change procedure where I've indicated.

Hope this fixes your problem.

Discuss

Discussion

Hi John,
if I don't misundersood you , this  is  not  possible !
this is  disappointed  !
I  thought  there  is  another  way  to  make  the  code is short  becuase  I  have  many  words and  I will add new every time.
 this is very unfortunate!
Abdo M (rep: 16) Jul 24, '23 at 1:03 pm
Abdo. 

You just need to use a counter and a For.. . then/ Next to loop (or step) through the array and test with Instr (and set a flag / Exit For if there's a match). It's just 4 or 5 extra lines and your array Arr can be any size.
John_Ru (rep: 6142) Jul 24, '23 at 1:10 pm
Ando

Please see my revised answer and new file.
John_Ru (rep: 6142) Jul 24, '23 at 6:38 pm
Hi John,
your  right about  use combobox and  avoid error when write in textbox.
the code  works perfe
thank you
Abdo M (rep: 16) Jul 25, '23 at 6:20 am
Glad that worked for you. Thanks for selecting my Answer, Abdo.
John_Ru (rep: 6142) Jul 25, '23 at 6:41 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login