+ Reply to Thread
Results 1 to 4 of 4

How to combine 2 events with a click on user form?

  1. #1
    Registered User
    Join Date
    01-26-2016
    Location
    England
    MS-Off Ver
    2010
    Posts
    6

    How to combine 2 events with a click on user form?

    Help~`~ I'm a rookie for VBA.
    I have 2 tasks below and how to run them together with just one event (click)? Thank you guys for your help.

    Task 1:
    Private Sub lstEmployee_click()
    Dim i As Integer
    On Error Resume Next
    'find the selected list item
    i = Me.lstEmployee.ListIndex
    'add the values to the text boxes
    Me.txtStaffID.Value = Me.lstEmployee.Column(0, i)
    Me.txtSurname.Value = Me.lstEmployee.Column(1, i)
    Me.txtFirstName.Value = Me.lstEmployee.Column(2, i)
    Me.txtChineseName.Value = Me.lstEmployee.Column(3, i)
    Me.txtPosition.Value = Me.lstEmployee.Column(4, i)
    Me.txtDepartment.Value = Me.lstEmployee.Column(5, i)
    Me.txtGroup.Value = Me.lstEmployee.Column(6, i)
    Me.txtPhone.Value = Me.lstEmployee.Column(7, i)
    Me.txtEmail.Value = Me.lstEmployee.Column(8, i)

    'reset error handler
    On Error GoTo 0
    ------------------------------------------------------------------------------

    Task 2:
    Private Sub lstEmployee_click()
    Dim i As Integer
    Dim fPath As String
    Dim Pic As String
    'set the file path
    fPath = ThisWorkbook.Path & "\" & "Pictures"
    i = Me.lstEmployee.ListIndex
    On Error Resume Next
    'display the picture
    Me.Pic.Picture = LoadPicture(fPath & "\" & Me.lstEmployee.Column(0, i) & ".jpg")
    'If employee picture is not available
    If Err = 53 Then
    Me.Pic.Picture = LoadPicture(fPath & "\" & "NoPicture.jpg")
    End If
    'reset error handler
    On Error GoTo 0

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: How to combine 2 events with a click on user form?

    Just combine them into 1 sub.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    01-26-2016
    Location
    England
    MS-Off Ver
    2010
    Posts
    6

    Re: How to combine 2 events with a click on user form?

    Tried but failed

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: How to combine 2 events with a click on user form?

    This should work.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA Code for CTRL + Right Click to Bring Up User Form
    By nathandavies9 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-13-2017, 09:55 AM
  2. Dynamically Generated Form And Their Click Events
    By fenilbhoot in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2013, 10:13 AM
  3. intercept events, such as user-click->save
    By hansaaa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2013, 10:36 PM
  4. Combine an Input Box & a User Form
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-30-2011, 03:28 PM
  5. User form and list - click events
    By vn900 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2011, 12:02 AM
  6. Why is the scrollbar in a user form, by the first click on a button, scrolling?
    By Excel-Master in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-21-2010, 11:01 PM
  7. User Form TextBox Events
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-22-2010, 02:07 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