Userforms - Inconsistent Transfer from Cell Reference to TextBox

Don Carlos

New Member
Joined
Feb 1, 2016
Messages
5
(Excel 2010 - 32 Bit on Windows 7 Enterprise)

Hello,

I ask for your assistance with transferring data between an Excel Sheet and a userform. The userform is intended to both capture (write to an Excel sheet) and retrieve (from the same Excel sheet).

My problem is that, when I debug the code, cell reference contents will not consistently be captured into the textboxes. Usually, either the 'PrimaryRisk' is successful in reporting the information from the sheet and the 'SecondaryRisk' is not, or vice versa (in coming up Null).

Below is a sample of the code, which is purposely overly redundant to see if a method is consistently reliable. Typically, it will work or it won't for any of the methods in that set.


Code:
'PrimaryRisk
        With UserForm3.PrimaryRisk
            .Text = Sheet4.Cells(nSelectedRecord + 1, 3)
            .Value = Sheet4.Cells(nSelectedRecord + 1, 3)
        End With
        If UserForm3.PrimaryRisk.Text = "" Then
            UserForm3.PrimaryRisk.Value = Sheet4.Cells(nSelectedRecord + 1, 3)
        End If
        If UserForm3.PrimaryRisk.Text = "" Then
            tTempText = Sheet4.Cells.Cells(nSelectedRecord + 1, 3).Value
            UserForm3.PrimaryRisk.Text = tTempText
        End If
        If UserForm3.PrimaryRisk.Text = "" Then
            UserForm3.PrimaryRisk.Value = Sheet4.Cells(nSelectedRecord + 1, 3).Value
        End If
        UserForm3.PrimaryRiskText.Value = WorksheetFunction.VLookup(UserForm3.PrimaryRisk.Value, Range("RISKTABLE"), 2, False)
        'SecondaryRisk (Optional)
        If Sheet4.Cells(nSelectedRecord + 1, 4) = "" Or Sheet4.Cells(nSelectedRecord + 1, 4) = 0 Then
            UserForm3.SecondaryRisk.Text = ""
            UserForm3.SecondaryRiskText.Value = ""
        Else
            With UserForm3.SecondaryRisk
                .Text = Sheet4.Cells(nSelectedRecord + 1, 4).Value
                .Value = Sheet4.Cells(nSelectedRecord + 1, 4).Value
            End With
            UserForm3.SecondaryRisk.Value = Sheet4.Cells(nSelectedRecord + 1, 4)
            If UserForm3.SecondaryRisk.Text = "" Then
                tTempText = Sheet4.Cells.Cells(nSelectedRecord + 1, 3).Value
                UserForm3.SecondaryRisk.Text = tTempText
            End If
            If UserForm3.SecondaryRisk.Text = "" Then
                UserForm3.SecondaryRisk.Value = Sheet4.Cells(nSelectedRecord + 1, 3).Value
            End If
            UserForm3.SecondaryRiskText.Value = WorksheetFunction.VLookup(UserForm3.SecondaryRisk.Value, Range("RISKTABLE"), 2, False)
        End If

Notes:
1. I have tried to interchange between .value and .text without a change in results.
2. nSelectRecord is either 1 if not determined by the user or the record number selected by the user.
3. tTempText is usually successful in capturing the Excel sheet information, but is usually not successful in being written into the textbox.

Thank you in advance for your assistance.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The code is connected to the "edit" command button on the userform.

Interestingly, the userform is reliable for collecting information and pasting onto a sheet using the reverse order of the code shared in this post (userform.textbox -> sheet.cell()).
 
Upvote 0
I agree. The additional interesting thing is that if it works, they all work, say for primary risk. The redundant code does not overwrite a successful collection. But if it does not work on the first line, it will not correct itself in the redundant code.

My own thought is that the cause may be copying the textbox rather than recreating the textbox from the toolbox. But this is just a thought.
 
Upvote 0
I did find a solution to my problem. I was able to reliably capture information from a spreadsheet tab into the userform using ".ControlSource" property and referencing unique cell reference for each textbox and listbox. An example is:

Code:
UserForm3.CostTimeFrame.ControlSource = "Reference!$Q$3"
.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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