范围内的唯一值

时间:2017-06-28 19:53:34

标签: excel vba excel-vba

我正在尝试编写一个查看打开的工作表(figWkst)的代码,在F列中查找,并收集所有唯一的名称,然后一次添加一个唯一的名称,后跟单词“watcher”和逗号。

因此,例如,如果F列中的唯一名称是“红色,白色,蓝色,绿色”,则新工作表中的列R行n将显示“Red Watcher,White Watcher,Blue Watcher,Green Watcher”。 “我现在只在列R中显示F行n显示的列。

我正在考虑使用字典,但我似乎无法绕过它。任何帮助,将不胜感激!谢谢。这是我的代码:

Sub Role()

'define variables
Dim RoleWkb As Workbook, figWkb As Workbook, RoleWkst As Worksheet, figWkst As Worksheet
Dim aCell As Long, a As Long, c As String, i As String, id As String, email As String, cityname As Variant

'open workbook
fNameAndPath = Application.GetOpenFilename(Title:="Role Workbook")
Set RoleWkb = Workbooks.Open(fNameAndPath)
Set figWkb = ThisWorkbook
Set RoleWkst = RoleWkb.Sheets("UserProfile")
Set figWkst = figWkb.Worksheets("User Information")
cityname = InputBox("City name?")



'adding watcher group
aCell = figWkst.Range("D" & Rows.Count).End(xlUp).Row
For a = 12 To aCell
    If figWkst.Cells(a, 17) = "x" Or figWkst.Cells(a, 17) = "X" Then
        If RoleWkst.Cells(a - 1, 18) <> "" Then
            RoleWkst.Cells(a - 1, 18) = Trim(RoleWkst.Cells(a - 1, 18) & ", " & cityname & " " & figWkst.Cells(a, 6) & " Watcher")
        Else
            RoleWkst.Cells(a - 1, 18) = Trim(cityname & " " & figWkst.Cells(a, 6) & " Watcher")
        End If
    End If
Next a

End Sub

1 个答案:

答案 0 :(得分:0)

这个怎么样......虽然再次掌握或模拟你的真实情况有点困难......

Sub RoleModified()

'define variables
Dim RoleWkb As Workbook, figWkb As Workbook, RoleWkst As Worksheet, figWkst As Worksheet
Dim aCell As Long, a As Long, c As String, i As String, id As String, email As String, cityname As Variant

'added some variables
Dim vNames()                'to contain all the original names
Dim vUniqueNames()          'to collect unique ones
Dim vUniqueCount As Integer 'to count the unique ones
Dim vUnique As Boolean      'to mark that it was stored already

'open workbook
fNameAndPath = Application.GetOpenFilename(Title:="Role Workbook")
Set RoleWkb = ThisWorkbook
Set figWkb = ThisWorkbook
Set RoleWkst = RoleWkb.Sheets("UserProfile")
Set figWkst = figWkb.Worksheets("User Information")
cityname = InputBox("City name?", , "London")

'whatever way you can refer to where the names are, this worked for my 
'simulation...
figWkst.Select
vNames = Intersect(figWkst.UsedRange, Range("F:F"))

'set up the variables
ReDim vUniqueNames(1 To UBound(vNames, 1))
vUniqueCount = 0

'loop through all the names and take each one only once
For n = 1 To UBound(vNames, 1)
    vUnique = True
    'test it against those already stored
    For m = 1 To UBound(vNames, 1)
        If vUniqueNames(m) = vNames(n, 1) Then
            vUnique = False
        End If
    Next m
    'if not found, store it
    If vUnique Then
        vUniqueCount = vUniqueCount + 1
        vUniqueNames(vUniqueCount) = vNames(n, 1)
    End If
Next n

'placing unique names to the other sheet, meaning of x's missed
For n = 1 To vUniqueCount
    RoleWkst.Cells(10 + n, 18) = cityname & " " & _
    vUniqueNames(n) & " Watcher"
Next n

End Sub