Computing.Net > Forums > Office Software > VB Userform - Select worksheet from Combobox

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB Userform - Select worksheet from Combobox

Reply to Message Icon

Name: nic550
Date: May 25, 2009 at 08:02:07 Pacific
OS: Windows Vista
Subcategory: Microsoft Office
Comment:

I'm trying to create a simple useform on excel. I want to select the worksheet into which the data is entered based on the value selected from a combobox list. In this case it is a list of athlete's name ('cboAthlete.value). If 'Bob Smith' is selected then the worksheet selected would be 'Bob Smith' and all the data inputed there.

Below is the code. I am a complete novice and appreciate that this is probably very simple but would be grateful for any help. Thanks


Private Sub cmdAdd_Click()

Dim iRow As Long
Dim iPart As Long
Dim ws As Worksheet
Set ws = Worksheets("MaxJumps")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

iPart = Me.cboAthlete.ListIndex

'check for a part number
If Trim(Me.cboAthlete.Value) = "" Then
Me.cboAthlete.SetFocus
MsgBox "Please enter your full name"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.cboAthlete.Value
ws.Cells(iRow, 2).Value = Me.txtDate.Value
ws.Cells(iRow, 3).Value = Me.txtWeight.Value
ws.Cells(iRow, 4).Value = Me.txtCMJ.Value
ws.Cells(iRow, 5).Value = Me.txtSJ.Value
ws.Cells(iRow, 6).Value = Me.txtDJ.Value
ws.Cells(iRow, 7).Value = Me.txtContact.Value

'clear the data
Me.cboAthlete.Value = ""
Me.txtDate.Value = Format(Date)
Me.txtWeight.Value = ""
Me.txtCMJ.Value = ""
Me.txtSJ.Value = ""
Me.txtDJ.Value = ""
Me.txtContact.Value = ""
Me.cboAthlete.SetFocus

End Sub

Private Sub cmdClose_Click()
Unload Me
End Sub


Private Sub UserForm_Initialize()

Dim cAthlete As Range

Dim ws As Worksheet
Set ws = Worksheets("AthleteList")

For Each cAthlete In ws.Range("AthleteList")
With Me.cboAthlete
.AddItem cAthlete.Value
.List(.ListCount - 1, 1) = cAthlete.Offset(0, 1).Value
End With
Next cAthlete

Me.txtDate.Value = Format(Date)
Me.txtWeight.Value = ""
Me.txtCMJ.Value = ""
Me.txtSJ.Value = ""
Me.txtDJ.Value = ""
Me.txtContact.Value = ""
Me.cboAthlete.SetFocus

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use 'Close' button!"
End If
End Sub



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


compile error in hidden m... macro code to import tex...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: VB Userform - Select worksheet from Combobox

Reference Worksheet from cell value www.computing.net/answers/office/reference-worksheet-from-cell-value/9421.html

data from master worksheet to other sheets www.computing.net/answers/office/data-from-master-worksheet-to-other-sheets/9122.html

Increment ComboBox Linked Cel www.computing.net/answers/office/increment-combobox-linked-cel/9575.html