Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 比较两个列表/两列中的每个列表组合_Vba_Excel - Fatal编程技术网

Vba 比较两个列表/两列中的每个列表组合

Vba 比较两个列表/两列中的每个列表组合,vba,excel,Vba,Excel,假设我在sheet1中有我的个人日志,在“A”列中有我的主账号,即客户账号,在“B”列中有他们的子账号 在表2中,我收到了相同结构的账户和子账户列表,我需要找到表2中的唯一编号,但我在表1的个人日志中没有该编号 例如,在表1中,我有 account sub account 1110000 12 1110000 14 我在第2张清单中收到,其中包含: account sub account 1110000 12 1110000 16 在这

假设我在sheet1中有我的个人日志,在“A”列中有我的主账号,即客户账号,在“B”列中有他们的子账号

在表2中,我收到了相同结构的账户和子账户列表,我需要找到表2中的唯一编号,但我在表1的个人日志中没有该编号

例如,在表1中,我有

account     sub account

1110000     12

1110000     14
我在第2张清单中收到,其中包含:

account     sub account

1110000     12

1110000     16
在这种情况下,宏应从sheet2列表中找到1110000 16(帐户相同,但子帐户是新的),并将其写入sheet1的下一个空单元格中

我非常感谢你们的帮助,我试过用match和vlookup,但都不起作用


Andrew

一种可能的解决方案是将日志表的帐户和子帐户复制到一个数组中。对表2中的科目和子科目执行相同的操作。然后循环检查sheet2中的所有帐户和子帐户对,并测试它们是否在日志数组中。如果没有,请将它们复制到新帐户数组中。最后,将新帐户复制到日志表中。下面是此解决方案的代码:

Option Explicit
Option Base 1

Sub CopyNewAccountsInLogSheet()

Dim initial As Range 'range with accounts and sub-accounts in sheet 1
Dim destination As Range 'the range where to add new accounts and sub-accounts in sheet1
Dim myLog() As Variant 'array with accounts and sub-accounts from log sheet
Dim newList() As Variant 'array of accounts and sub-accounts from sheet 2
Dim newAccounts() As Variant 'array of accounts and sub-accounts to add to log sheet
Dim x, y, n As Integer
Dim isInLog As Boolean

Set initial = Worksheets("Sheet1").Range("a1").CurrentRegion 'replace with correct range
Set destination = Worksheets("Sheet1").Range("A" & (initial.Rows.Count + 1)) 'first cell below initial range
myLog = initial.Value
newList = Worksheets("Sheet2").Range("a1").CurrentRegion.Value 'replace with correct range
ReDim newAccounts(UBound(newList, 1), 2)
n = 0

For x = 1 To UBound(newList, 1) 'loop through new list

    isInLog = False 'by default, we assume account + sub-account is not in log sheet

    For y = 1 To UBound(myLog, 1) 'compare with each account + sub-account in log file
        If CStr(myLog(y, 1)) & CStr(myLog(y, 2)) = CStr(newList(x, 1)) & CStr(newList(x, 2)) Then
            isInLog = True
            Exit For
        End If
    Next

    If Not isInLog Then
        n = n + 1
        newAccounts(n, 1) = newList(x, 1)
        newAccounts(n, 2) = newList(x, 2)
    End If

Next

If n > 0 Then destination.Resize(n, 2).Value = newAccounts 'copy new accounts in sheet

End Sub

是一个帐号还是多个?多个帐号,每个帐号有多个子帐号?帐号是如何组织的?它们是按数字排序的吗?由于宏的潜在复杂性和数据输入的稀缺性,您需要提供更多数据吗?我可以用你提供的数据回答你的问题,但它以后能帮你吗?我想如果你的数据没有组织好,你需要一个
词汇表
。账户总是从最小的数字到最大的数字排序。列表上总是数值,通常我在我的登录列“A”中有我收到的列表上的所有帐号,更多的时候我需要找到连接到我已经拥有的帐号的子帐号。有两种可能:a)我有账号,但我没有新的子账号b)我没有账号和子账号主要问题是要自动化,宏将检查账号,如果账号存在,检查子账号这个账号(如果账号不存在,写账号和子账号)超级棒!!!它起作用了。非常感谢你的帮助和努力!!