Can you check & advise on my userform code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
The code im using is shown below.
I have used a code from another userform where it had two comboboxes & now wish to use it on a worksheet where there is only one combobox.

Some info for you.
Worksheet called SUMMARY SHEET
Year for combobox is on worksheet called LIST which is in the same workbook.

I open the workbook then code checks cell A2 of which at present is empty so my userform is shown.
I click on the one drop down aroow on the combobox & make a year selection.

This is where i am now stuck.
After making the year selection i then press the commandbutton & it should then enter my selected year in cell A2
I actually see a RTE Could Not Find The Specified Object.

This is where i need some help plea, check / edit the code so the selected year is entered in the cell A2

My userfomr consists of Combobox with year range.
A Transfer Button to send that year range to the worksheet cell A2
A close userform button

Many thanks if you could assist



Code:
Private Sub TransferButton_Click()
    Dim i As Integer
    Dim ControlsArr As Variant, ctrl As Variant
    Dim x As Long
    For i = 1 To 1
       With Me.Controls("ComboBox" & i)
            If .ListIndex = -1 Then
                MsgBox "MUST SELECT A YEAR", 48, "SUMMARY SHEET YEAR MESSAGE"
                .SetFocus
                Exit Sub
            End If
        End With
    Next i
    
    ControlsArr = Array(Me.ComboBox1)
    
    With ThisWorkbook.Worksheets("SUMMARY SHEET")
        For i = 0 To UBound(ControlsArr)
         Select Case i
            Case 1, 2, 4
               .Cells(1, i + 2) = IIf(IsNumeric(ControlsArr(i)), Val(ControlsArr(i)), ControlsArr(i))
            Case Else
               .Cells(1, i + 2) = ControlsArr(i)
               ControlsArr(i).Text = ""
         End Select
    Next i


    End With
    
    ActiveWorkbook.Save
    Application.ScreenUpdating = True
    
    MsgBox "YEAR HAS BEEN INSERTED ON WORKSHEET", vbInformation, "SUCCESSFUL MESSAGE SUMMARY SHEET"
    Unload SUMMARYSHEETYEAR
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi,
looking at your code I am guessing that you have adapted it from another procedure as you have an array for controls with just one control?

If all you have on your form is just combobox to select a year then try this update & see if does what you want

Code:
Private Sub TransferButton_Click()
    Dim wsSummary As Worksheet
    Set wsSummary = ThisWorkbook.Worksheets("SUMMARY SHEET")




    With Me.ComboBox1
        If .ListIndex = -1 Then
            MsgBox "MUST SELECT A YEAR", 48, "SUMMARY SHEET YEAR MESSAGE"
            .SetFocus
            Exit Sub
        Else
            wsSummary.Cells(2, 1).Value = Val(.Text)
        End If
    End With


    
    ActiveWorkbook.Save


    
    MsgBox "YEAR HAS BEEN INSERTED ON WORKSHEET", vbInformation, "SUCCESSFUL MESSAGE SUMMARY SHEET"
    Unload Me
End Sub

Dave
 
Upvote 0
Hi,

I select 2019 - 2020 from the drop down list.
It transfers fine to the worksheet & in the correct place BUT it only shows 2019
 
Upvote 0
Hi,

I select 2019 - 2020 from the drop down list.
It transfers fine to the worksheet & in the correct place BUT it only shows 2019


You said in your first post that you were only selecting a year which gave the impression of a single numeric value in your combobox.


change this line

Code:
wsSummary.Cells(2, 1).Value = Val(.Text)

for this

Code:
wsSummary.Cells(2, 1).Value = .Text

Dave
 
Last edited:
Upvote 0
Sorry for confusion.
The new edit works fine.

have a nice day
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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