Given your description of the sheets in the VBA editor, the naming might be a bit different from the default Excel sheet naming. Excel assigns code names like Sheet1, Sheet2, etc., to the sheets based on their creation order, but these code names do not necessarily correspond to the visible tab names in Excel. The ThisWorkbook object represents the entire workbook.
In your case, to place a drop-down list on what you referred to as "sheet5" (which seems to be represented in VBA as Sheet3 in your description), and to pull data from "sheet4" (which appears to be Sheet1), you'll want to adjust the code to use these specific sheet code names.
However, the location where you put the VBA code is in a Module rather than directly under ThisWorkbook or any of the Sheet objects. Here is a refreshed guide based on your setup:
- Press
Alt + F11 to open the VBA Editor.
- In the VBA Editor, right-click any of the objects in the Project Explorer window (like
ThisWorkbook or any item named Sheet1, Sheet2, etc. under "Microsoft Excel Objects") and choose Insert > Module. This action creates a new module.
- Inside the new module, paste the adapted VBA code:
Sub CreateDropDownListFromSheet1ToSheet3()
Dim wsTarget As Worksheet
Dim wsSource As Worksheet
Dim rngSource As Range
Dim strAddress As String
' Adjusted to use the VBA code names - assuming Sheet1 is your "sheet4" and Sheet3 is your "sheet5"
' Make sure these code names correspond correctly to your actual sheet names
Set wsTarget = ThisWorkbook.Sheets("Sheet3") ' Assuming Sheet3 is what you call "sheet5"
Set wsSource = ThisWorkbook.Sheets("Sheet1") ' Assuming Sheet1 is what you call "sheet4"
' Define the range of the source values on what is actually "Sheet1" in your VBA (but what you're calling "sheet4")
Set rngSource = wsSource.Range("B2:AO2")
' Convert the source range address to a string in R1C1 style reference
strAddress = rngSource.Address(ReferenceStyle:=xlR1C1, External:=True)
' Clear any existing validations in target cell
wsTarget.Range("C4").Validation.Delete
' Create the Validation for the Drop-down list
With wsTarget.Range("C4").Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=strAddress
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
.ErrorTitle = "Input Error"
.ErrorMessage = "Please select from the list."
End With
End Sub
- Save the VBA project and close the VBA Editor.
- Run the macro by pressing
Alt + F8, selecting CreateDropDownListFromSheet1ToSheet3, and clicking Run.
Please ensure that the Sheet1 and Sheet3 mentioned in the VBA code correspond correctly to what you're referring to as "sheet4" and "sheet5", respectively. The naming inside the VBA can often be confusing if it doesn't match the visible names on the Excel tab. Adjust the code names (Sheet1, Sheet3, etc.) as needed to match your actual setup.