+ Reply to Thread
Results 1 to 10 of 10

"Object required" error in combo box/macro combination

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    "Object required" error in combo box/macro combination

    Hi,

    I'm trying to set up a command button that executes an action based on the combo box selection. The command button runs a plotting macro that is always the same, however, the combo box is used to select the worksheet which provides the data the plotting macro will apply to. All the data worksheets are laid out the same.

    The macro is set up so the data plotted is from the ActiveSheet, which is referenced to by the string strSheetName. I was trying to use the combobox selection to set strSheetName equal to the worksheet chosen, and then activate this worksheet before running the plotting macro.

    My problem is that I keep getting a compilation error: object required. The error shows up first in the line Set strSheetName As String. I tried initializing strSheetName as Public or Global, but Public had the same problem and Global wouldn't work for the string...this combo box code is in sheet20, whereas the macro is in a module (i hope that is okay).

    Here's my code so far:

    Formula: copy to clipboard

    Private Sub CBox1_Click()
    Dim strSheetName As String

    If CBox1 = FB1 Then
    Set strSheetName = Worksheets("FB1")
    End If
    If CBox1 = FB2 Then
    Set strSheetName = Worksheets("FB2")
    End If
    If CBox1 = FB3 Then
    Set strSheetName = Worksheets("FB3")
    End If
    If CBox1 = FB4 Then
    Set strSheetName = Worksheets("FB4")
    End IfWorksheets(strSheetName).Activate
    Application.Run ("GraphFB6")
    strSheetName = Empty

    End Sub



    Any help is greatly appreciated!

    Dan

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: "Object required" error in combo box/macro combination

    Dim strSheetName As Worksheet
    Don't dimension worksheets as strings. As well at the end of the macro
    Set strSheetName = Nothing
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  3. #3
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: "Object required" error in combo box/macro combination

    Thanks for the help, the code now gets passed the Object Required error. Unfortunately it gets caught up in a Runtime Error: Type mismatch at

    Formula: copy to clipboard
    Worksheets(strSheetName).Activate


    Any suggestions?

    Cheers

  4. #4
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: "Object required" error in combo box/macro combination

    Do I reference strSheetName differently if it's a Worksheet rather than a string (noobie)?

  5. #5
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: "Object required" error in combo box/macro combination

    Sorry missed that one - should be
    strSheetName.Activate

  6. #6
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: "Object required" error in combo box/macro combination

    Do I reference strSheetName differently if it's a Worksheet rather than a string (noobie)?
    That is right. If you want to make coding easier (and it is good programming practice) then you can dimension worksheets and refer to them more easily.
    Sub test()
    Dim wsK As Worksheet
    Set wsK = Worksheets("Results")
    wsK.Select
    Here you dimension the variable wsK as a worksheet and then you use the Set command to set the worksheet value. So the code above will set wsK to the worksheet named Results and select it.
    If you want you can use strings
    Dim shName As String
    shName = "Results"
    Worksheets(shName).Select
    Here the variable shName is equal to "Results" - then you can select the worksheet by using the string name to decide which sheet to show.
    If you are new to programming VBA you should have a read about dimensioning variables - there are some links in the Sticky on the first page of the forum http://www.excelforum.com/excel-prog...materials.html
    This is also a good link when starting out about the different types of variables you can dimension - http://www.ozgrid.com/VBA/variables.htm
    PS: Just so there is no confusion strSheetName is NOT a Keyword i.e. a VBA function/programming code - it is just a made up variable - it is in the form of what we call Camel Case and it is trying to be descriptive - it is good to use variable names that describe the type of data they might contain to make reading code easier - strSheetName - with Camel case the first letter is lower case then the next part of the variable name (Sheet) is capitalised and so is next part of variable name (Name) - strSheetName - it is just a variable that has been made up trying to show that the variable probably refers to a worksheet. It could have just as easily have been mySheetNameAsString - you can make the variable name anything you like however some Keywords are reserved - for example you could not use Set = 6 - Set is a keyword and cannot be used as a variable.
    Last edited by smuzoen; 07-05-2012 at 07:42 AM.

  7. #7
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: "Object required" error in combo box/macro combination

    Thanks, after changing that I was getting a runtime error "object variable or runtime variable not set". And the error occurred at:

    Formula: copy to clipboard
    SheetName.Activate

    at the bottom of all my IF/End if statements. Originally, I was going to call a macro in a module from this combo box code (which is in a sheet as a Microsoft Excel Object), but I tried just putting the macro in the bottom of the code as shown below. This doesn't work - nothing happens. When I go through line by line, there are no error messages, but it doesn't do anything. I'm wondering if the SheetName variable is not setting to a worksheet correctly, or if there's a variable types problem.

    Formula: copy to clipboard
    Private Sub ComboBox1_Click()

    Dim SheetName As Worksheet
    Dim lastdatapoint As Long

    If ComboBox1.Value = "FB1" Then
    Set SheetName = Worksheets("FB1")
    End If
    If ComboBox1.Value = "FB2" Then
    Set SheetName = Worksheets("FB2")
    End If
    If ComboBox1.Value = "FB3" Then
    Set SheetName = Worksheets("FB3")
    End If

    Application.ScreenUpdating = False

    With Worksheets("Plots")
    'delete any other chart on the sheet before proceding
    On Error Resume Next
    .ChartObjects.Delete
    End With

    With Worksheets("SheetName")
    lastdatapoint = Range("B" & Rows.Count).End(xlUp).Row
    .Shapes.AddChart.Select
    End With

    With ActiveChart
    .ChartType = xlLine
    .SetSourceData Worksheets(SheetName).Range("$R$3:$R" & lastdatapoint)
    .SeriesCollection(1).XValues = Worksheets(SheetName).Range("$B$3:$B" & lastdatapoint)
    .Location Where:=xlLocationAsObject, Name:="Plots"

    'x axis label spacing
    .Axes(xlCategory).TickMarkSpacing = 10
    .Axes(xlCategory).TickLabelSpacing = 10

    'axes titles
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature (C)"
    End With

    'make the graph area bigger
    With ActiveChart.Parent
    .Height = 325
    .Width = 500
    End With

    Application.ScreenUpdating = True

    Set SheetName = Nothing

    End Sub


    If you have any more thoughts as to why either of these problems are occurring please send them along!

    Thanks again,

    Dan

  8. #8
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: "Object required" error in combo box/macro combination

    Okay, above nothing was happening because of a stupid mistake:

    I had Private Sub ComboBox1_Click()

    instead of Private Sub CommandButton1_Click()

    After the change, I click the button and it goes to the worksheet selected from the drop down menu of the combobox, but no graphs are plotted

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: "Object required" error in combo box/macro combination

    I reckon
    Private Sub ComboBox1_Click()
    
    Dim SheetName            As Worksheet
    Dim cht                  As Chart
    Dim lastdatapoint        As Long
    
        Select Case ComboBox1.Value
            Case "FB1", "FB2", "FB3"
                Set SheetName = Worksheets(ComboBox1.Value)
            Case Else
                Exit Sub
        End Select
    
        Application.ScreenUpdating = False
    
        With SheetName
            lastdatapoint = .Range("B" & .Rows.Count).End(xlUp).Row
        End With
    
        With Worksheets("Plots")
    
            'delete any other chart on the sheet before proceding
    
            On Error Resume Next
    
            .ChartObjects.Delete
            ' bad idea to leave errors suppressed!
            On Error GoTo 0
            Set cht = .Shapes.AddChart.Chart
        End With
    
    
    
        With cht
            .ChartType = xlLine
            .SetSourceData SheetName.Range("$R$3:$R" & lastdatapoint)
            .SeriesCollection(1).XValues = SheetName.Range("$B$3:$B" & lastdatapoint)
            'x axis label spacing
            .Axes(xlCategory).TickMarkSpacing = 10
            .Axes(xlCategory).TickLabelSpacing = 10
            'axes titles
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temperature (C)"
    
            'make the graph area bigger
    
            With .Parent
    
                .Height = 325
    
                .Width = 500
    
            End With
        End With
        Application.ScreenUpdating = True
        Set SheetName = Nothing
    
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  10. #10
    Registered User
    Join Date
    07-03-2012
    Location
    Dubai
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: "Object required" error in combo box/macro combination

    Thanks for the code JP, I actually managed to figure out my mistake, but yours looks quite similar (I didn't test it though).

    The problem was that "SheetName" is a variable that represents a worksheet and isn't a worksheet. So while i have to reference my worksheets by writing "Worksheets("FB1").Range...." I have to write "SheetName.Range..." when referencing the variable.

    Thanks for all the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1