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

Ali M

Active Member
Joined
Oct 10, 2021
Messages
287
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
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
One way.

VBA Code:
Sub n()
    Dim WS As Worksheet
    Dim rng As Range
    Dim wsValidationListSheet As Worksheet
    Dim ValidationListRange As Range
    Dim I As Long
    
    Set wsValidationListSheet = Worksheets("Validation List") 'you must create a worksheet to hold the list of worksheets. This sheet can be hidden
    
    'Create the list of worksheets for the validation menu
    With wsValidationListSheet
        Application.Intersect(.UsedRange, .Columns("A:A")).ClearContents
        I = 1
        For Each WS In ActiveWorkbook.Worksheets
            .Cells(I, 1).Value = WS.Name
            I = I + 1
        Next WS
        Set ValidationListRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))             'alternate
    End With
    
    '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:="=" & ValidationListRange.Address(, , , True)
    End With
End Sub
 
Upvote 0
thanks for this solution , but how do that without using button . if I add new sheet it will add in datavalidation automatically without using button every time . last thing can do it without depends on list in column A ? just directly as in combobox without using helper column .
 
Upvote 0
One way would be to call it from the worksheet activate event.

VBA Code:
Private Sub Worksheet_Activate()
   Call n
End Sub
 
Upvote 0
you don't answer me with this part
last thing can do it without depends on list in column A ? just directly as in combobox without using helper column .
is not it clear for you ?
 
Upvote 0
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")
    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
fantastic ! may you exclude sheet1 from the dropdown , I try adding this condition
VBA Code:
If WS.Name <> "sheet1" Then
after
VBA Code:
    Set WS = ThisWorkbook.Worksheets("Sheet1")
but doesn't work , any idea?
 
Upvote 0
actually I use copy the sheet name from tab and paste into the code but doesn't work .
 
Upvote 0
actually I use copy the sheet name from tab and paste into the code but doesn't work .

I don't automatically know what "doesn't work" means. Unless you explain and/or post the complete subroutine with your modifications, I cannot advise you.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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