Can you check & advise on my userform code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,838
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,279
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,838
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,279
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,082,253
Messages
5,364,052
Members
400,776
Latest member
JimmyLee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top