2017-06-28 102 views
0

我试图编写一个代码,查看打开的工作表(figWkst),在F列中查找并收集所有唯一名称,然后在一次添加一个唯一名称后加上单词“看守者“和一个逗号。因此,例如,如果列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 
+0

这里我的答案给出了一个简单的方法来测试在循环范围/数组时使用重复数据https://stackoverflow.com/questions/42301589/identifying-duplicate-values-and-copying-to-other-cells-using-for-loops-in-vba-e/42301986 #42301986 –

回答

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