transfer a value in a userform that can be referenced during a click event via commandbutton

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
My code correctly captures the value for the variable "IncidentRow", but I do not know how to call for that exact value within a commandbutton click event.

The value is originally captured via exiting a textbox:


VBA Code:
Dim FoundCell As Range
Dim IncidentCell As Range

'
VBA Code:
Private Sub txtIncidentID1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim Search As String
    Dim FoundCell As Range
    Dim IncidentCell As Range
' Lookup the "Incident ID Number" in two worksheets:  Sheet22 and in Sheet1.  Save that value as 'FoundCell' (for worksheet22) and as 'IncidentCell' (for worksheet1)
    Search = Me.txtIncidentID1.value
    Set FoundCell = Sheet22.Range("B:B").Find(Search, LookAt:=xlWhole, LookIn:=xlValues)
    Set IncidentCell = Sheet1.Range("A:A").Find(Search, LookAt:=xlWhole, LookIn:=xlValues)
    If Not FoundCell Is Nothing Then
        GetRecord FoundCell.Row
        UpdateRecord IncidentCell.Row
    Else
        MsgBox Search & Chr(10) & "Incident ID not found.", 48, "Not Found"
        Me.txtIncidentID1.value = ""
        Cancel = True
    End If
    On Error Resume Next
End Sub



And then I can confirm that it was indeed correctly found and captured via a MsgBox:

VBA Code:
Sub UpdateRecord(ByRef IncidentRow As Long)
    MsgBox IncidentRow
End Sub



Found.JPG

But, when I then go to use that variable (which should be 1136) in my Commandbutton code, it comes up as 'Zero':
NotCaptured.jpg

How can i save the variable as the 1136 value and then recall it within the separate commandbutton code when it is triggered? Thank you.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You could create a public type and save to it.
Edit - Probaly easier just to put that value in the Workbook somewhere so that it can be referenced throughout all of the subs in the project.
 
Upvote 0
Solution
You could create a public type and save to it.
Edit - Probaly easier just to put that value in the Workbook somewhere so that it can be referenced throughout all of the subs in the project.
Thanks. That was a good idea (and wasn't something I probably would of thought of.) thanks again! (y)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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