【问题标题】:!!VBA CODE: Selecting Duplicates and Copying Unique Values!!VBA 代码:选择重复项和复制唯一值
【发布时间】:2014-03-11 19:09:18
【问题描述】:

我正在尝试让我的宏打开一个新工作簿并粘贴所有具有重复值的行。我希望它为每组重复值创建一个新工作簿。

更具体地说,我的代码假设基于 datediff 值 2 选择单元格,将具有相同唯一标识符的所有单元格组合在一起,然后将其复制并粘贴到新工作簿中。

例如,如果单元格值是,

A1= 1234  B1= 2 
A2= 1234  B2= 5 
A3= 321   B3= 7 
A4= 234   B4= 2
A5= 234   B5= 2

宏会复制 A1 的整行,然后将其粘贴到新工作簿中,然后复制 A4 和 A5 的整行并将其粘贴到另一个新工作簿中,因为这些单元格的列 B=2。它会这样做,直到列中没有值为止。

我的代码的问题在于,它打开了 10 多个不同的新工作簿,其中一些具有值,一些没有。前几个做了我想要的,但最后几个是空白的。

Sub test()
Dim wbNew As Workbook
lr = Range("A" & Rows.Count).End(xlUp).Row
myarr = uniqueValues(Range("A1:A" & lr))
For i = LBound(myarr) To UBound(myarr)

 With Sheet1
        .AutoFilterMode = False
        .Range("A1").AutoFilter Field:=1, Criteria1:=myarr(i)
        .AutoFilter.Range.EntireRow.Copy
 Set wbNew = Workbooks.Add()
 wbNew.Worksheets(1).Paste

    Workbooks("Workbook2.xlsm").Sheets("Invoice Template (2)").Copy Before:=wbNew.Sheets(1)
    ActiveSheet.Name = "Current Invoice"

    Dim s As Integer
    s = 2

    Dim t As Integer
    t = 21

    wbNew.Worksheets(2).Activate

    Do Until IsEmpty(Cells(s, 3))
    mini = Cells(s, 21).Value
    If mini = "2" Then

    Dim wsInvoice As Worksheet
    Set wsInvoice = wbNew.Sheets("Current Invoice")

        wsInvoice.Cells(t, 2).Value = Cells(s, 10).Value    'Volumes'
        wsInvoice.Cells(t, 3).Value = Cells(s, 8).Value     'Benefits'
        wsInvoice.Cells(t, 7).Value = Cells(s, 11).Value    'Rates'
        wsInvoice.Cells(8, 2).Value = Cells(s, 14).Value    'Insurer Name'
        wsInvoice.Cells(9, 2).Value = Cells(s, 16).Value    'Insurer Address'
        wsInvoice.Cells(13, 2).Value = Cells(s, 3).Value    'Client Name'
        wsInvoice.Cells(14, 2).Value = Cells(s, 4).Value    'Client Address'
        wsInvoice.Cells(10, 9).Value = Cells(s, 1).Value    'Policy Number'
        wsInvoice.Cells(11, 9).Value = Cells(s, 22).Value   'Renewal Date'
        wsInvoice.Cells(12, 9).Value = Cells(s, 20).Value   'Anniversary Date'

    With wsInvoice
    Select Case Cells(s, 9)
        Case 1001  'Formula for Life, AD & D, ASI, CI'
            Prem = (.Cells(t, 2) * .Cells(t, 7)) / 1000
        Case 1103  'Formula for LTD'
            Prem = (.Cells(t, 2) * .Cells(t, 7)) / 100
        Case 1104  'Formula for STD'
            Prem = (.Cells(t, 2) * .Cells(t, 7)) / 10
        Case 2112  'General Formula'
            Prem = (.Cells(t, 2) * .Cells(t, 7))
    End Select

    .Cells(t, 9).Value = Prem
    End With

    With wsInvoice
    Select Case Cells(s, 15)
        Case 5501 'Commission schedule AIG'

        Case 5502 'Commission schedule ACE INA'

        Case 5503 'Commission schedule BBD'
            FrontL = 1
            HBack = 0
        Case 5504 'Commission schedule CBA'

        Case 5505 'Commission schedule ENCON'

        Case 5506 'Commission schedule Fenchurch'
            FrontL = 1
            HBack = 0
        Case 5507 'Commission schedule Great West Life'
            FrontL = 1
            HBack = 0
        Case 5508 'Commission schedule Great West Life SelectPac'
            FrontL = 1
            HBack = 0
        Case 5509 'Commission schedule Greenshield Canada'

        Case 5510 'Commission schedule GHG'

        Case 5511 'Commsion Schedule Industrial Alliance'
            FrontL = 0.9
            HBack = 0.1
        Case 5512 'Commission schedule Manulife'
            FrontL = 0.9
            HBack = 0.1
        Case 5513 'Commission schedule RBC'
            FrontL = 0.8
            HBack = 0.2
        Case 5514 'Commission schedule SunAdvantage'
            FrontL = 0.9
            HBack = 0.1
            Comm = 0.06
        Case 5515 'Commission schedule Sun Life Financial'
            FrontL = 0.9
            HBack = 0.1
            Comm = 0.1
    End Select

    .Cells(38, 8).Value = FrontL
    .Cells(39, 8).Value = HBack
    .Cells(18, 4).Value = Comm
    End With

        t = t + 1

    End If


    s = s + 1

    Loop

End With


Next i
End Sub


Dim cell As Range
Dim tempList As Variant: tempList = ""
For Each cell In InputRange
    If cell.Value <> "" Then
        If InStr(1, tempList, cell.Value) = 0 Then
            If tempList = "" Then tempList = Trim(CStr(cell.Value)) Else tempList = tempList & "|" & Trim(CStr(cell.Value))
        End If
    End If
Next cell
uniqueValues = Split(tempList, "|")
End Function

任何帮助都会令人惊叹和真正感激。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    整个子被包裹在一个for循环中

    For i = LBound(myarr) To UBound(myarr)
    

    您的工作表创建在此循环内,因此对于每个值,整个代码集都将运行。我还没有全部看过,但您可以先添加一个 if 语句来跳过某些不会产生输出的值。

    【讨论】:

    • 谢谢!我终于明白你的建议了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2013-03-04
    相关资源
    最近更新 更多