abhijeets1991
New Member
- Joined
- Mar 12, 2020
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
I have 25 text boxes named in the following manner on a UserForm
Name: id_[X]_box 1<= x <= 25
I am trying to write a program which can register a change event for all 25 boxes and populate the corresponding [DESCRIPTION] Labels.
Naming scheme for Description Labels Name: desc_[X]_label 1 <= X <= 25
When I program for a change event for just one box (i.e id_box_1), the functionality works fine.
When I try to implement for the 25 boxes with WithEvents and ClassModules, I am getting an error "Can't compile Module"
The form's Name: links
Please see relevant code snippets below
Code in the UserForm_Initialize function
Custom Class Module Code
Class Module Name: text_boxes_change
Kindly provide any insights.
Thank You
Name: id_[X]_box 1<= x <= 25
I am trying to write a program which can register a change event for all 25 boxes and populate the corresponding [DESCRIPTION] Labels.
Naming scheme for Description Labels Name: desc_[X]_label 1 <= X <= 25
When I program for a change event for just one box (i.e id_box_1), the functionality works fine.
When I try to implement for the 25 boxes with WithEvents and ClassModules, I am getting an error "Can't compile Module"
The form's Name: links
Please see relevant code snippets below
Code in the UserForm_Initialize function
VBA Code:
Private Sub UserForm_Initialize()
'Code to make single change event subroutine register for all id_[INT]_textboxes on links form
Dim ctrl As MSForms.Control
Dim text_box_handler As text_boxes_change
Set textBox_collection = New Collection
For Each ctrl In Me.controls
If TypeOf ctrl Is MSForms.TextBox Then
If Split(ctrl.Name, "_")(0) = "id" Then
Set text_box_handler = New text_boxes_change
Set text_box_handler.control_text_box = ctrl
textBox_collection.Add text_box_handler
End If
End If
Next ctrl
End Sub
Custom Class Module Code
Class Module Name: text_boxes_change
VBA Code:
Option Explicit
'This class assists in validating multiple text boxes on forms without having to define event funtions for each text box separately
'Global Constants
Const CASHFLOW As String = "Chart"
Const SETUP As String = "Settings"
Const INVOICE_STATUSES As String = "K13:K18"
Const TIME_UNITS As String = "L21:L24"
Const RELATION_TYPES As String = "M21:M25"
Const ACTIVITIES_COL As String = "T"
Const PROJ_START_ROW As Integer = 6
Public WithEvents MyTextBox As MSForms.TextBox
Public Property Set control_text_box(ByVal tb As MSForms.TextBox)
Set MyTextBox = tb
End Property
Public Sub BoxesGroup_Change()
'Setting default background color for the box
Me.MyTextBox.BackColor = RGB(255, 255, 255)
'Setting up Cashflow Worksheet Object
Dim cashflow_sheet As Worksheet
Set cashflow_sheet = Sheets("Chart")
'Finding lastrow with text inside the Sub-Activites column in Chart sheet
Dim lastrow As Integer
lastrow = cashflow_sheet.Cells(Rows.Count, ACTIVITIES_COL).End(xlUp).Row
'Range to represent the activities column in Chart worksheet
Dim activities_range As Range
Set activities_range = cashflow_sheet.Range(ACTIVITIES_COL & CStr(PROJ_START_ROW) & ":" & ACTIVITIES_COL & CStr(lastrow))
'A variable to store the user inputed value for id_box
Dim row_id As String
MsgBox ("salar")
row_id = Me.MyTextBox.value
If IsNumeric(row_id) = True Then
If CInt(row_id) >= PROJ_START_ROW And CInt(row_id) <= lastrow Then
Dim desc_caption As String
'SheetFunctions is a Module ; links_description is a Function that returns a string representing a cell address based on the rules of the workbook; functionality is tested and verified for this part
desc_caption = SheetFunctions.links_description(row_id)
If desc_caption <> "" Then
Me.MyTextBox.BackColor = RGB(255, 255, 255)
Me.desc_1_label.Caption = desc_caption
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
End Sub
Kindly provide any insights.
Thank You