get list of sheet in dropdown(datavalidation) not combobox "run time error438

Ali M

Active Member
Joined
Oct 10, 2021
Messages
290
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
hello
I try getting list of sheets into dropdown (datavaldation ) I don't want using combobox, is it possible?
this is what I got but gives error object doesn't support this property
VBA Code:
Sub n()
Dim ws As Worksheet
Dim  rng As Range
 Dim oSheet As Excel.Worksheet
'change Sheet1 to suit
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set rng = ws.Range("B1")
For Each oSheet In ActiveWorkbook.Sheets
With rng.Validation
    .Delete 'delete previous validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:="='" & ws.Name & "'!" & oSheet
End With
Next
End Sub
so I want doing that by worksheet change event and make it dynamically when add new sheet or delete should update in datavalidation automatically
thanks
 
this is the whole code
VBA Code:
Sub n()
    Dim WS As Worksheet
    Dim rng As Range
    Dim ValidationList As String

    
    'Create the list for the validation menu
    For Each WS In ActiveWorkbook.Worksheets
        ValidationList = ValidationList & WS.Name & ","
    Next WS
    ValidationList = Left(ValidationList, Len(ValidationList) - 1)
    
    'define the validation cell range
    Set WS = ThisWorkbook.Worksheets("Sheet1")
    If WS.Name <> "Sheet1" Then

    Set rng = WS.Range("B1")
    
    'Update the validation
    With rng.Validation
        .Delete 'delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=ValidationList
    End With
    End If
End Sub
I don't automatically know what "doesn't work" means
should not include Sheet1 into dropdown
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You have placed If WS.Name <> "Sheet1" Then in the wrong location. Do this instead:

VBA Code:
Sub n()
    Dim WS As Worksheet
    Dim rng As Range
    Dim ValidationList As String
    
    
    'Create the list for the validation menu
    For Each WS In ActiveWorkbook.Worksheets
        If WS.Name <> "Sheet1" Then
            ValidationList = ValidationList & WS.Name & ","
        End If
    Next WS
    ValidationList = Left(ValidationList, Len(ValidationList) - 1)
    
    'define the validation cell range
    Set WS = ThisWorkbook.Worksheets("Sheet1")
    Set rng = WS.Range("B1")
    
    'Update the validation
    With rng.Validation
        .Delete 'delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=ValidationList
    End With
End Sub
 
Upvote 0
Solution
new version is excellent !
many thanks for every thing (y)
 
Upvote 0

Forum statistics

Threads
1,215,812
Messages
6,127,024
Members
449,352
Latest member
Tileni

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