Can you check & advise on my userform code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
2,808
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,251
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,808
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,251
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,081,530
Messages
5,359,347
Members
400,524
Latest member
Excelbat

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top