An update on both issues rasied in this query:
Firstly, I have found that my Command Buttons remain locked down in the SHARE state even without sheet protection being on because all macros and objects get locked anyway when SHARE is turned on.
Secondly, the solution I posted above for creating a unique list of "Cities" worked fine for a small list but missed the mark when I tried with a large list. So to ensure Forum integrity of solutions, I am posting my second solution which works with large lists, and which also works when the workbook is in a SHARE state. This second solution not only also allows for new cities to be added, but also for cities no longer required to be removed, something I didn't consider with the previous code.
Step 1 is to first create a baseline list of Cities on Sheet2 (from cell A2 down) from which the following code will compare with the active list on Sheet1, then update going forward.
Sub TestMaster()
Dim txt As String
Dim DataSheet As Worksheet
Set DataSheet = Sheets("Sheet1")
Set ws4 = Worksheets("Sheet2")
Application.ScreenUpdating = False
With ws4
.Range("B2:B300").ClearContents
End With
With DataSheet
.AutoFilterMode = False
End With
Range("A4:K300").Select
Range("K300").Activate
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B4:B299"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A4:K299")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With DataSheet
.Range("B4:B300").Copy
.Paste ws4.Range("B2")
End With
' STARTS HERE
Sheets("Sheet2").Activate
Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row).Name = "data"
Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Name = "data1"
For Each cell In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
If Not IsError(Application.VLookup(cell, Range("data1"), 1, False)) Then
Else
Range(cell, cell.Offset(0, 0)).Copy
Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll
End If
Next
For Each cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
If Not IsError(Application.VLookup(cell, Range("data"), 1, False)) Then
Else
Range(cell, cell).ClearContents
End If
Next
'
Range("A2:A400").Select
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("A2:A400") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("A2:A400")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("F1").Select
' ENDS TO HERE
With DataSheet
.Range("A3:K3").AutoFilter
End With
Sheets("Sheet1").Select
With Application
.DisplayFormulaBar = False
.DisplayFullScreen = True
.ScreenUpdating = True
End With
End Sub
Bookmarks