Can you check & advise on my userform code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,620
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,157
Office Version
2013
Platform
Windows
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
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,620
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,157
Office Version
2013
Platform
Windows
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:

Forum statistics

Threads
1,078,486
Messages
5,340,616
Members
399,387
Latest member
amrita34

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top