gpt4 book ai didi

excel - 试图用 VBA 找到重复项来做一些奇怪的事情(一个棘手的问题)

转载 作者:行者123 更新时间:2023-12-04 20:01:39 27 4
gpt4 key购买 nike

我在 excel 中有 2 个选项卡,我对 VBA 有点陌生:
运营:
enter image description here
详情:
image 2

看看这个:“操作”选项卡中的说明字段将包含不同的“操作代码”(它可能包含 1 个操作代码、2 个操作代码或更多)。它是一个 11 位数字。问题是这个字段是固定的,有时操作代码会被截断。
只有那些带有 的号码11 位的确切数量 必须考虑
我想实现这一点(必须按顺序执行):

  • VBA 应该从“操作”选项卡的“描述”单元格中找到每个事务。在这种情况下,第一行包含一个事务,第 2 行包含一个事务,第 3 行包含 2 个事务并且只考虑 11 位数字内的操作代码
  • 它应该从选项卡“操作”中复制数字并将其粘贴到选项卡“描述”中的“已链接”列中
  • 查找“描述”字段中的所有重复值,将它们分组并执行以下操作:
    一个)
    enter image description here

  • --- 如果重复值包含“FC”和“N/C”类型,则必须从“N/C”类型的“操作”选项卡中输入单元格“编号”的值,并将其放在“链接”列中从“详细信息”选项卡中,然后在“注意”字段中写入“完成”字样,最后将“网络”列中的“金钱”列粘贴。
    预期结果:
    enter image description here
    enter image description here
    编辑#1:这将是所需的输出:
    id|id|id|id|id|id|id|TYPE|NUMBER         |SECTOR|DESCRIPTION                    |MONEY|AMOUNT|TOTAL|WEB|||DIFFERENCE||
    1 |1 |1 |1 |1 |1 |1 |FAC |B0001100005429 |XX |SADADECO 19278294999 |XX |XX |XX |XX ||| ||
    1 |1 |1 |1 |1 |1 |1 |N/C |B0001100005445 |XX |SADADECO 19278294999 |XX |XX |XX |23 ||| ||
    2 |2 |2 |2 |2 |2 |2 |FAC |B0001100006545 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |XX ||| ||
    2 |2 |2 |2 |2 |2 |2 |N/C |B0001100005449 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |33 ||| ||
    3 |3 |3 |3 |3 |3 |3 |FAC |B0001100007429 |XX |rer 19266048445 19266048223 |XX |XX |XX |XX ||| ||
    3 |3 |3 |3 |3 |3 |3 |N/C |B0001100007729 |XX |rer 19266048445 19266048223 |XX |XX |XX |33 ||| ||
    4 |4 |4 |4 |4 |4 |4 |FAC |B0001100007829 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |XX ||| ||
    4 |4 |4 |4 |4 |4 |4 |N/C |B0001100009029 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |434||| ||
    5 |5 |5 |5 |5 |5 |5 |FAC |B0001100009229 |XX |rer 19266049495 |XX |XX |XX |XX ||| ||
    6 |6 |6 |6 |6 |6 |6 |FAC |B0001100009429 |XX |rer 19266049499 |XX |XX |XX |XX ||| ||
    7 |7 |7 |7 |7 |7 |7 |FAC |B0001100009929 |XX |rer 19266049999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |C0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |D0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REV |E0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001100004929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001109904929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001109904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001108904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||



    Date |operation|type| type| transaction number |operation |money| linked | note
    27/02/2022| null |null| null| null |19278294999|23 |B0001100005445|DONE
    27/02/2022| null |null| null| null |19299490733|33 |B0001100005449|DONE
    27/02/2022| null |null| null| null |19266048223|33 |B0001100007729|DONE
    27/02/2022| null |null| null| null |19266048445|434 |B0001100009029|DONE
    27/02/2022| null |null| null| null |19266049495|23 |B0001100009229|
    27/02/2022| null |null| null| null |19266049499|223 |B0001100009429|
    27/02/2022| null |null| null| null |19266049999|424 |B0001100009929|
    27/02/2022| null |null| null| null |19271194999|74574|E0001100004929|

    数据集:
    操作表
    id|id|id|id|id|id|id|TYPE|NUMBER         |SECTOR|DESCRIPTION                    |MONEY|AMOUNT|TOTAL|WEB|||DIFFERENCE||
    1 |1 |1 |1 |1 |1 |1 |FAC |B0001100005429 |XX |SADADECO 19278294999 |XX |XX |XX |XX ||| ||
    1 |1 |1 |1 |1 |1 |1 |N/C |B0001100005445 |XX |SADADECO 19278294999 |XX |XX |XX |XX ||| ||
    2 |2 |2 |2 |2 |2 |2 |FAC |B0001100006545 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |XX ||| ||
    2 |2 |2 |2 |2 |2 |2 |N/C |B0001100005449 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |XX ||| ||
    3 |3 |3 |3 |3 |3 |3 |FAC |B0001100007429 |XX |rer 19266048445 19266048223 |XX |XX |XX |XX ||| ||
    3 |3 |3 |3 |3 |3 |3 |N/C |B0001100007729 |XX |rer 19266048445 19266048223 |XX |XX |XX |XX ||| ||
    4 |4 |4 |4 |4 |4 |4 |FAC |B0001100007829 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |XX ||| ||
    4 |4 |4 |4 |4 |4 |4 |N/C |B0001100009029 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |XX ||| ||
    5 |5 |5 |5 |5 |5 |5 |FAC |B0001100009229 |XX |rer 19266049495 |XX |XX |XX |XX ||| ||
    6 |6 |6 |6 |6 |6 |6 |FAC |B0001100009429 |XX |rer 19266049499 |XX |XX |XX |XX ||| ||
    7 |7 |7 |7 |7 |7 |7 |FAC |B0001100009929 |XX |rer 19266049999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |C0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |D0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REV |E0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001100004929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001109904929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001109904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001108904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||
    详细信息表:
    Date      |operation|type|  type|   transaction number |operation  |money|  linked| note
    27/02/2022| null |null| null| null |19278294999|23 | |
    27/02/2022| null |null| null| null |19299490733|33 | |
    27/02/2022| null |null| null| null |19266048223|33 | |
    27/02/2022| null |null| null| null |19266048445|434 | |
    27/02/2022| null |null| null| null |19266049495|23 | |
    27/02/2022| null |null| null| null |19266049499|223 | |
    27/02/2022| null |null| null| null |19266049999|424 | |
    27/02/2022| null |null| null| null |19271194999|74574| |
    27/02/2022| null |null| null| null |19266048223|343 | |

    使用以下代码可以解决步骤 1 和 2:
    Sub M_snb()

    Dim wsOps As Worksheet, wsDets As Worksheet
    Dim c As Range, col As Collection, v, m
    Dim dataOps, dataDets, rO As Long, rD As Long

    Set wsOps = ThisWorkbook.Worksheets("Operations")
    Set wsDets = ThisWorkbook.Worksheets("Details")

    dataOps = wsOps.Range("A1").CurrentRegion.Value
    dataDets = wsDets.Range("A1").CurrentRegion.Value

    For rO = 2 To UBound(dataOps, 1)
    Set col = AllNumbers(dataOps(rO, 3))
    For Each v In col
    For rD = 2 To UBound(dataDets, 1)
    If CStr(dataDets(rD, 1)) = v Then
    dataDets(rD, 3) = dataOps(rO, 1)
    dataOps(rO, 4) = dataOps(rO, 4) + dataDets(rD, 2)
    End If
    Next rD
    Next v
    Next rO

    DropArray dataOps, wsOps.Range("A1")
    DropArray dataDets, wsDets.Range("A1")
    End Sub

    'return all 11-digit strings in v as a Collection
    Function AllNumbers(v) As Collection
    Const NUM_DIGITS As Long = 11
    Dim m As Object, mc As Object, col As New Collection, txt, i As Long, patt, ss
    txt = " " & v & " "
    patt = String(NUM_DIGITS, "#")
    i = 2
    For i = 2 To Len(txt) - NUM_DIGITS
    ss = Mid(txt, i, 11)
    If ss Like patt Then
    If Not Mid(txt, i - 1, 1) Like "#" Then
    If Not Mid(txt, i + NUM_DIGITS, 1) Like "#" Then
    col.Add ss
    End If
    End If
    End If
    Next i
    Set AllNumbers = col
    End Function

    'Utility method: put a 2d array on a sheet at rng
    Sub DropArray(arr, rng As Range)
    rng.Cells(1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    End Sub

    编辑#1:这将是 desi
    id|id|id|id|id|id|id|TYPE|NUMBER         |SECTOR|DESCRIPTION                    |MONEY|AMOUNT|TOTAL|WEB|||DIFFERENCE||
    1 |1 |1 |1 |1 |1 |1 |FAC |B0001100005429 |XX |SADADECO 19278294999 |XX |XX |XX |XX ||| ||
    1 |1 |1 |1 |1 |1 |1 |N/C |B0001100005445 |XX |SADADECO 19278294999 |XX |XX |XX |23 ||| ||
    2 |2 |2 |2 |2 |2 |2 |FAC |B0001100006545 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |XX ||| ||
    2 |2 |2 |2 |2 |2 |2 |N/C |B0001100005449 |XX |ORDONEZC9920 19299490733 |XX |XX |XX |33 ||| ||
    3 |3 |3 |3 |3 |3 |3 |FAC |B0001100007429 |XX |rer 19266048445 19266048223 |XX |XX |XX |XX ||| ||
    3 |3 |3 |3 |3 |3 |3 |N/C |B0001100007729 |XX |rer 19266048445 19266048223 |XX |XX |XX |33 ||| ||
    4 |4 |4 |4 |4 |4 |4 |FAC |B0001100007829 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |XX ||| ||
    4 |4 |4 |4 |4 |4 |4 |N/C |B0001100009029 |XX |IGN_GONTAN 19266048445 19299494|XX |XX |XX |434||| ||
    5 |5 |5 |5 |5 |5 |5 |FAC |B0001100009229 |XX |rer 19266049495 |XX |XX |XX |XX ||| ||
    6 |6 |6 |6 |6 |6 |6 |FAC |B0001100009429 |XX |rer 19266049499 |XX |XX |XX |XX ||| ||
    7 |7 |7 |7 |7 |7 |7 |FAC |B0001100009929 |XX |rer 19266049999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |C0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REC |D0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    8 |8 |8 |8 |8 |8 |8 |REV |E0001100004929 |XX |SADADECO 19271194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001100004929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    9 |9 |9 |9 |9 |9 |9 |FAC |Z0001109904929 |XX |FSDKFKS 19551194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001109904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||
    10|10|10|10|10|10|10|REC |W0001108904729 |XX |AFDKFKR 19711194999 |XX |XX |XX |XX ||| ||



    Date |operation|type| type| transaction number |operation |money| linked | note
    27/02/2022| null |null| null| null |19278294999|23 |B0001100005445|DONE
    27/02/2022| null |null| null| null |19299490733|33 |B0001100005449|DONE
    27/02/2022| null |null| null| null |19266048223|33 |B0001100007729|DONE
    27/02/2022| null |null| null| null |19266048445|434 |B0001100009029|DONE
    27/02/2022| null |null| null| null |19266049495|23 |B0001100009229|
    27/02/2022| null |null| null| null |19266049499|223 |B0001100009429|
    27/02/2022| null |null| null| null |19266049999|424 |B0001100009929|
    27/02/2022| null |null| null| null |19271194999|74574|E0001100004929|

    DO NOTE that field "web" from tab "Operations" contains an Excelformula and when a value is updated by using the VBA it should notdelete the formula for the other cells



    enter image description here
    enter image description here

    最佳答案

    编辑 : 终于修复和测试了(感觉不好让它未完成)。这基于您共享的工作簿。不再使用数组作为数据,因为您似乎也有公式。

    Sub M_snb()
    Const VAL_NC As String = "N/C"
    Const VAL_FAC As String = "FAC"

    'column positions - ops
    Const COL_OPS_TYPE As Long = 8
    Const COL_OPS_NUMBER As Long = 9
    Const COL_OPS_DESCR As Long = 11
    Const COL_OPS_MONEY As Long = 12

    'column positions - details
    Const COL_DET_OPS_NUM As Long = 5
    Const COL_DET_MONEY As Long = 6
    Const COL_DET_LINKED As Long = 7
    Const COL_DET_NOTE As Long = 8

    Dim wsOps As Worksheet, wsDets As Worksheet
    Dim c As Range, col As Collection, v, m
    Dim rngOps As Range, rngDets As Range, rO As Long, rD As Long, rw
    Dim dict As Object, colRows As Collection
    Dim bFAC As Boolean, bNC As Boolean, amt, typ

    Set dict = CreateObject("scripting.dictionary")

    Set wsOps = ThisWorkbook.Worksheets("Operations")
    Set wsDets = ThisWorkbook.Worksheets("Details")

    Set rngOps = wsOps.Range("A1").CurrentRegion
    Set rngDets = wsDets.Range("A1").CurrentRegion

    'Loop over ops data and find all unique 11-digit numbers,
    ' and store the rows they're found on in a collection per number
    For rO = 2 To rngOps.Rows.Count
    Set col = AllNumbers(rngOps.Cells(rO, COL_OPS_DESCR).Value)
    For Each v In col
    If Not dict.exists(v) Then dict.Add v, New Collection 'new number?
    dict(v).Add rO 'store current row number
    Next v
    Next rO

    For Each v In dict.keys 'loop the unique numbers

    Set colRows = dict(v) 'all Operations rows which contain this number...
    bFAC = False
    bNC = False
    For Each rw In colRows 'loop rows and check "types"
    Select Case rngOps.Cells(rw, COL_OPS_TYPE).Value
    Case VAL_NC: bNC = True
    Case VAL_FAC: bFAC = True
    End Select
    If bFAC And bNC Then Exit For 'already found both
    Next rw

    'loop over Details and see what rows can be matched to this number
    ' you'll need to figure out the details here...
    For rD = 2 To rngDets.Rows.Count
    If CStr(rngDets.Cells(rD, COL_DET_OPS_NUM).Value) = v Then
    rngDets.Cells(rD, COL_DET_LINKED).Value = rngOps.Cells(colRows(1), COL_OPS_NUMBER).Value
    'dataOps(rO, 4) = dataOps(rO, 4) + dataDets(rD, 2) 'fix this
    If bNC And bFAC Then 'have both types?
    rngDets.Cells(rD, COL_DET_NOTE).Value = "DONE"
    End If
    'copy the "money" value from Details back to Operations
    amt = rngDets.Cells(rD, COL_DET_MONEY).Value
    For Each rw In colRows
    If rngOps.Cells(rw, COL_OPS_TYPE).Value = VAL_NC Then
    rngOps.Cells(rw, COL_OPS_MONEY).Value = amt
    End If
    Next rw
    End If
    Next rD
    Next v
    End Sub


    'return all 11-digit strings in v as a Collection
    Function AllNumbers(v) As Collection
    Const NUM_DIGITS As Long = 11
    Dim m As Object, mc As Object, col As New Collection, txt, i As Long, patt, ss
    txt = " " & v & " "
    patt = String(NUM_DIGITS, "#")
    i = 2
    For i = 2 To Len(txt) - NUM_DIGITS
    ss = Mid(txt, i, 11)
    If ss Like patt Then
    If Not Mid(txt, i - 1, 1) Like "#" Then
    If Not Mid(txt, i + NUM_DIGITS, 1) Like "#" Then
    col.Add ss
    End If
    End If
    End If
    Next i
    Set AllNumbers = col
    End Function

    关于excel - 试图用 VBA 找到重复项来做一些奇怪的事情(一个棘手的问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71289108/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com