Runtime error 380 when trying to set RowSource property of ComboBox in VBA in Excel 2010 Pro x64

Der Hexer

New Member
Joined
Nov 7, 2013
Messages
12
Hello everyone,
I have a problem and I hope someone can help me with this. In the first place the problem sounds like one of the typical standard mistakes but I think in this case it is different.
I will try to explain, using an example what the problem is:

  • I have two worksheets "Tests" and "Definitions". In the "Definitions"-Worksheet I created three columns, each containing different physical units.
  • The names of these ranges are stored in the workbook space.
  • Then I have a small table containing these three names: "Pressure, Temperature, Force"
  • On the worksheet "Tests" I have a small table with two cells that use data validation.
  • The first cells data validation is connected to the table containing the range names
  • The second ones data validation is linked to the first cell by the INDIRECT macro.
Everything works fine in that worksheet. The user can pick the type of physical quantity (e.g. Pressure) in the first cell and the list of the second cell is than populated with pressure units.
Now I tried to use this table as base for my user form. So I placed two ComboBoxes, namely "ComboBox1" and "ComboBox2" on that form. I than initialized the ComboBoxes as follows:

Code:
Private Sub UserForm_Initialize()
  [SIZE=2]'if the Worksheet wasn't set yet, than do it now[/SIZE]
  If WS Is Nothing Then
    Set WS = Worksheets("Tests")
  End If

  'set the RowSource as well as the ControlSource properties here
  Me.ComboBox1.RowSource = WS.Cells(4, 3).Validation.Formula1
  Me.ComboBox1.ControlSource = "'" + WS.Name + "'!" + WS.Cells(4, 3).Address
  Me.ComboBox2.ControlSource = "'" + WS.Name + "'!" + WS.Cells(5, 3).Address
End Sub
The Variable WS is declared globally on top of the module via
Code:
Dim WS As Worksheet

ComboBox1 is filled with "Pressure, Temperature, Force".
When the user selects one of these entries than ComboBox2 should be populated with the appropriate list of units:
Code:
Private Sub ComboBox1_Change()
  If WS Is Nothing Then Exit Sub

  On Error Resume Next

  'Write the selected value of the ComboBox1 directly into the target cell
  'to ensure, that ComboBox2 displays the appropriate list for that value NOW
  WS.Cells(4, 3).Value = ComboBox1.Text

  'reset the Err.Number
  Err.Number = 0

  'Try to set the RowSource property. In that case the Formula of the validation
  'uses an Excel-macro called "INDIRECT"
  Me.ComboBox2.RowSource = WS.Cells(5, 3).Validation.Formula1 'Often (but not always) Runtime Error 380 - but why?

  'If an error was raised than display the message here:
  If Not (Err.Number = 0) Then
    Call MsgBox(Err.Description, vbOKOnly + vbCritical, "Run-time-Error " + CStr(Err.Number))
  End If
End Sub
Unfortunately ComboBox2 isn't filled with the list of units according to the selection of ComboBox1, but an Error is raised instead:
Run-time error '380': Could not set the RowSource property

The WS.Cells(5, 3).Validation.Formula1 is defined as =INDIRECT($C$4).

So can someone tell me what the reason for that error is, because I think that the validation formula is correct so far.

Thank You in advance.

System specifications:
MS Windows 7 Ultimate x64
MS Excel Professional Plus 2010 (Version 14.0.7106.5003, x64)
 
It's not a good idea to create controls at run time. Create all the controls and VBA code at design time and show only those that are needed.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
So how do you know that Cells(5,3) is the cell that you want to use for the RowSource? You could do this:

Code:
Dim WS As Worksheet

Private Sub UserForm_Initialize()
    If WS Is Nothing Then
        Set WS = Worksheets("Tests")
    End If
    Me.ComboBox1.RowSource = WS.Cells(4, 3).Validation.Formula1
End Sub

Private Sub ComboBox1_Change()
    WS.Cells(4, 3).Value = ComboBox1.Value
    Me.ComboBox2.RowSource = Evaluate(Replace(WS.Cells(5, 3).Validation.Formula1, "INDIRECT", ""))
End Sub

Notice that I omitted the RowSource assignment as it's use is not best practice.
 
Upvote 0
Shouldn't the "INDIRECT(...)" macro return the range-address by the range name? If so, why does it not return the range in VBA than???
 
Upvote 0
Because it can be problematical, as evidenced by numerous threads about it on this forum (including yours maybe).

Well the dynamically generated controls work fine in every aspect, so far. But because the problem is still the same when I hard code it (to test if it is a problem from the dynamically generated controls) I can assume that the dynamically generated controls are OK so far.
 
Upvote 0
(...)

Code:
Dim WS As Worksheet

Private Sub UserForm_Initialize()
    If WS Is Nothing Then
        Set WS = Worksheets("Tests")
    End If
    Me.ComboBox1.RowSource = WS.Cells(4, 3).Validation.Formula1
End Sub

Private Sub ComboBox1_Change()
    WS.Cells(4, 3).Value = ComboBox1.Value
    Me.ComboBox2.RowSource = Evaluate(Replace(WS.Cells(5, 3).Validation.Formula1, "INDIRECT", ""))
End Sub

Notice that I omitted the RowSource assignment as it's use is not best practice.

You mean ControlSource. Aren't You?

If I interprete the last line of the Private Sub ComboBox1_Change() correctly, than it means that You simply remove the INDIRECT macro so that the range name remains. That's smart man :cool:. That should work. I'll try it now!
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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