ObiwanKenobi24
New Member
- Joined
- Jun 2, 2022
- Messages
- 1
- Office Version
- 2019
- Platform
- Windows
Hello,
I have a problem to be able to create this macro, I explain more below.
First, I have 2 sheets. The first one is called Dropdown and this sheet shows 2 columns. Column A is the Country column and column B is the City column. This sheet groups all the cities that a country can have. There are lots of countries and cities so we must assume that the data can be infinite.
Then, I have the second sheet called PCM where I need to put the information. For example, if I write in column D row 2 the state of UK, I want the macro to create a dropdown list in next column same row (E2) with their respective cities. This data validation list must contain the cities that correspond to its country and these are extracted from the Dropdown sheet. For example, for the state of UK the dropdown list created in next column same row should be: Birmingham, Glasgow, Liverpool and Cardiff. I need this to happen every time I write a country (always in column D) that is listed in the Dropdown sheet and in the next column the same row with their respective cities in the form of a dropdown list.
An example:
The code I've been working is this one but no success until now. If someone could help me with this it would be very kind.
Thanks.
I have a problem to be able to create this macro, I explain more below.
First, I have 2 sheets. The first one is called Dropdown and this sheet shows 2 columns. Column A is the Country column and column B is the City column. This sheet groups all the cities that a country can have. There are lots of countries and cities so we must assume that the data can be infinite.
Then, I have the second sheet called PCM where I need to put the information. For example, if I write in column D row 2 the state of UK, I want the macro to create a dropdown list in next column same row (E2) with their respective cities. This data validation list must contain the cities that correspond to its country and these are extracted from the Dropdown sheet. For example, for the state of UK the dropdown list created in next column same row should be: Birmingham, Glasgow, Liverpool and Cardiff. I need this to happen every time I write a country (always in column D) that is listed in the Dropdown sheet and in the next column the same row with their respective cities in the form of a dropdown list.
An example:
The code I've been working is this one but no success until now. If someone could help me with this it would be very kind.
VBA Code:
Sub DistributeDropdowns()
Const ProcName As String = "DistributeDropdowns"
On Error GoTo ClearError
Const sName As String = "Dropdown"
Const saCol As Long = 1
Const svCol As Long = 2
Const dvRows As String = "2:1000"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim srg As Range: Set srg = sws.Range("A1").CurrentRegion.Columns(saCol)
Dim nCount As Long: nCount = srg.Rows.Count
Dim nData As Variant: nData = srg.Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim n As Long
Dim nString As String
For n = 2 To nCount
nString = nData(n, 1)
If dict.Exists(nString) Then
Set dict(nString) = Union(dict(nString), sws.Cells(n, svCol))
Else
Set dict(nString) = sws.Cells(n, svCol)
End If
Next n
Application.ScreenUpdating = False
Const dwsName As String = "PCM"
Dim dws As Worksheet: Set dws = wb.Worksheets(dwsName)
Dim drg As Range
For Each Cell In Range("D2:D1000").Cells
If dict.Exists(nData(n, 4)) Then
With dws.Range("D2:D1000").Cells
nCount = .Columns.Count
nData = .Value
Set drg = .EntireColumn.Rows(dvRows)
End With
For n = 2 To nCount
If dict.Exists(nData(n, 4)) Then
With drg.Rows(n).Validation
.Delete
.Add xlValidateList, xlValidAlertStop, xlEqual, _
"='" & sName & "'!" & dict(nData(n, 4)).Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
Next n
End If
Next
'wb.Save
Application.ScreenUpdating = True
MsgBox "Dropdowns distributed.", vbInformation
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub
Thanks.