VBA String in cell to check box value

Alexandra20

New Member
Joined
May 24, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to make this code work:

Private Sub commandB_Click()
If Cells(ActiveCell.Row, "G") = "Výtah" Then
realita.vytah.Value = True
End If

If Cells(ActiveCell.Row, "G") = "Konstrukce" Then
realita.konstrukce.Value = True
End If

............ other similar IF functions .............
userForm.Show
End Sub

It is supposed to open the user form with checkboxes named: "Výtah", "Konstrukce",... and many more, however, some of them should be already checked according to what is written in the active cell (in the column G).

BTW, the G column has the text string in this form - Výtah, Konstrukce, other words (it could be also blank or there could be only one option written, in that case, there is no comma).

Why is the code not working?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I have the following question, "realita" is the name of your userform?
If yes then try this:

VBA Code:
Private Sub commandB_Click()
  With Range("G" & ActiveCell.Row)
    If InStr(1, .Value, "Výtah") > 0 Then
      realita.vytah.Value = True
    End If
   
    If InStr(1, .Value, "Konstrukce") > 0 Then
      realita.konstrukce.Value = True
    End If
  End With
  '............ other similar IF functions .............
  'UserForm.Show
End Sub

Or For all your texts and checkbox

VBA Code:
Private Sub commandB_Click()
  Dim txt As Variant, cbx As Variant
  Dim i As Long
  txt = Array("Výtah", "Konstrukce", "other")
  cbx = Array("vytah", "konstrukce", "Other")
  '
  For i = 0 To UBound(txt)
    If InStr(1, Range("G" & ActiveCell.Row).Value, txt(i)) > 0 Then
      realita.Controls(cbx(i)).Value = True
    End If
  Next
End Sub
 
Last edited:
Upvote 0
I have the following question, "realita" is the name of your userform?
If yes then try this:

VBA Code:
Private Sub commandB_Click()
  With Range("G" & ActiveCell.Row)
    If InStr(1, .Value, "Výtah") > 0 Then
      realita.vytah.Value = True
    End If
  
    If InStr(1, .Value, "Konstrukce") > 0 Then
      realita.konstrukce.Value = True
    End If
  End With
  '............ other similar IF functions .............
  'UserForm.Show
End Sub

Or For all your texts and checkbox

VBA Code:
Private Sub commandB_Click()
  Dim txt As Variant, cbx As Variant
  Dim i As Long
  txt = Array("Výtah", "Konstrukce", "other")
  cbx = Array("vytah", "konstrukce", "Other")
  '
  For i = 0 To UBound(txt)
    If InStr(1, Range("G" & ActiveCell.Row).Value, txt(i)) > 0 Then
      realita.Controls(cbx(i)).Value = True
    End If
  Next
End Sub
yes, the userform is "realita" I forgot to mention that but thanks a lot, everything works wonderfully :)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,528
Members
449,316
Latest member
sravya

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