This code allows editing of cells via userform textboxes.
Class modules are used to transfer contents as you type.
Most has been developed by others.
A selection is made and the userform is populated when the macro is run. There is a differentiator ... the order of showing textboxes is by processing cells Down then Across, which is not the default, the default is Across then Down.
Up to this point, it works.
When it comes to editing the cell using a textbox, the wrong cell is changed (except for first and last cells). The link between the cell and textbox is still the default.
Please advise fix.
CLASS MODULE clsBoxes
Option Explicit
Public WithEvents TextBoxGo As MSForms.TextBox
Private cDelegateHold As clsDelegate
Public Property Set cDelegate(value As clsDelegate)
Set cDelegateHold = value
End Property
Private Sub TextBoxGo_Change() 'When a textbox changes
cDelegateHold.PassControl TextBoxGo
End Sub
CLASS MODULE clsDelegate
Option Explicit
Public Event TextBoxGoChanged(TextBoxGo As MSForms.TextBox)
Public Sub PassControl(TextBoxGo As MSForms.TextBox)
RaiseEvent TextBoxGoChanged(TextBoxGo)
End Sub
CLASS MODULE clsTyping
Option Explicit
Private WithEvents TextBoxTyping As MSForms.TextBox
Public Property Set Control(TextBoxTypingArgument As MSForms.TextBox)
Set TextBoxTyping = TextBoxTypingArgument
End Property
Private Sub TextBoxTyping_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call UserForm1.TransferAtKeyUp(TextBoxTyping.SelLength, KeyCode)
End Sub
FORM CODE
Option Explicit
Private WithEvents MyApplication As Excel.Application
Public WithEvents cDelegate As clsDelegate
Private TextBoxes As Collection
Private mActiveTextBox As MSForms.TextBox
Dim TextBoxCollection As Collection
'INITIALIZE
Private Sub UserForm_Initialize()
'General
Set cDelegate = New clsDelegate
Call FormCreation
'Typing
Dim ctrl As MSForms.Control
Dim obj As clsTyping
Set TextBoxCollection = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.TextBox Then
Set obj = New clsTyping
Set obj.Control = ctrl
TextBoxCollection.Add obj
End If
Next ctrl
Set obj = Nothing
End Sub
'ACTIVATE
Private Sub UserForm_Activate()
ButtonExit.SetFocus
End Sub
'CREATE FORM
Private Sub FormCreation()
Dim cEvents As clsBoxes
Dim Box As MSForms.TextBox
Dim BoxTop As Long, BoxHeight As Long, BoxLeft As Long, BoxWidth As Long, BoxGap As Long
Dim BoxName As String
Dim RangeColumn As Range
Dim cell As Range
Dim Index As Long
'Introduction
Set MyApplication = Application
BoxHeight = 24: BoxTop = 0: BoxLeft = 0: BoxWidth = 388: BoxGap = 0
Index = 1
If TextBoxes Is Nothing Then
Set TextBoxes = New Collection
End If
'Create textboxes
For Each RangeColumn In Selection.Columns 'To change processing by columns (default is by rows)
For Each cell In RangeColumn.Cells
If Index > TextBoxes.Count Then
Set cEvents = New clsBoxes
Set cEvents.cDelegate = cDelegate
BoxName = "TextBox" & Index
Set Box = Me.TextBoxFrame.Controls.Add("Forms.Textbox.1", BoxName, True)
Set cEvents.TextBoxGo = Box
TextBoxes.Add cEvents
End If
With Box
.Height = BoxHeight: .top = BoxTop: .Left = BoxLeft: .width = BoxWidth
.Font.Size = 12
.Text = cell.Formula
End With
Index = Index + 1
BoxTop = BoxTop + BoxHeight + BoxGap
Debug.Print BoxName & " = " & cell
Next cell
Next RangeColumn
'Size form
Me.Height = BoxTop + 148 'TRIAL & ERROR
TextBoxFrame.Height = BoxTop
End Sub
'RECEIVING EVENT
Private Sub cDelegate_TextBoxGoChanged(TextBoxGo As MSForms.TextBox)
Set mActiveTextBox = TextBoxGo
End Sub
'TYPING IN TEXTBOXES
Public Sub TransferAtKeyUp(SelectionCount As Long, ByVal KeyCode As MSForms.ReturnInteger)
Dim TextBox As MSForms.TextBox
Dim cell As Range
Dim Index As Long
Index = 1
For Each cell In MyApplication.Selection
If TextBoxes(Index).TextBoxGo.Name = mActiveTextBox.Name Then
With mActiveTextBox 'Transfer to cell
cell.value = mActiveTextBox.value
cell.value = mActiveTextBox.value
End With
End If
Index = Index + 1
Next
End Sub
'EXIT
Private Sub ButtonExit_Click()
Unload Me
End Sub
Bookmarks