[英]Excel VBA - Appending Values from a LIST on daily changing Data
Problem to solve for:需要解决的问题:
Sheet1 in my workbook refreshes daily.我工作簿中的 Sheet1 每天刷新一次。 Column B in Sheet1 populates several rows with Account Names (and account names can have multiple rows).
Sheet1 中的 B 列用帐户名填充多行(帐户名可以有多行)。
I want Sheet2 Column A in my workbook to populate a distinct list of distinct accounts from Column B in Sheet1, WITH THE CATCH being, I want this to continuously append as Sheet1 will populate a new list of Accounts daily.我希望我的工作簿中的 Sheet2 A 列从 Sheet1 中的 B 列中填充不同帐户的不同列表,随着 CATCH 的出现,我希望它连续 append 因为 Sheet1 将每天填充一个新的帐户列表。 In other words, if there are 5 accounts today, and 2 accounts tomorrow, I want Sheet 2 Column A to show all 7 Accounts.
换句话说,如果今天有 5 个帐户,明天有 2 个帐户,我希望表 2 的 A 列显示所有 7 个帐户。
I've scraped together some code from other posts that I thought would do this, but it's not populating anything in Sheet2.我从其他帖子中收集了一些我认为可以执行此操作的代码,但它没有在 Sheet2 中填充任何内容。 Please see the attached image and code below:
请参阅下面的附件图片和代码:
Code:代码:
Sub TestMacro()
Dim Cell As Range
Dim Key As String
Dim Dict As Object
Dim LookupWks As Worksheet
Dim MstrWks As Worksheet
Dim NextCell As Range
Dim r As Long
Set MstrWks = ThisWorkbook.Worksheets("Sheet1")
Set LookupWks = ThisWorkbook.Worksheets("Sheet2")
Set Dict = CreateObject("Scripting.Dictionary")
Dict.CompareMode = vbTextCompare
For r = 2 To MstrWks.Cells(Rows.Count, "A").End(xlUp).Row
Key = MstrWks.Cells(r, "A")
If Trim(Key) <> "" Then
If Not Dict.Exists(Key) Then
Dict.Add Key, r
End If
End If
Next r
Set NextCell = LookupWks.Cells(2, "A").End(xlUp).Offset(1, 0)
For r = 2 To LookupWks.Cells(Rows.Count, "A").End(xlUp).Row
Key = LookupWks.Cells(r, "A")
If Trim(Key) <> "" Then
If Not Dict.Exists(Key) Then
NextCell.Value = Key
Set NextCell = NextCell.Offset(1, 0)
End If
End If
Next r
End Sub结束子
I've done quite a bit of research on this topic, and hacked together some code from other posts and tweaks that I had seen, but it's not populating anything.我对这个主题做了很多研究,并从其他帖子和我看到的调整中收集了一些代码,但它没有填充任何东西。
The problem is your code is only looking at the populated cells in sheet 2, so it stops before it ever gets to the keys that don't exist on that sheet.问题是您的代码只查看工作表 2 中填充的单元格,因此它会在到达该工作表上不存在的键之前停止。
If we iterate the dictionary instead of the cells and use find it will populate your sheet 2 with the missing keys:如果我们迭代字典而不是单元格并使用查找,它将使用缺少的键填充您的工作表 2:
Dim Cell As Range
Dim key As Variant ' I changed this to variant to use it as an iterator later on
Dim Dict As Object
Dim LookupWks As Worksheet
Dim MstrWks As Worksheet
Dim NextCell As Range
Dim r As Long
Set MstrWks = ThisWorkbook.Worksheets("Sheet1")
Set LookupWks = ThisWorkbook.Worksheets("Sheet2")
Set Dict = CreateObject("Scripting.Dictionary")
Dict.CompareMode = vbTextCompare
' Don't forget to add a sheet reference to Rows.Count, it may give the wrong value
For r = 2 To MstrWks.Cells(MstrWks.Rows.Count, "A").End(xlUp).Row
key = MstrWks.Cells(r, "A")
If Trim(key) <> "" Then
If Not Dict.Exists(key) Then
Dict.Add key, r
End If
End If
Next r
Dim findrng As Range
With LookupWks
r = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
For Each key In Dict
Set findrng = .Range("A:A").Find(key, .Cells(2, 1), xlValues, xlWhole, xlByRows, xlNext)
If findrng Is Nothing Then
.Cells(r, 1).Value = key
r = r + 1
End If
Next key
End With
I've put together some modifications to your code that should hopefully get you moving in the right direction.我已经对您的代码进行了一些修改,希望可以让您朝着正确的方向前进。 I've embedded comments directly into the code to give you an idea of what's happening.
我已将注释直接嵌入到代码中,以便您了解正在发生的事情。 Let me know if it helps.
让我知道是否有帮助。
Sub TestMacro()
Dim Cell As Range
Dim Key As String
Dim Dict As Object
Dim LookupWks As Worksheet
Dim MstrWks As Worksheet
Dim NextCell As Range
Dim r As Long
Dim DestDict As Object
Set MstrWks = ThisWorkbook.Worksheets("Sheet1")
Set LookupWks = ThisWorkbook.Worksheets("Sheet2")
Set Dict = CreateObject("Scripting.Dictionary")
Dict.CompareMode = vbTextCompare
'This is good. It establishes a dictionary of uniques from Master sheet
For r = 2 To MstrWks.Cells(Rows.Count, "A").End(xlUp).Row
Key = MstrWks.Cells(r, "A")
If Trim(Key) <> "" Then
If Not Dict.exists(Key) Then
Dict.Add Key, r
End If
End If
Next r
''''
' I might actually create another dictionary here against Sheet2
' This would contain uniques from Sheet 2 so that we don't add
' an element that is already here. This dict will contain items
' that are in sheet2. You can also likely use a Match function
' to check if items in the original dict are in this sheet.
'''
Set DestDict = CreateObject("scripting.dictionary")
For r = 2 To LookupWks.Cells(Rows.Count, "A").End(xlUp).Row
Key = LookupWks.Cells(r, "A")
If Trim(Key) <> "" Then
If Not DestDict.exists(Key) Then
DestDict.Add Key, r
End If
End If
Next r
'''''
' Now you have a dictionary with uniques from sheet1 and sheet 2
' Loop through the Sheet1 dict and add to sheet2 if the item
' is not in sheet2
'''''
Set NextCell = LookupWks.Cells(LookupWks.Rows.Count, "A").End(xlUp).Offset(1, 0)
For Each oKey In Dict.Keys
If Not DestDict.exists(oKey) Then
NextCell.Value = oKey
Set NextCell = NextCell.Offset(1)
End If
Next oKey
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.