Results 1 to 21 of 21

Adapt Code for 1D Range Data Entry to Work With 2D Range

Threaded View

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Adapt Code for 1D Range Data Entry to Work With 2D Range

    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
    Attached Files Attached Files
    Last edited by AliGW; 11-01-2019 at 04:41 AM. Reason: Title improved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Get cells value in textbox userform
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2017, 02:13 AM
  2. Userform textbox to combine all cells comments
    By mariec_06 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-05-2016, 10:53 AM
  3. update cell/cells after updating a textbox on a userform
    By 40jmart1705 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2014, 03:07 PM
  4. [SOLVED] Placing the data from a range of cells into a userform textbox
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2014, 03:51 PM
  5. UserForm textbox data to empty cells
    By RLWells in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2012, 02:42 AM
  6. Linking Cells to Textbox on a Userform
    By Mpeters3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2011, 01:15 PM
  7. Copying userform data in textbox into cells
    By dowinterfor6 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-21-2011, 02:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1