【问题标题】:Unique Values from a range范围内的唯一值
【发布时间】:2017-06-29 07:07:48
【问题描述】:

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

因此,例如,如果 F 列中的唯一名称是“红、白、蓝、绿”,则新工作表中的 R 列第 n 行将显示“红色观察者、白色观察者、蓝色观察者、绿色观察者。 "我现在只在 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

【问题讨论】:

标签: vba excel


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多