TOTM
TOTM

Reputation: 107

How to change a control's value on a userform with a Public Sub in Module 2

I'm trying to run a sub to change the value of a series of check boxes based on the value of the cell that the record is in. I have the Sub as public in a module because it's used multiple times in various subs within the userform itself. My code is below.

Public Sub ExceptionBoxCheck(ControlNameList As Range, BrevCodeList As Range, RecordValueRow As Range, BrevCodeColumn As Integer, AvailCheckBoxes As Integer)
    Dim ControlName As String, i As Integer, CodeCellContent As String
    Dim BrevCode As String

    For i = 0 To AvailCheckBoxes
        BrevCode = BrevCodeList.Offset(i, 0).Value
        CodeCellContent = RecordValueRow.Offset(0, BrevCodeColumn).Value
        ControlName = ControlNameList.Offset(i, 0).Value
        If InStr(CodeCellContent, BrevCode) <> 0 Then
            Me.Controls(ControlName).Value = True
        End If
    Next i
End Sub

The above sub is supposed to run through the list of control names and compare them to the current control name being checked with AvailCheckBoxes being the total number of boxes to be checked. The issue I'm running into is I'm getting an "Improper use of Me" runtime error and I can't seem to find anything that addresses this specific problem. The only thing I can think of is that there is an issue being caused by having this sub in Module2 rather than in the userform itself.

Upvotes: 0

Views: 341

Answers (1)

Variatus
Variatus

Reputation: 14383

Pass Me as a variable from the calling procedure in the Form's class module to the sub ExceptionBoxCheck. Then you can refer to all controls on that form via the assigned variable name.

Upvotes: 1

Related Questions