Dropdowns to Combo boxes

rahulbassi

Board Regular
Joined
Aug 14, 2011
Messages
130
Hi All,
I'm using below code for turning all the dropdowns to combo boxes. The probelm is that it is working if the database(for list) is in the same sheet. But, my database is in different worksheet "Database".
Please suggest.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet -----> This limits the scope to Sheet only
'Dim ss As Workbook -----> This is not working
'Set ss = ActiveWorkbook
Set ws = ActiveSheet

On Error GoTo errHandler
If Target.count > 1 Then GoTo exitHandler
Set cboTemp = ws.OLEObjects("TempCombo")
'Set cboTemp = ws.OLEObjects(Sheet1.TempCombo)
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
' MsgBox (ws.Range(str).AddressLocal)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If
exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
errHandler:
Resume exitHandler

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If you mean the list of values for the comboboxes is on Database try this.

Change this:
Code:
Set ws = ActiveSheet
To this:
Code:
Set ws = Worksheets("Database")
 
Upvote 0
My fault, I thought ws was only being used for the list.

Change the code back and try this.

Add this at the top of the code.
Code:
Dim wsData As Worksheets
Add this below the line I suggested you should change.
Code:
Set wsData = Worksheets("Database")
Change this:
Code:
.ListFillRange = ws.Range(str).Address
To this:
Code:
.ListFillRange = wsData.Range(str).Address
 
Upvote 0
Hi Norie
The combo box is not having any value in it now after i had made the changes you recommended.
Should I send you the code and the complete situation?
 
Upvote 0
Best stick to the forum for now.

Can you post the exact code you have now?

Also, post details of where the list is on worksheet 'Database'.
 
Upvote 0

Forum statistics

Threads
1,214,397
Messages
6,119,271
Members
448,882
Latest member
Lorie1693

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