VBA Error (UserForm)

walkerl8

Board Regular
Joined
Apr 30, 2013
Messages
201
Hi All,

Am encountering an error with the following code. Hopefully the code is pretty self-explanatory - I simply have a UserForm which until all fields are completed then the 'Add' function isn't available to click to paste the data. I am encountering the error within the bolded section of the code.


Appreciate anyone's recommendations.


Thanks,


Luke


Code:
Option Explicit
Private Sub AddButton_Click()
    If ValidateInput = "Failed" Then Exit Sub
    PostToFile
End Sub
Private Sub NewEntryButton_Click()
    If ValidateInput = "Failed" Then Exit Sub
    PostToFile
    ClearForm
End Sub
Private Sub ClearFormButton_Click()
    ClearForm
End Sub
Private Sub CancelButton_Click()
    Unload Me
End Sub
[B]Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    Dim Item As Range
    Set ws = ThisWorkbook.Sheets("Database")
    txbDate = Date
    'fill athlete combobox
    For Each Item In ws.Range("AthleteList")   'dynamic named range
        cmbAthlete.AddItem Item
    Next Item
    'fill exercise combobox
    For Each Item In ws.Range("ExerciseList")  'dynamic named range
        cmbExercise.AddItem Item
    Next Item
End Sub[/B]
Sub ClearForm()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.Name <> "txbDate" Then
            Select Case TypeName(ctrl)
            Case "TextBox"
                ctrl.Text = ""
            Case "ComboBox"
                ctrl.ListIndex = -1
            End Select
        End If
    Next
End Sub
Function ValidateInput()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        Select Case TypeName(ctrl)
        Case "TextBox"
            If ctrl.Text = "" Then
                ValidateInput = "Failed"
                MsgBox "Please enter " & Right(ctrl.Name, Len(ctrl.Name) - 3)
                Exit Function
            End If
        Case "ComboBox"
            If ctrl.ListIndex = -1 Then
                ValidateInput = "Failed"
                MsgBox "Please make selection " & Right(ctrl.Name, Len(ctrl.Name) - 3)
                Exit Function
            End If
        End Select
    Next
    ValidateInput = "Passed"
End Function
Sub PostToFile()
    Dim ws As Worksheet
    Dim nr As Long
    Dim reg As Range
    Set ws = ThisWorkbook.Sheets("Database")
    nr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
    ws.Cells(nr, 1) = CDate(txbDate)
    ws.Cells(nr, 3) = cmbAthlete
    ws.Cells(nr, 7) = cmbExercise
    ws.Cells(nr, 8) = Val(txbLoad)
    ws.Cells(nr, 9) = Val(txbReps)
    ws.Cells(nr, 11) = txbComments
End Sub
 
Last edited:
The following code found within 'Module1' is now highlighting as shown with the following error: 'Microsoft Visual Basic - Run-time error 1004: Method 'Range of Object_Worksheet failed'

Code:
Sub irc_mi_Click()
[B]UserForm.Show[/B]
End Sub
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Is your UserForm actually called UserForm and not something like UserForm1 etc.

The codes below are running fine for me with the UserForm called UserForm1.

Code:
Sub UserForminit()
   UserForm1.Show
End Sub

Code:
Private Sub UserForm_Initialize()
    Dim ws As Worksheet, txbDate As Date
    Dim Item As Range
    Set ws = ThisWorkbook.Sheets("Database")
    txbDate = Date
    'fill athlete combobox
    For Each Item In ws.Range("AthleteList")   'dynamic named range
        Me.AthleteInput.AddItem Item.Text
    Next Item
    'fill exercise combobox
    For Each Item In ws.Range("ExerciseList")  'dynamic named range
        Me.ExerciseInput.AddItem Item.Text
    Next Item
End Sub

file attached in link...

https://app.box.com/s/b2wsjfn27a931if66nsl96bpgudnkauw
 
Last edited:
Upvote 0
See last post I made for the file I was using.
 
Upvote 0
Your named ranges are on sheet Validations not Database.
Code:
Private Sub UserForm_Initialize()
    Dim ws As Worksheet, txbDate As Date
    Dim Item As Range
    Set ws = ThisWorkbook.Sheets("Database")
    txbDate = Date
    'fill athlete combobox
    For Each Item In Sheets("Validations").Range("AthleteList")   'dynamic named range
        Me.AthleteInput.AddItem Item.Text
    Next Item
    'fill exercise combobox
    For Each Item In Sheets("Validations").Range("ExerciseList")  'dynamic named range
        Me.ExerciseInput.AddItem Item.Text
    Next Item
End Sub
 
Upvote 0
Ah I see,

That seems to have brought the UserForm up.

The 'Date' box should automatically input the present date - do you know why that isn't happening now?

Appreciate your time,
 
Upvote 0
Code:
Private Sub UserForm_Initialize()
    Dim ws As Worksheet, txbDate As Date
    Dim Item As Range
    Set ws = ThisWorkbook.Sheets("Database")
    txbDate = Date
    Me.DateInput = txbDate
    'fill athlete combobox
    For Each Item In Sheets("Validations").Range("AthleteList")   'dynamic named range
        Me.AthleteInput.AddItem Item.Text
    Next Item
    'fill exercise combobox
    For Each Item In Sheets("Validations").Range("ExerciseList")  'dynamic named range
        Me.ExerciseInput.AddItem Item.Text
    Next Item
End Sub
 
Last edited:
Upvote 0
Mark,

That works perfectly now - I had to make a couple of changes to the post it area of the code which now seems to paste into the database. Last thing if you have any advice - if you bring the UserForm up on the file and click 'Add' - a message box appears, however if only 'Date' is filled in the message appears to say "leteInput" when it should say "AthleteInput" (or whatever textboox/combobox requires filling in).

Do you know why this is?

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,334
Members
449,155
Latest member
ravioli44

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