Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 12-15-2015, 10:00 PM
Stewie840 Stewie840 is offline Using combobox to pass data from an Excel spread sheet to text boxes on userform Windows 7 32bit Using combobox to pass data from an Excel spread sheet to text boxes on userform Office 2010 32bit
Novice
Using combobox to pass data from an Excel spread sheet to text boxes on userform
 
Join Date: Dec 2015
Posts: 8
Stewie840 is on a distinguished road
Default

This is the code I have come up with so far but I get a runtime error if I start typing something that is not in the list.

Code:
 
Private Sub UserForm_Initialize()
 
Dim xlApp As Object
Dim xlWb As Object
Dim xlSht As Object
Dim lastRow As Integer
Dim i As Long
 
'Centre user form in Application
Me.StartUpPosition = 0
Me.Top = Application.Top + (Application.Height / 2) - (Me.Height / 2)
Me.Left = Application.Left + (Application.Width / 2) - (Me.Width / 2)
 
'Populate Employee Details from Excel file
Set xlApp = CreateObject("Excel.Application")
 
Set xlWb = xlApp.Workbooks.Open '("Path\Employees.xlsx")
Set xlSht = xlWb.Worksheets(1)
 
cboID.ColumnCount = 4
 
With xlSht
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
 
With Me.cboID
For i = 2 To lastRow
.AddItem xlSht.Cells(i, 1)
.List(.ListCount - 1, 1) = xlSht.Cells(i, 2)
.List(.ListCount - 1, 2) = xlSht.Cells(i, 3)
.List(.ListCount - 1, 3) = xlSht.Cells(i, 4)
Next i
End With
 
xlApp.DisplayAlerts = False
xlWb.Close
Set xlSht = Nothing
Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
 
Private Sub cboID_Change()
txtFName.Text = Me.cboID.Column(1)
txtGName.Text = Me.cboID.Column(2)
txtDOB.Text = Me.cboID.Column(3)
End Sub
It works if the user starts typing the correct numbers into the cboID.

If they do get a run-time error I just want it to reset and try again.

I am still a novice at all this but have been trolling the internet for days for a solution. Once again I appreciate your help.

Cheers

Stewie
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of Combobox in other userform Vibov Excel Programming 4 11-19-2015 04:12 AM
Using combobox to pass data from an Excel spread sheet to text boxes on userform Pass combobox content to header wpryan Word VBA 3 07-17-2015 01:44 AM
Using combobox to pass data from an Excel spread sheet to text boxes on userform How To Open an Macro From 2003 Excel in 2013 Excel Spread Sheet? ADubin Excel Programming 3 02-08-2015 04:57 AM
Mail merge, text boxes, and two documents per sheet! MelissaAnnie Mail Merge 2 09-24-2014 03:46 PM
Condensing a spread sheet hawkeyefxr Excel 4 08-22-2012 05:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft